Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAT-18148: refactorings + tests #175

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
filipelautert marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ChangeObjectTests require to have the expectedSnapshot file to work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the checkboxes are green, there are failed tests https://github.com/liquibase/liquibase-databricks/pull/175/checks?check_run_id=29317521268

Does the workflow have errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expectedSql file is there... but the SQL is incorrect because we depend on the core fix .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About expectedSnapshots: I used createTableDataTypeText.xml as an example and there is no expectedSnapshot to it, just sql... weird

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)
Loading