Skip to content

Commit

Permalink
DAT-18148: refactorings + tests (#175)
Browse files Browse the repository at this point in the history
* feat: add support to auto-increment/generated columns

* feat: revert changes to auto-increment.

* feat: remove useless class

* chore(tests): adding tests for default computed values

* chore(tests): removing default keyword

* chore(tests): empty result to make tests work

* added expected json to createTableWithDefaultValues.json

---------

Co-authored-by: KushnirykOleh <[email protected]>
  • Loading branch information
filipelautert and KushnirykOleh authored Sep 10, 2024
1 parent 0a3a00b commit 6099eac
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -18,6 +19,10 @@
)
public class IntegerDatatypeDatabricks extends IntType {

@Getter
@Setter
private boolean autoIncrement;

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof DatabricksDatabase) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:databricks="http://www.liquibase.org/xml/ns/databricks"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/databricks
http://www.liquibase.org/xml/ns/databricks/liquibase-databricks-latest.xsd">

<changeSet author="fl" id="1">
<createTable tableName="tableWithDefaultValues" >
<column name="longcolumn" type="long" autoIncrement="true" generationType="IDENTITY" />
<column name="eventTime" type="timestamp"/>
<column name="year" type="int" defaultValueComputed="GENERATED ALWAYS AS (YEAR(eventTime))"/>
<column name="eventDate" type="date" defaultValueComputed="GENERATED ALWAYS AS (CAST(eventTime AS DATE))" />
<column name="eventDescription" type="string">
<constraints nullable="false" />
</column>
<column name="eventShortDescription" type="string" defaultValueComputed="GENERATED ALWAYS AS (SUBSTRING(eventDescription, 0, 1))" />
</createTable>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6099eac

Please sign in to comment.