diff --git a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java index 3d3ee95b..bf3d274b 100644 --- a/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java +++ b/src/main/java/liquibase/ext/databricks/database/DatabricksDatabase.java @@ -10,6 +10,7 @@ import liquibase.structure.DatabaseObject; import liquibase.structure.core.Catalog; import liquibase.structure.core.Schema; +import org.apache.commons.lang3.StringUtils; import lombok.Setter; import java.math.BigInteger; @@ -145,31 +146,49 @@ public String getAutoIncrementClause(final BigInteger startWith, final BigIntege } // generate an SQL:2003 STANDARD compliant auto increment clause by default - String autoIncrementClause = getAutoIncrementClause(generationType, defaultOnNull); boolean generateStartWith = generateAutoIncrementStartWith(startWith); boolean generateIncrementBy = generateAutoIncrementBy(incrementBy); if (generateStartWith || generateIncrementBy) { - autoIncrementClause += getAutoIncrementOpening(); - if (generateStartWith) { - autoIncrementClause += String.format(getAutoIncrementStartWithClause(), (startWith == null) ? defaultAutoIncrementStartWith : startWith); - } + autoIncrementClause += buildAutoIncrementClause(startWith, incrementBy, generateStartWith, generateIncrementBy); + } - if (generateIncrementBy) { - if (generateStartWith) { // for databricks there is no comma - autoIncrementClause += " "; + return autoIncrementClause; + } - } + private String buildAutoIncrementClause(final BigInteger startWith, final BigInteger incrementBy, boolean generateStartWith, boolean generateIncrementBy) { + StringBuilder clauseBuilder = new StringBuilder(getAutoIncrementOpening()); - autoIncrementClause += String.format(getAutoIncrementByClause(), (incrementBy == null) ? defaultAutoIncrementBy : incrementBy); - } + if (generateStartWith) { + clauseBuilder.append(String.format(getAutoIncrementStartWithClause(), (startWith == null) ? defaultAutoIncrementStartWith : startWith)); + } - autoIncrementClause += getAutoIncrementClosing(); + if (generateIncrementBy) { + if (generateStartWith) { // for databricks there is no comma + clauseBuilder.append(" "); + } + clauseBuilder.append(String.format(getAutoIncrementByClause(), (incrementBy == null) ? defaultAutoIncrementBy : incrementBy)); } - return autoIncrementClause; + clauseBuilder.append(getAutoIncrementClosing()); + return clauseBuilder.toString(); + } + + @Override + protected String getAutoIncrementClause() { + return "GENERATED BY DEFAULT AS IDENTITY"; + } + + @Override + protected String getAutoIncrementStartWithClause() { + return "START WITH %d"; + } + + @Override + protected String getAutoIncrementByClause() { + return "INCREMENT BY %d"; } @Override diff --git a/src/main/java/liquibase/ext/databricks/datatype/BigintDatatypeDatabricks.java b/src/main/java/liquibase/ext/databricks/datatype/BigintDatatypeDatabricks.java index a3a45c48..898c385b 100644 --- a/src/main/java/liquibase/ext/databricks/datatype/BigintDatatypeDatabricks.java +++ b/src/main/java/liquibase/ext/databricks/datatype/BigintDatatypeDatabricks.java @@ -1,19 +1,23 @@ package liquibase.ext.databricks.datatype; -import liquibase.datatype.core.BigIntType; -import liquibase.change.core.LoadDataChange; import liquibase.database.Database; import liquibase.datatype.DataTypeInfo; import liquibase.datatype.DatabaseDataType; -import liquibase.datatype.LiquibaseDataType; +import liquibase.datatype.core.BigIntType; import liquibase.ext.databricks.database.DatabricksDatabase; import liquibase.servicelocator.PrioritizedService; +import lombok.Getter; +import lombok.Setter; @DataTypeInfo(name = "bigint", aliases = {"java.sql.Types.BIGINT", "java.math.BigInteger", "java.lang.Long", "integer8", "bigserial", "serial8", "int8"}, minParameters = 0, maxParameters = 0, priority = PrioritizedService.PRIORITY_DATABASE) public class BigintDatatypeDatabricks extends BigIntType { + @Getter + @Setter + private boolean autoIncrement; + @Override public DatabaseDataType toDatabaseDataType(Database database) { if (database instanceof DatabricksDatabase) { diff --git a/src/main/java/liquibase/ext/databricks/datatype/DatetimeDatatypeDatabricks.java b/src/main/java/liquibase/ext/databricks/datatype/DatetimeDatatypeDatabricks.java index ac6ce694..1ae11b4d 100644 --- a/src/main/java/liquibase/ext/databricks/datatype/DatetimeDatatypeDatabricks.java +++ b/src/main/java/liquibase/ext/databricks/datatype/DatetimeDatatypeDatabricks.java @@ -1,10 +1,8 @@ package liquibase.ext.databricks.datatype; -import liquibase.change.core.LoadDataChange; import liquibase.database.Database; import liquibase.datatype.DataTypeInfo; import liquibase.datatype.DatabaseDataType; -import liquibase.datatype.LiquibaseDataType; import liquibase.datatype.core.DateTimeType; import liquibase.ext.databricks.database.DatabricksDatabase; import liquibase.servicelocator.PrioritizedService; diff --git a/src/main/java/liquibase/ext/databricks/datatype/IntegerDatatypeDatabricks.java b/src/main/java/liquibase/ext/databricks/datatype/IntegerDatatypeDatabricks.java index 4bec6600..5460c4de 100644 --- a/src/main/java/liquibase/ext/databricks/datatype/IntegerDatatypeDatabricks.java +++ b/src/main/java/liquibase/ext/databricks/datatype/IntegerDatatypeDatabricks.java @@ -1,12 +1,13 @@ package liquibase.ext.databricks.datatype; -import liquibase.change.core.LoadDataChange; import liquibase.database.Database; import liquibase.datatype.DataTypeInfo; import liquibase.datatype.DatabaseDataType; import liquibase.datatype.core.IntType; import liquibase.ext.databricks.database.DatabricksDatabase; import liquibase.servicelocator.PrioritizedService; +import lombok.Getter; +import lombok.Setter; @DataTypeInfo( @@ -18,6 +19,10 @@ ) public class IntegerDatatypeDatabricks extends IntType { + @Getter + @Setter + private boolean autoIncrement; + @Override public DatabaseDataType toDatabaseDataType(Database database) { if (database instanceof DatabricksDatabase) { diff --git a/src/test/resources/liquibase/harness/change/changelogs/databricks/createTableWithDefaultValues.xml b/src/test/resources/liquibase/harness/change/changelogs/databricks/createTableWithDefaultValues.xml new file mode 100644 index 00000000..51fbe93b --- /dev/null +++ b/src/test/resources/liquibase/harness/change/changelogs/databricks/createTableWithDefaultValues.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/liquibase/harness/change/expectedSnapshot/databricks/createTableWithDefaultValues.json b/src/test/resources/liquibase/harness/change/expectedSnapshot/databricks/createTableWithDefaultValues.json new file mode 100644 index 00000000..4b27f0f4 --- /dev/null +++ b/src/test/resources/liquibase/harness/change/expectedSnapshot/databricks/createTableWithDefaultValues.json @@ -0,0 +1,63 @@ +{ + "snapshot": { + "objects": { + "liquibase.structure.core.Table": [ + { + "table": { + "name": "tableWithDefaultValues" + } + } + ], + "liquibase.structure.core.Column": [ + { + "column": { + "name": "longcolumn", + "type": { + "typeName": "BIGINT" + } + } + }, + { + "column": { + "name": "eventTime", + "type": { + "typeName": "TIMESTAMP" + } + } + }, + { + "column": { + "name": "year", + "type": { + "typeName": "INT" + } + } + }, + { + "column": { + "name": "eventDate", + "type": { + "typeName": "DATE" + } + } + }, + { + "column": { + "name": "eventDescription", + "type": { + "typeName": "STRING" + } + } + }, + { + "column": { + "name": "eventShortDescription", + "type": { + "typeName": "STRING" + } + } + } + ] + } + } +} diff --git a/src/test/resources/liquibase/harness/change/expectedSql/databricks/createTableWithDefaultValues.sql b/src/test/resources/liquibase/harness/change/expectedSql/databricks/createTableWithDefaultValues.sql new file mode 100644 index 00000000..950940a0 --- /dev/null +++ b/src/test/resources/liquibase/harness/change/expectedSql/databricks/createTableWithDefaultValues.sql @@ -0,0 +1 @@ +CREATE TABLE main.liquibase_harness_test_ds.tableWithDefaultValues (longcolumn LONG GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1), eventTime TIMESTAMP, year INT GENERATED ALWAYS AS (YEAR(eventTime)), eventDate date GENERATED ALWAYS AS (CAST(eventTime AS DATE)), eventDescription STRING NOT NULL, eventShortDescription STRING GENERATED ALWAYS AS (SUBSTRING(eventDescription, 0, 1))) USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true) \ No newline at end of file