diff options
Diffstat (limited to 'sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java')
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java | 17 |
1 files changed, 17 insertions, 0 deletions
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)) { |