From cc9e6ccf56e64c5451e9478cd9b2949bbe41019a Mon Sep 17 00:00:00 2001 From: KushnirykOleh Date: Tue, 11 Jun 2024 17:10:21 +0300 Subject: [PATCH 1/3] fixed PKSnapshotGenerator, removed unneeded classes --- .../bigquery/database/BigQueryDatabase.java | 64 +++++++-------- .../BigQueryPrimaryKeySnapshotGenerator.java | 8 +- .../BigQuerySequenceSnapshotGenerator.java | 60 -------------- ...ueryUniqueConstraintSnapshotGenerator.java | 81 ------------------- .../liquibase.snapshot.SnapshotGenerator | 2 - .../liquibase.sqlgenerator.SqlGenerator | 2 - .../changelogs/bigquery/addPrimaryKey.xml | 8 -- .../expectedSnapshot/addPrimaryKey.json | 15 ++++ 8 files changed, 49 insertions(+), 191 deletions(-) delete mode 100644 src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQuerySequenceSnapshotGenerator.java delete mode 100644 src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryUniqueConstraintSnapshotGenerator.java delete mode 100644 src/test/resources/liquibase/harness/snapshot/changelogs/bigquery/addPrimaryKey.xml diff --git a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java index df69ac84..47d05021 100644 --- a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java +++ b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java @@ -11,10 +11,7 @@ import liquibase.executor.ExecutorService; import liquibase.statement.core.GetViewDefinitionStatement; import liquibase.structure.DatabaseObject; -import liquibase.structure.core.Catalog; -import liquibase.structure.core.Schema; -import liquibase.structure.core.Sequence; -import liquibase.structure.core.Table; +import liquibase.structure.core.*; import java.util.HashSet; import java.util.Set; @@ -54,11 +51,6 @@ public int getPriority() { return BIGQUERY_PRIORITY_DATABASE; } - @Override - public boolean supportsDatabaseChangeLogHistory() { - return true; - } - @Override public String correctObjectName(String objectName, Class objectType) { if (Table.class.isAssignableFrom(objectType) && objectName.equalsIgnoreCase("DATABASECHANGELOGHISTORY")) { @@ -100,29 +92,6 @@ public int getDatabaseMinorVersion() { return BQDriver.DRIVER_MINOR_VERSION; } - @Override - public boolean supports(Class object) { - if (Sequence.class.isAssignableFrom(object)) { - return false; - } - return super.supports(object); - } - - @Override - public boolean supportsInitiallyDeferrableColumns() { - return false; - } - - @Override - public boolean supportsDropTableCascadeConstraints() { - return false; - } - - @Override - public boolean supportsTablespaces() { - return false; - } - @Override protected String getQuotingStartCharacter() { return "`"; @@ -153,10 +122,14 @@ public String getDefaultDriver(String url) { return null; } + @Override + public boolean supportsDatabaseChangeLogHistory() { + return true; + } @Override public boolean supportsSequences() { - return this.supports(Sequence.class); + return false; } @Override @@ -165,12 +138,33 @@ public boolean supportsRestrictForeignKeys() { } @Override - public boolean supportsPrimaryKeyNames() { + public boolean supportsNotNullConstraintNames() { return false; } @Override - public boolean supportsNotNullConstraintNames() { + public boolean supports(Class object) { + if (Sequence.class.isAssignableFrom(object)) { + return false; + } + if (UniqueConstraint.class.isAssignableFrom(object)) { + return false; + } + return super.supports(object); + } + + @Override + public boolean supportsInitiallyDeferrableColumns() { + return false; + } + + @Override + public boolean supportsDropTableCascadeConstraints() { + return false; + } + + @Override + public boolean supportsTablespaces() { return false; } diff --git a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryPrimaryKeySnapshotGenerator.java b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryPrimaryKeySnapshotGenerator.java index 360f78d5..54ba8332 100644 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryPrimaryKeySnapshotGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryPrimaryKeySnapshotGenerator.java @@ -53,9 +53,10 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot } PrimaryKey returnKey = null; - String keyColumnUsageStatement = String.format("SELECT * FROM %s.INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE table_name = ?", schema.getSchema()); + String keyColumnUsageStatement = String.format("SELECT * FROM %s.INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME = ?", + schema.getSchema()); Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database); - List> maps = executor.queryForList(new RawParameterizedSqlStatement(keyColumnUsageStatement, searchTableName)); + List> maps = executor.queryForList(new RawParameterizedSqlStatement(keyColumnUsageStatement, example.getName())); String columnName; for (Map map : maps) { columnName = Objects.toString(map.get("COLUMN_NAME"), null); @@ -93,7 +94,8 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro Schema schema = table.getSchema(); Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database); - String tableConstraintsStatement = String.format("SELECT * FROM %s.INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE table_name = ?", schema.getSchema()); + String tableConstraintsStatement = String.format("SELECT * FROM %s.INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE " + + "CONSTRAINT_TYPE = 'PRIMARY KEY' AND table_name = ?", schema.getSchema()); List> maps = executor.queryForList(new RawParameterizedSqlStatement(tableConstraintsStatement, table.getName())); for (Map map : maps) { diff --git a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQuerySequenceSnapshotGenerator.java b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQuerySequenceSnapshotGenerator.java deleted file mode 100644 index ea860b2e..00000000 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQuerySequenceSnapshotGenerator.java +++ /dev/null @@ -1,60 +0,0 @@ -package liquibase.ext.bigquery.snapshot.jvm; - -import liquibase.CatalogAndSchema; -import liquibase.Scope; -import liquibase.database.AbstractJdbcDatabase; -import liquibase.database.Database; -import liquibase.exception.DatabaseException; -import liquibase.ext.bigquery.database.BigQueryDatabase; -import liquibase.snapshot.DatabaseSnapshot; -import liquibase.snapshot.SnapshotGenerator; -import liquibase.snapshot.jvm.SequenceSnapshotGenerator; -import liquibase.statement.SqlStatement; -import liquibase.statement.core.RawSqlStatement; -import liquibase.structure.DatabaseObject; -import liquibase.structure.core.Schema; - -public class BigQuerySequenceSnapshotGenerator extends SequenceSnapshotGenerator { - - @Override - public int getPriority(Class objectType, Database database) { - if (!(database instanceof BigQueryDatabase)) { - return PRIORITY_NONE; - } - int priority = super.getPriority(objectType, database); - if (priority > PRIORITY_NONE && database instanceof BigQueryDatabase) { - priority += PRIORITY_DATABASE; - } - return priority; - } - - @Override - public Class[] replaces() { - return new Class[]{SequenceSnapshotGenerator.class}; - } - - @Override - protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException { - Scope.getCurrentScope().getLog(this.getClass()).info("Sequences are not supported by BigQuery"); - return null; - } - - @Override - protected SqlStatement getSelectSequenceStatement(Schema schema, Database database) { - if (database instanceof BigQueryDatabase) { - // BigQuery does not support sequences - //String catalog = database.getDefaultCatalogName(); - CatalogAndSchema catalogAndSchema = (new CatalogAndSchema(schema.getCatalogName(), schema.getName())).customize(database); - - String jdbcSchemaName = database.correctObjectName(((AbstractJdbcDatabase) database).getJdbcSchemaName(catalogAndSchema), Schema.class); - - return new RawSqlStatement( - "SELECT NULL AS SEQUENCE_NAME, NULL AS START_VALUE, NULL AS AS MIN_VALUE, NULL AS MAX_VALUE, " + - "NULL AS INCREMENT_BY, " + - "NULL AS WILL_CYCLE FROM " + - jdbcSchemaName + "." + database.getSystemSchema().toUpperCase() + ".COLUMNS WHERE 1=0"); - } - return super.getSelectSequenceStatement(schema, database); - } - -} diff --git a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryUniqueConstraintSnapshotGenerator.java b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryUniqueConstraintSnapshotGenerator.java deleted file mode 100644 index 066c4832..00000000 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryUniqueConstraintSnapshotGenerator.java +++ /dev/null @@ -1,81 +0,0 @@ -package liquibase.ext.bigquery.snapshot.jvm; - -import liquibase.Scope; -import liquibase.database.Database; -import liquibase.exception.DatabaseException; -import liquibase.executor.ExecutorService; -import liquibase.ext.bigquery.database.BigQueryDatabase; -import liquibase.snapshot.CachedRow; -import liquibase.snapshot.DatabaseSnapshot; -import liquibase.snapshot.SnapshotGenerator; -import liquibase.snapshot.jvm.UniqueConstraintSnapshotGenerator; -import liquibase.statement.core.RawParameterizedSqlStatement; -import liquibase.structure.DatabaseObject; -import liquibase.structure.core.*; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class BigQueryUniqueConstraintSnapshotGenerator extends UniqueConstraintSnapshotGenerator { - - @Override - public int getPriority(Class objectType, Database database) { - if (!(database instanceof BigQueryDatabase)) { - return PRIORITY_NONE; - } - int priority = super.getPriority(objectType, database); - if (priority > PRIORITY_NONE && database instanceof BigQueryDatabase) { - priority += PRIORITY_DATABASE; - } - return priority; - } - - @Override - public Class[] replaces() { - return new Class[] { UniqueConstraintSnapshotGenerator.class }; - } - - @Override - protected List listConstraints(Table table, DatabaseSnapshot snapshot, Schema schema) throws DatabaseException, SQLException { - Scope.getCurrentScope().getLog(this.getClass()).info("Unique Constraints not supported by BigQuery"); - return new ArrayList<>(); //new BigQueryResultSetConstraintsExtractor(snapshot, schema.getCatalogName(), schema.getName(), table.getName())).fastFetch(); - } - - @Override - protected List> listColumns(UniqueConstraint example, Database database, DatabaseSnapshot snapshot) throws DatabaseException { - Relation table = example.getRelation(); - Schema schema = table.getSchema(); - String name = example.getName(); - String schemaName = database.correctObjectName(schema.getName(), Schema.class); - String constraintName = database.correctObjectName(name, UniqueConstraint.class); - String tableName = database.correctObjectName(table.getName(), Table.class); - - List parameters = new ArrayList<>(); - - StringBuilder sqlBuilder = new StringBuilder(); - sqlBuilder.append("select CONSTRAINT_NAME, COLUMN_NAME FROM ") - .append(database.getSystemSchema()) - .append(".TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='UNIQUE'"); - - if (schemaName != null) { - sqlBuilder.append("and CONSTRAINT_SCHEMA=? "); - parameters.add(schemaName); - } - - if (tableName != null) { - sqlBuilder.append("and TABLE_NAME=? "); - parameters.add(tableName); - } - - if (constraintName != null) { - sqlBuilder.append("and CONSTRAINT_NAME=? "); - parameters.add(constraintName); - } - - return Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database) - .queryForList(new RawParameterizedSqlStatement(sqlBuilder.toString(), parameters.toArray())); - } -} - diff --git a/src/main/resources/META-INF/services/liquibase.snapshot.SnapshotGenerator b/src/main/resources/META-INF/services/liquibase.snapshot.SnapshotGenerator index dda641a1..e768dfeb 100644 --- a/src/main/resources/META-INF/services/liquibase.snapshot.SnapshotGenerator +++ b/src/main/resources/META-INF/services/liquibase.snapshot.SnapshotGenerator @@ -1,6 +1,4 @@ liquibase.ext.bigquery.snapshot.jvm.BigQueryDatasetSnapshotGenerator -liquibase.ext.bigquery.snapshot.jvm.BigQueryUniqueConstraintSnapshotGenerator -liquibase.ext.bigquery.snapshot.jvm.BigQuerySequenceSnapshotGenerator liquibase.ext.bigquery.snapshot.jvm.BigQueryViewSnapshotGenerator liquibase.ext.bigquery.snapshot.jvm.BigQueryForeignKeySnapshotGenerator liquibase.ext.bigquery.snapshot.jvm.BigQueryPrimaryKeySnapshotGenerator diff --git a/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator b/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator index 43654dd4..1d7a8f8a 100644 --- a/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator +++ b/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator @@ -1,8 +1,6 @@ liquibase.ext.bigquery.sqlgenerator.BigQueryAddColumnGenerator liquibase.ext.bigquery.sqlgenerator.BigQueryAddForeignKeyConstraintGenerator liquibase.ext.bigquery.sqlgenerator.BigQueryAddPrimaryKeyConstraintGenerator -liquibase.ext.bigquery.sqlgenerator.BigQueryDropForeignKeyConstraintGenerator -liquibase.ext.bigquery.sqlgenerator.BigQueryDropPrimaryKeyConstraintGenerator liquibase.ext.bigquery.sqlgenerator.BigQueryCreateDatabaseChangeLogLockTableGenerator liquibase.ext.bigquery.sqlgenerator.BigQueryCreateDatabaseChangeLogTableGenerator liquibase.ext.bigquery.sqlgenerator.BigQueryDeleteGenerator diff --git a/src/test/resources/liquibase/harness/snapshot/changelogs/bigquery/addPrimaryKey.xml b/src/test/resources/liquibase/harness/snapshot/changelogs/bigquery/addPrimaryKey.xml deleted file mode 100644 index 467b138a..00000000 --- a/src/test/resources/liquibase/harness/snapshot/changelogs/bigquery/addPrimaryKey.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/test/resources/liquibase/harness/snapshot/expectedSnapshot/addPrimaryKey.json b/src/test/resources/liquibase/harness/snapshot/expectedSnapshot/addPrimaryKey.json index b095d07a..325e491f 100644 --- a/src/test/resources/liquibase/harness/snapshot/expectedSnapshot/addPrimaryKey.json +++ b/src/test/resources/liquibase/harness/snapshot/expectedSnapshot/addPrimaryKey.json @@ -1,6 +1,21 @@ { "snapshot": { "objects": { + "liquibase.structure.core.Table": [ + { + "table": { + "name": "test_table", + "primaryKey": "liquibase.structure.core.PrimaryKey.*" + } + } + ], + "liquibase.structure.core.PrimaryKey": [ + { + "primaryKey": { + "name": "test_table.pk$" + } + } + ] } } } \ No newline at end of file From 2fe2ed8bc9de7b24416638cba6c2512842441647 Mon Sep 17 00:00:00 2001 From: KushnirykOleh Date: Tue, 11 Jun 2024 18:14:18 +0300 Subject: [PATCH 2/3] reverted supportsSequences() changes --- .../java/liquibase/ext/bigquery/database/BigQueryDatabase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java index 47d05021..62191cf3 100644 --- a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java +++ b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java @@ -129,7 +129,7 @@ public boolean supportsDatabaseChangeLogHistory() { @Override public boolean supportsSequences() { - return false; + return this.supports(Sequence.class); } @Override From 3a358701c705ffcdadb433055e3b31d43f7756f6 Mon Sep 17 00:00:00 2001 From: KushnirykOleh Date: Wed, 12 Jun 2024 15:40:57 +0300 Subject: [PATCH 3/3] reverted supportsPrimaryKeyNames method --- .../liquibase/ext/bigquery/database/BigQueryDatabase.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java index 62191cf3..263694f7 100644 --- a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java +++ b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java @@ -142,6 +142,10 @@ public boolean supportsNotNullConstraintNames() { return false; } + public boolean supportsPrimaryKeyNames() { + return false; + } + @Override public boolean supports(Class object) { if (Sequence.class.isAssignableFrom(object)) {