aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-07 10:44:50 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-12 13:05:19 +0100
commit0690781a55308bdec3dca95f8cefa2d2838636b8 (patch)
tree91cf321430172622327497ffa72d5d6af48a58da
parent1b16981bab6f9309b80fa08dde67aef9a2b3dc1d (diff)
downloadsonarqube-0690781a55308bdec3dca95f8cefa2d2838636b8.tar.gz
sonarqube-0690781a55308bdec3dca95f8cefa2d2838636b8.zip
SONAR-5471 support default value in boolean and varchar columns
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java13
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java13
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java11
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java7
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java3
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java17
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java11
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java21
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java22
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java5
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java4
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java64
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java4
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java5
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java1
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java5
22 files changed, 193 insertions, 27 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
index 3d1c3d5539d..d9aae31fb29 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
@@ -19,13 +19,19 @@
*/
package org.sonar.db.version;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
public abstract class AbstractColumnDef implements ColumnDef {
private final String columnName;
private final boolean isNullable;
+ @CheckForNull
+ private final Object defaultValue;
- public AbstractColumnDef(String columnName, boolean isNullable) {
+ public AbstractColumnDef(String columnName, boolean isNullable, @Nullable Object defaultValue) {
this.columnName = columnName;
this.isNullable = isNullable;
+ this.defaultValue = defaultValue;
}
@Override
@@ -37,4 +43,9 @@ public abstract class AbstractColumnDef implements ColumnDef {
public boolean isNullable() {
return isNullable;
}
+
+ @Override
+ public Object getDefaultValue() {
+ return defaultValue;
+ }
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
index f61a30dd3df..3806cca6c4a 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
@@ -25,6 +25,7 @@ import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.PostgreSql;
import static com.google.common.collect.Lists.newArrayList;
+import static java.lang.String.format;
import static org.sonar.db.version.Validations.validateTableName;
/**
@@ -80,6 +81,18 @@ public class AddColumnsBuilder {
private void addColumn(StringBuilder sql, ColumnDef columnDef) {
sql.append(columnDef.getName()).append(" ").append(columnDef.generateSqlType(dialect));
+ Object defaultValue = columnDef.getDefaultValue();
+ if (defaultValue != null) {
+ sql.append(" DEFAULT ");
+ // TODO remove duplication with CreateTableBuilder
+ if (defaultValue instanceof String) {
+ sql.append(format("'%s'", defaultValue));
+ } else if (defaultValue instanceof Boolean) {
+ sql.append((boolean) defaultValue ? dialect.getTrueSqlValue() : dialect.getFalseSqlValue());
+ } else {
+ sql.append(defaultValue);
+ }
+ }
sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java
index da9765e428f..81e571d6b27 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java
@@ -28,6 +28,7 @@ import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Lists.newArrayList;
/**
@@ -54,6 +55,8 @@ public class AlterColumnsBuilder {
}
public AlterColumnsBuilder updateColumn(ColumnDef columnDef) {
+ // limitation of Oracle, only attribute changes must be defined in ALTER.
+ checkArgument(columnDef.getDefaultValue()==null, "Default value is not supported on alter of column '%s'", columnDef.getName());
columnDefs.add(columnDef);
return this;
}
@@ -137,6 +140,7 @@ public class AlterColumnsBuilder {
if (addNotNullableProperty) {
sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
}
+
}
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java
index dc62060e71c..c5eea2195ae 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java
@@ -28,7 +28,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class BigIntegerColumnDef extends AbstractColumnDef {
private BigIntegerColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
public static Builder newBigIntegerColumnDefBuilder() {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
index 31e51fb9897..9d94c404d30 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
@@ -31,7 +31,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class BlobColumnDef extends AbstractColumnDef {
public BlobColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
@Override
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java
index c2cd7d17789..6503b89127e 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.version;
+import javax.annotation.Nullable;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
@@ -32,8 +33,9 @@ import static org.sonar.db.version.Validations.validateColumnName;
* Used to define VARCHAR column
*/
public class BooleanColumnDef extends AbstractColumnDef {
+
private BooleanColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, builder.defaultValue);
}
public static Builder newBooleanColumnDefBuilder() {
@@ -59,7 +61,7 @@ public class BooleanColumnDef extends AbstractColumnDef {
public static class Builder {
private String columnName;
-
+ private Boolean defaultValue = null;
private boolean isNullable = true;
public Builder setColumnName(String columnName) {
@@ -72,6 +74,11 @@ public class BooleanColumnDef extends AbstractColumnDef {
return this;
}
+ public Builder setDefaultValue(@Nullable Boolean b) {
+ this.defaultValue = b;
+ return this;
+ }
+
public BooleanColumnDef build() {
validateColumnName(columnName);
return new BooleanColumnDef(this);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java
index a57f0043a2e..93e342bafe5 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java
@@ -19,7 +19,6 @@
*/
package org.sonar.db.version;
-import javax.annotation.CheckForNull;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
@@ -31,13 +30,11 @@ import static org.sonar.db.version.Validations.validateColumnName;
/**
* Used to define CLOB columns
- *
- * Warning, for the moment it's only supporting MsSQL
*/
public class ClobColumnDef extends AbstractColumnDef {
private ClobColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
public static Builder newClobColumnDefBuilder() {
@@ -63,9 +60,7 @@ public class ClobColumnDef extends AbstractColumnDef {
}
public static class Builder {
- @CheckForNull
private String columnName;
-
private boolean isNullable = true;
public Builder setColumnName(String columnName) {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
index 91d987e1838..7fc8e4bd013 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.version;
+import javax.annotation.CheckForNull;
import org.sonar.db.dialect.Dialect;
public interface ColumnDef {
@@ -29,4 +30,6 @@ public interface ColumnDef {
String generateSqlType(Dialect dialect);
+ @CheckForNull
+ Object getDefaultValue();
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java
index 84b34fda5b4..f771b94e07a 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java
@@ -39,6 +39,7 @@ import org.sonar.db.dialect.PostgreSql;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
+import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Stream.of;
import static org.sonar.db.version.Validations.validateConstraintName;
@@ -134,6 +135,7 @@ public class CreateTableBuilder {
res.append(columnDef.getName());
res.append(' ');
appendDataType(res, dialect, columnDef);
+ appendDefaultValue(res, columnDef);
appendNullConstraint(res, columnDef);
appendColumnFlags(res, dialect, columnDef);
if (columnDefIterator.hasNext()) {
@@ -169,6 +171,21 @@ public class CreateTableBuilder {
}
}
+ private void appendDefaultValue(StringBuilder sql, ColumnDef columnDef) {
+ Object defaultValue = columnDef.getDefaultValue();
+ if (defaultValue != null) {
+ sql.append(" DEFAULT ");
+
+ if (defaultValue instanceof String) {
+ sql.append(format("'%s'", defaultValue));
+ } else if (defaultValue instanceof Boolean) {
+ sql.append((boolean) defaultValue ? dialect.getTrueSqlValue() : dialect.getFalseSqlValue());
+ } else {
+ sql.append(defaultValue);
+ }
+ }
+ }
+
private void appendColumnFlags(StringBuilder res, Dialect dialect, ColumnDef columnDef) {
Collection<ColumnFlag> columnFlags = this.flagsByColumn.get(columnDef);
if (columnFlags != null && columnFlags.contains(ColumnFlag.AUTO_INCREMENT)) {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java
index ba4c46358ef..69c466958de 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java
@@ -38,7 +38,7 @@ public class DecimalColumnDef extends AbstractColumnDef {
private final int scale;
private DecimalColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
this.precision = builder.precision;
this.scale = builder.scale;
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java
index 8b41a8a4ed8..5b2098572fe 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java
@@ -32,7 +32,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class IntegerColumnDef extends AbstractColumnDef {
private IntegerColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
public static Builder newIntegerColumnDefBuilder() {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java
index 81fe6d06eb4..caf02b08d2b 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java
@@ -35,7 +35,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class TinyIntColumnDef extends AbstractColumnDef {
private TinyIntColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
@Override
diff --git a/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java
index cfab9db27e5..319f6869004 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java
@@ -20,6 +20,7 @@
package org.sonar.db.version;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.MsSql;
@@ -42,7 +43,7 @@ public class VarcharColumnDef extends AbstractColumnDef {
private final int columnSize;
private VarcharColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, builder.defaultValue);
this.columnSize = builder.columnSize;
}
@@ -71,6 +72,9 @@ public class VarcharColumnDef extends AbstractColumnDef {
private boolean isNullable = true;
+ @CheckForNull
+ public String defaultValue = null;
+
public Builder setColumnName(String columnName) {
this.columnName = validateColumnName(columnName);
return this;
@@ -86,6 +90,11 @@ public class VarcharColumnDef extends AbstractColumnDef {
return this;
}
+ public Builder setDefaultValue(@Nullable String s) {
+ this.defaultValue = s;
+ return this;
+ }
+
public VarcharColumnDef build() {
validateColumnName(columnName);
requireNonNull(columnSize, "Limit cannot be null");
diff --git a/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java
index d2df1bec86f..5587b107edf 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java
@@ -30,41 +30,44 @@ import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder;
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
public class AddColumnsBuilderTest {
- static final String TABLE_NAME = "issues";
+ private static final String TABLE_NAME = "issues";
+
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void add_columns_on_h2() {
assertThat(createSampleBuilder(new H2()).build())
- .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)");
+ .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default BOOLEAN DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)");
}
@Test
public void add_columns_on_mysql() {
assertThat(createSampleBuilder(new MySql()).build())
- .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)");
+ .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default TINYINT(1) DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)");
}
@Test
public void add_columns_on_oracle() {
assertThat(createSampleBuilder(new Oracle()).build())
- .isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR (10) NOT NULL)");
+ .isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR (10) NOT NULL, col_with_default NUMBER(1) DEFAULT 0 NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)");
}
@Test
public void add_columns_on_postgresql() {
assertThat(createSampleBuilder(new PostgreSql()).build())
- .isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL");
+ .isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL, ADD COLUMN col_with_default BOOLEAN DEFAULT false NOT NULL, ADD COLUMN varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL");
}
@Test
public void add_columns_on_mssql() {
assertThat(createSampleBuilder(new MsSql()).build())
- .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name NVARCHAR (10) NOT NULL");
+ .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name NVARCHAR (10) NOT NULL, col_with_default BIT DEFAULT 0 NOT NULL, varchar_col_with_default NVARCHAR (3) DEFAULT 'foo' NOT NULL");
}
@Test
@@ -78,6 +81,10 @@ public class AddColumnsBuilderTest {
private AddColumnsBuilder createSampleBuilder(Dialect dialect) {
return new AddColumnsBuilder(dialect, TABLE_NAME)
.addColumn(new BigIntegerColumnDef.Builder().setColumnName("date_in_ms").setIsNullable(true).build())
- .addColumn(new VarcharColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build());
+ .addColumn(new VarcharColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build())
+
+ // columns with default values
+ .addColumn(newBooleanColumnDefBuilder().setColumnName("col_with_default").setDefaultValue(false).setIsNullable(false).build())
+ .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_with_default").setLimit(3).setDefaultValue("foo").setIsNullable(false).build());
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java
index f38f225be97..d8369c3913b 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java
@@ -30,12 +30,13 @@ import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder;
import static org.sonar.db.version.DecimalColumnDef.newDecimalColumnDefBuilder;
import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
public class AlterColumnsBuilderTest {
- static final String TABLE_NAME = "issues";
+ private static final String TABLE_NAME = "issues";
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -115,6 +116,25 @@ public class AlterColumnsBuilderTest {
new AlterColumnsBuilder(new H2(), TABLE_NAME).build();
}
+ /**
+ * As we want DEFAULT value to be removed from all tables, it is supported
+ * only on creation of tables and columns, not on alter.
+ */
+ @Test
+ public void updateColumn_throws_IAE_if_default_value_is_defined() {
+ BooleanColumnDef column = newBooleanColumnDefBuilder()
+ .setColumnName("enabled")
+ .setIsNullable(false)
+ .setDefaultValue(false)
+ .build();
+ AlterColumnsBuilder alterColumnsBuilder = new AlterColumnsBuilder(new H2(), TABLE_NAME);
+
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Default value is not supported on alter of column 'enabled'");
+
+ alterColumnsBuilder.updateColumn(column);
+ }
+
private AlterColumnsBuilder createSampleBuilder(Dialect dialect) {
return new AlterColumnsBuilder(dialect, TABLE_NAME)
.updateColumn(
diff --git a/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java
index 76928a423ec..cb0486a39e8 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java
@@ -41,20 +41,23 @@ public class BooleanColumnDefTest {
BooleanColumnDef def = new BooleanColumnDef.Builder()
.setColumnName("enabled")
.setIsNullable(false)
+ .setDefaultValue(true)
.build();
assertThat(def.getName()).isEqualTo("enabled");
assertThat(def.isNullable()).isFalse();
+ assertThat(def.getDefaultValue()).isEqualTo(true);
}
@Test
- public void build_column_def_with_default_values() throws Exception {
+ public void build_column_def_with_only_required_attributes() throws Exception {
BooleanColumnDef def = new BooleanColumnDef.Builder()
.setColumnName("enabled")
.build();
assertThat(def.getName()).isEqualTo("enabled");
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isNull();
}
@Test
diff --git a/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java
index 6843b81711a..0479e278698 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java
@@ -43,16 +43,18 @@ public class ClobColumnDefTest {
public void build_string_column_def() throws Exception {
assertThat(underTest.getName()).isEqualTo("issues");
assertThat(underTest.isNullable()).isTrue();
+ assertThat(underTest.getDefaultValue()).isNull();
}
@Test
- public void build_string_column_def_with_default_values() throws Exception {
+ public void build_string_column_def_with_only_required_attributes() throws Exception {
ClobColumnDef def = new ClobColumnDef.Builder()
.setColumnName("issues")
.build();
assertThat(def.getName()).isEqualTo("issues");
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isNull();
}
@Test
diff --git a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java
index 12b32882f05..e9b765af1d1 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java
@@ -509,6 +509,70 @@ public class CreateTableBuilderTest {
});
}
+ @Test
+ public void build_adds_DEFAULT_clause_on_varchar_column_on_H2() {
+ verifyDefaultClauseOnVarcharColumn(H2, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL)");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_varchar_column_on_MSSQL() {
+ verifyDefaultClauseOnVarcharColumn(MS_SQL, "CREATE TABLE table_42 (status NVARCHAR (1) DEFAULT 'P' NOT NULL)");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_varchar_column_on_MySQL() {
+ verifyDefaultClauseOnVarcharColumn(MY_SQL, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_varchar_column_on_Oracle() {
+ verifyDefaultClauseOnVarcharColumn(ORACLE, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL)");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_varchar_column_on_PostgreSQL() {
+ verifyDefaultClauseOnVarcharColumn(POSTGRESQL, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL)");
+ }
+
+ private static void verifyDefaultClauseOnVarcharColumn(Dialect dialect, String expectedSql) {
+ List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME)
+ .addColumn(newVarcharColumnDefBuilder().setColumnName("status").setLimit(1).setIsNullable(false).setDefaultValue("P").build())
+ .build();
+ assertThat(stmts).containsExactly(expectedSql);
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_boolean_column_on_H2() {
+ verifyDefaultClauseOnBooleanColumn(H2, "CREATE TABLE table_42 (enabled BOOLEAN DEFAULT true NOT NULL)");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_boolean_column_on_MSSQL() {
+ verifyDefaultClauseOnBooleanColumn(MS_SQL, "CREATE TABLE table_42 (enabled BIT DEFAULT 1 NOT NULL)");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_boolean_column_on_MySQL() {
+ verifyDefaultClauseOnBooleanColumn(MY_SQL, "CREATE TABLE table_42 (enabled TINYINT(1) DEFAULT true NOT NULL) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_boolean_column_on_Oracle() {
+ verifyDefaultClauseOnBooleanColumn(ORACLE, "CREATE TABLE table_42 (enabled NUMBER(1) DEFAULT 1 NOT NULL)");
+ }
+
+ @Test
+ public void build_adds_DEFAULT_clause_on_boolean_column_on_PostgreSQL() {
+ verifyDefaultClauseOnBooleanColumn(POSTGRESQL, "CREATE TABLE table_42 (enabled BOOLEAN DEFAULT true NOT NULL)");
+ }
+
+ private static void verifyDefaultClauseOnBooleanColumn(Dialect dialect, String expectedSql) {
+ List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME)
+ .addColumn(newBooleanColumnDefBuilder().setColumnName("enabled").setIsNullable(false).setDefaultValue(true).build())
+ .build();
+ assertThat(stmts).containsExactly(expectedSql);
+ }
+
private static String bigIntSqlType(Dialect dialect) {
return Oracle.ID.equals(dialect.getId()) ? "NUMBER (38)" : "BIGINT";
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java
index bf16b4f3914..e5f70681fa6 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java
@@ -51,6 +51,7 @@ public class DecimalColumnDefTest {
assertThat(def.getPrecision()).isEqualTo(30);
assertThat(def.getScale()).isEqualTo(20);
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isNull();
}
@Test
@@ -94,7 +95,7 @@ public class DecimalColumnDefTest {
}
@Test
- public void create_builder_with_default_values() throws Exception {
+ public void create_builder_with_only_required_attributes() throws Exception {
DecimalColumnDef def = new DecimalColumnDef.Builder()
.setColumnName("issues")
.build();
@@ -102,6 +103,7 @@ public class DecimalColumnDefTest {
assertThat(def.getPrecision()).isEqualTo(38);
assertThat(def.getScale()).isEqualTo(20);
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isNull();
}
@Test
diff --git a/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java
index 935fb96cfd8..d36bb8c8b13 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java
@@ -76,6 +76,11 @@ public class IntegerColumnDefTest {
}
@Test
+ public void getDefaultValue_always_returns_null() {
+ assertThat(newIntegerColumnDefBuilder().setColumnName("a").build().getDefaultValue()).isNull();
+ }
+
+ @Test
public void generateSqlType_for_MsSql() {
assertThat(underTest.generateSqlType(new MsSql())).isEqualTo("INT");
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java
index 406536629e9..65cfbe3bec6 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java
@@ -47,6 +47,7 @@ public class TinyIntColumnDefTest {
assertThat(def.getName()).isEqualTo("foo");
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isNull();
}
@Test
diff --git a/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java
index 9eaa199fd51..1cf4fe54289 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java
@@ -41,15 +41,17 @@ public class VarcharColumnDefTest {
.setColumnName("issues")
.setLimit(10)
.setIsNullable(true)
+ .setDefaultValue("foo")
.build();
assertThat(def.getName()).isEqualTo("issues");
assertThat(def.getColumnSize()).isEqualTo(10);
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isEqualTo("foo");
}
@Test
- public void build_string_column_def_with_default_values() throws Exception {
+ public void build_string_column_def_with_only_required_attributes() throws Exception {
VarcharColumnDef def = new VarcharColumnDef.Builder()
.setColumnName("issues")
.setLimit(10)
@@ -58,6 +60,7 @@ public class VarcharColumnDefTest {
assertThat(def.getName()).isEqualTo("issues");
assertThat(def.getColumnSize()).isEqualTo(10);
assertThat(def.isNullable()).isTrue();
+ assertThat(def.getDefaultValue()).isNull();
}
@Test