generated from liquibase/liquibase-extension-example
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from liquibase/DAT-18790
[DAT-18790] added defaultValue snapshotting
- Loading branch information
Showing
6 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...est/java/liquibase/ext/databricks/snapshot/jvm/ColumnSnapshotGeneratorDatabricksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package liquibase.ext.databricks.snapshot.jvm; | ||
|
||
import liquibase.database.jvm.JdbcConnection; | ||
import liquibase.exception.DatabaseException; | ||
import liquibase.ext.databricks.database.DatabricksDatabase; | ||
import liquibase.snapshot.JdbcDatabaseSnapshot; | ||
import liquibase.statement.DatabaseFunction; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.Column; | ||
import liquibase.structure.core.Table; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ColumnSnapshotGeneratorDatabricksTest { | ||
|
||
@Mock | ||
private JdbcDatabaseSnapshot snapshot; | ||
@InjectMocks | ||
private ColumnSnapshotGeneratorDatabricks snapshotGenerator; | ||
@Captor | ||
private ArgumentCaptor<String> queryCaptor; | ||
private DatabaseObject testedColumn; | ||
|
||
private static final Map<String, String> COLUMN_WITH_DEFAULT_NAMES = new HashMap<String , String>() {{ | ||
put("intcolumn", "-101"); | ||
put("eventDescription", "default string, regular ? almost();!$#@^%[] String bigint"); | ||
put("eventShortDescription", "short desc"); | ||
}}; | ||
private static final Map<String, DatabaseFunction> COLUMN_WITH_DEFAULT_COMPUTED_NAMES = new HashMap<String , DatabaseFunction>() {{ | ||
put("eventTime", new DatabaseFunction("current_timestamp()")); | ||
put("year", new DatabaseFunction("YEAR(CURRENT_TIMESTAMP())")); | ||
put("eventDate", new DatabaseFunction("CAST(CURRENT_TIMESTAMP() AS DATE)")); | ||
}}; | ||
private static final String TEST_CATALOG_NAME = "main"; | ||
private static final String TEST_SCHEMA_NAME = "liquibase_harness_test_ds"; | ||
private static final String TEST_TABLE_NAME = "tablewithdefaultvalues"; | ||
private static final String EXPECTED_SHOW_CREATE_QUERY = "SHOW CREATE TABLE main.liquibase_harness_test_ds.tablewithdefaultvalues;"; | ||
private static final String SHOW_CREATE_TABLE_RESPONSE = "CREATE TABLE main.liquibase_harness_test_ds.tablewithdefaultvalues(" + | ||
"longcolumn BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1), " + | ||
"intcolumn INT DEFAULT -101, " + | ||
"eventTime TIMESTAMP NOT NULL DEFAULT current_timestamp(), " + | ||
"year INT DEFAULT YEAR(CURRENT_TIMESTAMP()), " + | ||
"eventDate DATE DEFAULT CAST(CURRENT_TIMESTAMP() AS DATE) COMMENT 'a comment, " + | ||
"eventDescription STRING NOT NULL DEFAULT 'default string, regular ? almost();!$#@^%[] String bigint', " + | ||
"eventShortDescription STRING DEFAULT \"short desc\") USING delta " + | ||
" TBLPROPERTIES ('delta.columnMapping.mode' = 'name', 'delta.feature.allowColumnDefaults' = 'supported') "; | ||
|
||
|
||
@BeforeEach | ||
public void setUp() throws DatabaseException, SQLException { | ||
snapshotGenerator = new ColumnSnapshotGeneratorDatabricks(); | ||
testedColumn = new Column(); | ||
testedColumn.setAttribute("relation", new Table(TEST_CATALOG_NAME, TEST_SCHEMA_NAME, TEST_TABLE_NAME)); | ||
when(snapshot.getScratchData(queryCaptor.capture())).thenReturn(SHOW_CREATE_TABLE_RESPONSE); | ||
} | ||
|
||
@Test | ||
void snapshotObjectTest() throws DatabaseException, SQLException { | ||
for(Map.Entry<String, String> columnWithDefault : COLUMN_WITH_DEFAULT_NAMES.entrySet()) { | ||
testedColumn.setName(columnWithDefault.getKey()); | ||
testedColumn.setAttribute("liquibase-complete", true); | ||
DatabaseObject databaseObject = snapshotGenerator.snapshotObject(testedColumn, snapshot); | ||
assertTrue(databaseObject instanceof Column); | ||
assertNull(((Column) databaseObject).getComputed()); | ||
assertNotNull(((Column) databaseObject).getDefaultValue()); | ||
assertEquals(columnWithDefault.getValue(), ((Column) databaseObject).getDefaultValue()); | ||
} | ||
for(Map.Entry<String, DatabaseFunction> columnWithDefaultComputed: COLUMN_WITH_DEFAULT_COMPUTED_NAMES.entrySet()) { | ||
testedColumn.setName(columnWithDefaultComputed.getKey()); | ||
testedColumn.setAttribute("liquibase-complete", true); | ||
DatabaseObject databaseObject = snapshotGenerator.snapshotObject(testedColumn, snapshot); | ||
assertTrue(databaseObject instanceof Column); | ||
assertTrue(((Column) databaseObject).getComputed()); | ||
assertNotNull(((Column) databaseObject).getDefaultValue()); | ||
assertEquals(columnWithDefaultComputed.getValue(), ((Column) databaseObject).getDefaultValue()); | ||
} | ||
assertEquals(EXPECTED_SHOW_CREATE_QUERY, queryCaptor.getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...esources/liquibase/harness/change/expectedSql/databricks/createTableWithDefaultValues.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +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) | ||
CREATE TABLE main.liquibase_harness_test_ds.tableWithDefaultValues (longcolumn LONG GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1), eventTime TIMESTAMP DEFAULT current_timestamp() NOT NULL, year INT DEFAULT YEAR(CURRENT_TIMESTAMP()), eventDate date DEFAULT CAST(CURRENT_TIMESTAMP() AS DATE), eventDescription STRING DEFAULT 'default string, regular ? almost();!$#@^%[] String bigint' NOT NULL, eventShortDescription STRING DEFAULT 'short desc') USING delta TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported', 'delta.columnMapping.mode' = 'name', 'delta.enableDeletionVectors' = true) |