diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml index 015769b1..267a310e 100644 --- a/.github/release-drafter.yml +++ b/.github/release-drafter.yml @@ -1,5 +1,5 @@ -name-template: 'Support for Liquibase BigQuery Extension v$NEXT_MINOR_VERSION' +name-template: 'Support for Liquibase BigQuery Extension v$RESOLVED_VERSION' tag-template: 'v$RESOLVED_VERSION' exclude-labels: - 'skipReleaseNotes' @@ -49,4 +49,4 @@ template: | $CHANGES - **Full Changelog**: https://github.com/liquibase/liquibase-hibernate/compare/$PREVIOUS_TAG...$RESOLVED_VERSION + **Full Changelog**: https://github.com/liquibase/liquibase-bigquery/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION diff --git a/src/main/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChange.java b/src/main/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChange.java index 7337abb1..25de0c34 100644 --- a/src/main/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChange.java +++ b/src/main/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChange.java @@ -9,7 +9,7 @@ import liquibase.ext.bigquery.database.BigQueryDatabase; import liquibase.servicelocator.PrioritizedService; import liquibase.statement.SqlStatement; -import liquibase.statement.core.RawSqlStatement; +import liquibase.statement.core.RawParameterizedSqlStatement; import liquibase.structure.core.Column; import java.util.ArrayList; @@ -28,7 +28,6 @@ public boolean supports(Database database) { @Override public SqlStatement[] generateStatements(final Database database) { - List statements = new ArrayList<>(); AddColumnChange addNewColumnChange = new AddColumnChange(); addNewColumnChange.setSchemaName(getSchemaName()); @@ -37,9 +36,9 @@ public SqlStatement[] generateStatements(final Database database) { columnConfig.setName(getFinalColumnName()); columnConfig.setType(getFinalColumnType()); addNewColumnChange.addColumn(columnConfig); - statements.addAll(Arrays.asList(addNewColumnChange.generateStatements(database))); + List statements = new ArrayList<>(Arrays.asList(addNewColumnChange.generateStatements(database))); - String updateStatement = ""; + String updateStatement; updateStatement = "UPDATE " + database.escapeTableName(getCatalogName(), getSchemaName(), getTableName()) + " SET " + database.escapeObjectName(getFinalColumnName(), Column.class) @@ -47,7 +46,7 @@ public SqlStatement[] generateStatements(final Database database) { , "'" + getJoinString() + "'", database.escapeObjectName(getColumn2Name(), Column.class)) + " WHERE 1 = 1 "; - statements.add(new RawSqlStatement(updateStatement)); + statements.add(new RawParameterizedSqlStatement(updateStatement)); DropColumnChange dropColumn1Change = new DropColumnChange(); dropColumn1Change.setSchemaName(getSchemaName()); @@ -61,6 +60,6 @@ public SqlStatement[] generateStatements(final Database database) { dropColumn2Change.setColumnName(getColumn2Name()); statements.addAll(Arrays.asList(dropColumn2Change.generateStatements(database))); - return statements.toArray(new SqlStatement[statements.size()]); + return statements.toArray(new SqlStatement[0]); } } diff --git a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java index 263694f7..da702ba6 100644 --- a/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java +++ b/src/main/java/liquibase/ext/bigquery/database/BigQueryDatabase.java @@ -108,7 +108,7 @@ public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws D } @Override public String escapeObjectName(String objectName, Class objectType) { - if (objectType.equals(Schema.class) || objectType.equals(Catalog.class)) { + if (objectType.equals(Schema.class)) { return objectName; } return super.escapeObjectName(objectName, objectType); @@ -142,6 +142,7 @@ public boolean supportsNotNullConstraintNames() { return false; } + @Override public boolean supportsPrimaryKeyNames() { return false; } diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/BignumericDataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/BignumericDataTypeBigQuery.java index 7411a749..73aa8998 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/BignumericDataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/BignumericDataTypeBigQuery.java @@ -19,9 +19,6 @@ public class BignumericDataTypeBigQuery extends LiquibaseDataType { private static final String BIGNUMERIC = "BIGNUMERIC"; - public BignumericDataTypeBigQuery() { - } - @Override public boolean supports(Database database) { return database instanceof BigQueryDatabase; diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/BoolDataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/BoolDataTypeBigQuery.java index 179cd5b6..5d603610 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/BoolDataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/BoolDataTypeBigQuery.java @@ -16,8 +16,6 @@ priority = PrioritizedService.PRIORITY_DATABASE ) public class BoolDataTypeBigQuery extends LiquibaseDataType { - public BoolDataTypeBigQuery() { - } @Override public boolean supports(Database database) { diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/Float64DataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/Float64DataTypeBigQuery.java index fb16f402..de8f581d 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/Float64DataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/Float64DataTypeBigQuery.java @@ -16,8 +16,6 @@ priority = PrioritizedService.PRIORITY_DATABASE ) public class Float64DataTypeBigQuery extends LiquibaseDataType { - public Float64DataTypeBigQuery() { - } @Override public boolean supports(Database database) { diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/GeographyDataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/GeographyDataTypeBigQuery.java index ea50e6fe..912774e8 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/GeographyDataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/GeographyDataTypeBigQuery.java @@ -16,8 +16,6 @@ priority = PrioritizedService.PRIORITY_DATABASE ) public class GeographyDataTypeBigQuery extends LiquibaseDataType { - public GeographyDataTypeBigQuery() { - } @Override public boolean supports(Database database) { diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/Int64DataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/Int64DataTypeBigQuery.java index 31a2a2a7..beca88f2 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/Int64DataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/Int64DataTypeBigQuery.java @@ -16,8 +16,6 @@ priority = PrioritizedService.PRIORITY_DATABASE ) public class Int64DataTypeBigQuery extends LiquibaseDataType { - public Int64DataTypeBigQuery() { - } @Override public boolean supports(Database database) { diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/NumericDataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/NumericDataTypeBigQuery.java index 27a4d94b..db33e70c 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/NumericDataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/NumericDataTypeBigQuery.java @@ -16,8 +16,6 @@ priority = PrioritizedService.PRIORITY_DATABASE ) public class NumericDataTypeBigQuery extends LiquibaseDataType { - public NumericDataTypeBigQuery() { - } @Override public boolean supports(Database database) { diff --git a/src/main/java/liquibase/ext/bigquery/datatype/core/StringDataTypeBigQuery.java b/src/main/java/liquibase/ext/bigquery/datatype/core/StringDataTypeBigQuery.java index 5755a099..5924af92 100644 --- a/src/main/java/liquibase/ext/bigquery/datatype/core/StringDataTypeBigQuery.java +++ b/src/main/java/liquibase/ext/bigquery/datatype/core/StringDataTypeBigQuery.java @@ -17,8 +17,6 @@ aliases = { "varchar", "clob", "java.lang.String" } ) public class StringDataTypeBigQuery extends VarcharType { - public StringDataTypeBigQuery() { - } @Override public boolean supports(Database database) { @@ -28,19 +26,19 @@ public boolean supports(Database database) { @Override public DatabaseDataType toDatabaseDataType(Database database) { if (database instanceof BigQueryDatabase) { - - DatabaseDataType type = new DatabaseDataType("STRING", this.getParameters()); + String dataTypeString = "STRING"; + DatabaseDataType type = new DatabaseDataType(dataTypeString, this.getParameters()); if (this.getParameters().length == 0) { - type.setType("STRING"); + type.setType(dataTypeString); } else { String firstParameter = String.valueOf(this.getParameters()[0]); try { int stringSize = Integer.parseInt(firstParameter); if (stringSize == 65535) { - type.setType("STRING"); + type.setType(dataTypeString); } } catch (NumberFormatException e) { - type.setType("STRING"); + type.setType(dataTypeString); } } return type; diff --git a/src/main/java/liquibase/ext/bigquery/diff/output/changelog/core/BigQueryChangedTableChangeGenerator.java b/src/main/java/liquibase/ext/bigquery/diff/output/changelog/core/BigQueryChangedTableChangeGenerator.java index c41cf7f9..1456dc3b 100644 --- a/src/main/java/liquibase/ext/bigquery/diff/output/changelog/core/BigQueryChangedTableChangeGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/diff/output/changelog/core/BigQueryChangedTableChangeGenerator.java @@ -15,9 +15,6 @@ public class BigQueryChangedTableChangeGenerator extends ChangedTableChangeGenerator { - public BigQueryChangedTableChangeGenerator() { - } - @Override public int getPriority(Class objectType, Database database) { int priority = super.getPriority(objectType, database); @@ -50,7 +47,7 @@ public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences diffe Scope.getCurrentScope().getLog(this.getClass()).warning("A change of the tablespace was detected, however, Liquibase does not currently generate statements to move a table between tablespaces."); } - return null; + return new Change[0]; } } diff --git a/src/main/java/liquibase/ext/bigquery/executor/BigQueryExecutor.java b/src/main/java/liquibase/ext/bigquery/executor/BigQueryExecutor.java deleted file mode 100644 index b4634a44..00000000 --- a/src/main/java/liquibase/ext/bigquery/executor/BigQueryExecutor.java +++ /dev/null @@ -1,35 +0,0 @@ -package liquibase.ext.bigquery.executor; - -import liquibase.database.Database; -import liquibase.exception.DatabaseException; -import liquibase.executor.jvm.JdbcExecutor; -import liquibase.ext.bigquery.database.BigQueryDatabase; -import liquibase.sql.visitor.SqlVisitor; -import liquibase.statement.SqlStatement; - -import java.util.List; - -import static liquibase.ext.bigquery.database.BigQueryDatabase.BIGQUERY_PRIORITY_DATABASE; - -public class BigQueryExecutor extends JdbcExecutor { - - @Override - public int getPriority() { - return BIGQUERY_PRIORITY_DATABASE; - } - - @Override - public boolean supports(Database database) { - return database instanceof BigQueryDatabase; - } - - @Override - public void execute(SqlStatement sql) throws DatabaseException { - super.execute(sql); - } - - @Override - public void execute(SqlStatement sql, List sqlVisitors) throws DatabaseException { - super.execute(sql, sqlVisitors); - } -} diff --git a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryDatasetSnapshotGenerator.java b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryDatasetSnapshotGenerator.java index 208aa749..d48dee7b 100644 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryDatasetSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryDatasetSnapshotGenerator.java @@ -24,35 +24,26 @@ public class BigQueryDatasetSnapshotGenerator extends SchemaSnapshotGenerator { @Override public int getPriority(Class objectType, Database database) { - if (!(database instanceof BigQueryDatabase)) { + if (database instanceof BigQueryDatabase) { + return super.getPriority(objectType, database) + PRIORITY_DATABASE; + } else { return PRIORITY_NONE; } - int priority = super.getPriority(objectType, database); - if (priority > PRIORITY_NONE && database instanceof BigQueryDatabase) { - priority += PRIORITY_DATABASE; - } - return priority; } @Override protected String[] getDatabaseSchemaNames(Database database) throws SQLException, DatabaseException { List returnList = new ArrayList<>(); - ResultSet schemas = null; - try { - schemas = ((JdbcConnection) database.getConnection()).getMetaData() - .getSchemas(database.getDefaultCatalogName(), null); + try (ResultSet schemas = ((JdbcConnection) database.getConnection()).getMetaData() + .getSchemas(database.getDefaultCatalogName(), null)) { while (schemas.next()) { returnList.add(JdbcUtil.getValueForColumn(schemas, "TABLE_SCHEM", database)); } - } finally { - if (schemas != null) { - schemas.close(); - } } - return returnList.toArray(new String[returnList.size()]); + return returnList.toArray(new String[0]); } @Override @@ -106,8 +97,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot Catalog catalog = example1.getCatalog(); String[] dbCatalogNames = this.getDatabaseCatalogNames(database); - for (int i = 0; i < dbCatalogNames.length; ++i) { - String candidateCatalogName = dbCatalogNames[i]; + for (String candidateCatalogName : dbCatalogNames) { if (catalog.equals(new Catalog(candidateCatalogName))) { match = new Schema(catalog, catalogName); } diff --git a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryForeignKeySnapshotGenerator.java b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryForeignKeySnapshotGenerator.java index 706fa90f..e85d915f 100644 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryForeignKeySnapshotGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryForeignKeySnapshotGenerator.java @@ -40,7 +40,6 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot String fkFullName = searchTableName + "." + example.getName(); String searchCatalog = ((AbstractJdbcDatabase) database).getJdbcCatalogName(fkTable.getSchema()); String searchSchema = ((AbstractJdbcDatabase) database).getJdbcSchemaName(fkTable.getSchema()); - String systemSchema = database.getSystemSchema().toUpperCase(); String query = new StringBuilder("SELECT ") .append("TC.CONSTRAINT_NAME as CONSTRAINT_NAME, ") @@ -52,9 +51,9 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot .append("CCU.TABLE_SCHEMA as PRIMARY_KEY_TABLE_SCHEMA, ") .append("CCU.TABLE_NAME as PRIMARY_KEY_TABLE, ") .append("CCU.COLUMN_NAME as PRIMARY_KEY_COLUMN ") - .append(String.format("FROM %1$s.%2$s.TABLE_CONSTRAINTS as TC JOIN %1$s.%2$s.CONSTRAINT_COLUMN_USAGE as CCU on " + - "TC.CONSTRAINT_NAME=CCU.CONSTRAINT_NAME JOIN %1$s.%2$s.KEY_COLUMN_USAGE as KCU on KCU.CONSTRAINT_NAME=TC.CONSTRAINT_NAME ", - searchSchema, systemSchema)) + .append(String.format("FROM %1$s.INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC JOIN %1$s.INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as CCU on " + + "TC.CONSTRAINT_NAME=CCU.CONSTRAINT_NAME JOIN %1$s.INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU on KCU.CONSTRAINT_NAME=TC.CONSTRAINT_NAME ", + database.escapeObjectName(searchSchema, Schema.class))) .append("WHERE TC.TABLE_NAME=? AND TC.TABLE_SCHEMA=? AND TC.TABLE_CATALOG=? AND TC.CONSTRAINT_TYPE='FOREIGN KEY' AND TC.CONSTRAINT_NAME=?") .toString(); List> results = Scope.getCurrentScope().getSingleton(ExecutorService.class) @@ -113,13 +112,10 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro Database database = snapshot.getDatabase(); Schema schema = table.getSchema(); - CatalogAndSchema catalogAndSchema = (new CatalogAndSchema(schema.getCatalogName(), schema.getName())).customize(database); - String jdbcSchemaName = database.correctObjectName(((AbstractJdbcDatabase) database).getJdbcSchemaName(catalogAndSchema), Schema.class); - String query = String.format("SELECT CONSTRAINT_NAME FROM %s.%s.TABLE_CONSTRAINTS WHERE TABLE_NAME=? AND TABLE_SCHEMA=? AND TABLE_CATALOG=? AND " + - "CONSTRAINT_TYPE='FOREIGN KEY';", jdbcSchemaName, database.getSystemSchema().toUpperCase()); + String query = String.format("SELECT CONSTRAINT_NAME FROM %s.%s.INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY';", + database.escapeObjectName(schema.getCatalogName(), Catalog.class), schema.getName()); List> tableConstraints = Scope.getCurrentScope().getSingleton(ExecutorService.class) - .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query, table.getName(), schema.getName(), - schema.getCatalogName())); + .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query, table.getName())); for (Map row : tableConstraints) { String foreignKeyName = Objects.toString(row.get("CONSTRAINT_NAME")); ForeignKey fk = new ForeignKey() 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 54ba8332..d988edb2 100644 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryPrimaryKeySnapshotGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryPrimaryKeySnapshotGenerator.java @@ -8,7 +8,6 @@ import liquibase.executor.ExecutorService; import liquibase.ext.bigquery.database.BigQueryDatabase; import liquibase.snapshot.DatabaseSnapshot; -import liquibase.snapshot.InvalidExampleException; import liquibase.snapshot.SnapshotGenerator; import liquibase.snapshot.jvm.PrimaryKeySnapshotGenerator; import liquibase.statement.core.RawParameterizedSqlStatement; @@ -24,17 +23,14 @@ import java.util.Objects; public class BigQueryPrimaryKeySnapshotGenerator extends PrimaryKeySnapshotGenerator { - + private static final String CONSTRAINT_NAME = "CONSTRAINT_NAME"; @Override public int getPriority(Class objectType, Database database) { - if (!(database instanceof BigQueryDatabase)) { + if (database instanceof BigQueryDatabase) { + return super.getPriority(objectType, database) + PRIORITY_DATABASE; + } else { return PRIORITY_NONE; } - int priority = super.getPriority(objectType, database); - if (priority > PRIORITY_NONE) { - priority += PRIORITY_DATABASE; - } - return priority; } @Override @@ -43,18 +39,12 @@ public Class[] replaces() { } @Override - protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { + protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException { Database database = snapshot.getDatabase(); - Schema schema = example.getSchema(); - String searchTableName = null; - if (((PrimaryKey) example).getTable() != null) { - searchTableName = ((PrimaryKey) example).getTable().getName(); - searchTableName = database.correctObjectName(searchTableName, Table.class); - } PrimaryKey returnKey = null; String keyColumnUsageStatement = String.format("SELECT * FROM %s.INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME = ?", - schema.getSchema()); + example.getSchema().getName()); Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database); List> maps = executor.queryForList(new RawParameterizedSqlStatement(keyColumnUsageStatement, example.getName())); String columnName; @@ -68,7 +58,7 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot String schemaName = (String) map.get("TABLE_SCHEMA"); CatalogAndSchema tableSchema = new CatalogAndSchema(catalogName, schemaName); returnKey.setTable((Table) new Table().setName(Objects.toString(map.get("TABLE_NAME"), null)).setSchema(new Schema(tableSchema.getCatalogName(), tableSchema.getSchemaName()))); - returnKey.setName(Objects.toString(map.get("CONSTRAINT_NAME"), null)); + returnKey.setName(Objects.toString(map.get(CONSTRAINT_NAME), null)); } returnKey.addColumn(position - 1, new Column(columnName) @@ -91,16 +81,15 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro if (foundObject instanceof Table) { Table table = (Table) foundObject; Database database = snapshot.getDatabase(); - 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 " + - "CONSTRAINT_TYPE = 'PRIMARY KEY' AND table_name = ?", schema.getSchema()); + "CONSTRAINT_TYPE = 'PRIMARY KEY' AND table_name = ?", table.getSchema().getName()); List> maps = executor.queryForList(new RawParameterizedSqlStatement(tableConstraintsStatement, table.getName())); for (Map map : maps) { - if (map.containsKey("CONSTRAINT_NAME")) { - String constraintName = Objects.toString(map.get("CONSTRAINT_NAME"), null); + if (map.containsKey(CONSTRAINT_NAME)) { + String constraintName = Objects.toString(map.get(CONSTRAINT_NAME), null); PrimaryKey primaryKey = new PrimaryKey().setName(constraintName); primaryKey.setTable((Table) foundObject); if (!database.isSystemObject(primaryKey)) { diff --git a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryViewSnapshotGenerator.java b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryViewSnapshotGenerator.java index 488b074a..923541ab 100644 --- a/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryViewSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/snapshot/jvm/BigQueryViewSnapshotGenerator.java @@ -9,7 +9,7 @@ import liquibase.ext.bigquery.database.BigQueryDatabase; import liquibase.snapshot.DatabaseSnapshot; import liquibase.snapshot.jvm.ViewSnapshotGenerator; -import liquibase.statement.core.RawSqlStatement; +import liquibase.statement.core.RawParameterizedSqlStatement; import liquibase.structure.DatabaseObject; import liquibase.structure.core.Schema; import liquibase.structure.core.View; @@ -20,17 +20,13 @@ public class BigQueryViewSnapshotGenerator extends ViewSnapshotGenerator { - @Override public int getPriority(Class objectType, Database database) { - if (!(database instanceof BigQueryDatabase)) { + if (database instanceof BigQueryDatabase) { + return super.getPriority(objectType, database) + PRIORITY_DATABASE; + } else { return PRIORITY_NONE; } - int priority = super.getPriority(objectType, database); - if (priority > PRIORITY_NONE && database instanceof BigQueryDatabase) { - priority += PRIORITY_DATABASE; - } - return priority; } @@ -44,12 +40,12 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot Schema schema = example.getSchema(); CatalogAndSchema catalogAndSchema = (new CatalogAndSchema(schema.getCatalogName(), schema.getName())).customize(database); - String jdbcSchemaName = database.correctObjectName(((AbstractJdbcDatabase) database).getJdbcSchemaName(catalogAndSchema), Schema.class); - String query = String.format("SELECT view_definition FROM %s.%s.VIEWS WHERE table_name='%s' AND table_schema='%s' AND table_catalog='%s';", - jdbcSchemaName, database.getSystemSchema().toUpperCase(), example.getName(), schema.getName(), schema.getCatalogName()); + String jdbcSchemaName = database.escapeObjectName(((AbstractJdbcDatabase) database).getJdbcSchemaName(catalogAndSchema), Schema.class); + String query = String.format("SELECT view_definition FROM %s.INFORMATION_SCHEMA.VIEWS WHERE table_name=? AND table_schema=? AND " + + "table_catalog=?", jdbcSchemaName); List> viewsMetadataRs = Scope.getCurrentScope().getSingleton(ExecutorService.class) - .getExecutor("jdbc", database).queryForList(new RawSqlStatement(query)); + .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query, example.getName(), schema.getName(), schema.getCatalogName())); if (viewsMetadataRs.isEmpty()) { return null; diff --git a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryCreateTableGenerator.java b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryCreateTableGenerator.java index 7cda3c9f..494143e0 100644 --- a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryCreateTableGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryCreateTableGenerator.java @@ -121,6 +121,6 @@ public Sql[] generateSql(CreateTableStatement statement, Database database, SqlG String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")"; additionalSql.add(0, new UnparsedSql(sql, this.getAffectedTable(statement))); - return additionalSql.toArray(new Sql[additionalSql.size()]); + return additionalSql.toArray(new Sql[0]); } } diff --git a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryInsertOrUpdateGenerator.java b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryInsertOrUpdateGenerator.java index 2d8e692e..ec83d7a9 100644 --- a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryInsertOrUpdateGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryInsertOrUpdateGenerator.java @@ -11,8 +11,6 @@ public class BigQueryInsertOrUpdateGenerator extends InsertOrUpdateGenerator { - public BigQueryInsertOrUpdateGenerator() { - } @Override public boolean supports(InsertOrUpdateStatement statement, Database database) { diff --git a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameTableGenerator.java b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameTableGenerator.java index 3754fd6e..463550de 100644 --- a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameTableGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameTableGenerator.java @@ -26,9 +26,10 @@ public boolean supports(RenameTableStatement statement, Database database) { @Override public Sql[] generateSql(RenameTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { String sql; - sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getOldTableName()) + " RENAME TO " + database.escapeObjectName(statement.getNewTableName(), Table.class); + sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getOldTableName()) + " RENAME TO " + + database.escapeObjectName(statement.getNewTableName(), Table.class); - return new Sql[]{new UnparsedSql(sql, new DatabaseObject[]{this.getAffectedOldTable(statement), this.getAffectedNewTable(statement)})}; + return new Sql[]{new UnparsedSql(sql, this.getAffectedOldTable(statement), this.getAffectedNewTable(statement))}; } } diff --git a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameViewGenerator.java b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameViewGenerator.java index c2b1408b..98c56953 100644 --- a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameViewGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryRenameViewGenerator.java @@ -13,8 +13,6 @@ public class BigQueryRenameViewGenerator extends RenameViewGenerator { - public BigQueryRenameViewGenerator(){} - @Override public int getPriority() { return BIGQUERY_PRIORITY_DATABASE; @@ -28,6 +26,9 @@ public boolean supports(RenameViewStatement statement, Database database) { @Override public Sql[] generateSql(RenameViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { - return new Sql[]{new UnparsedSql("ALTER VIEW " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getOldViewName()) + " RENAME TO " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getNewViewName()), new DatabaseObject[]{this.getAffectedOldView(statement), this.getAffectedNewView(statement)})}; + return new Sql[]{ + new UnparsedSql("ALTER VIEW " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getOldViewName()) + + " RENAME TO " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getNewViewName()), + this.getAffectedOldView(statement), this.getAffectedNewView(statement))}; } } diff --git a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryUpdateGenerator.java b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryUpdateGenerator.java index 7977a7d3..3de6b262 100644 --- a/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryUpdateGenerator.java +++ b/src/main/java/liquibase/ext/bigquery/sqlgenerator/BigQueryUpdateGenerator.java @@ -16,10 +16,6 @@ public class BigQueryUpdateGenerator extends UpdateGenerator { - public BigQueryUpdateGenerator() { - super(); - } - @Override public int getPriority() { return PRIORITY_DATABASE; diff --git a/src/main/resources/META-INF/services/liquibase.executor.Executor b/src/main/resources/META-INF/services/liquibase.executor.Executor deleted file mode 100644 index 2554c417..00000000 --- a/src/main/resources/META-INF/services/liquibase.executor.Executor +++ /dev/null @@ -1 +0,0 @@ -liquibase.ext.bigquery.executor.BigQueryExecutor \ No newline at end of file diff --git a/src/test/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChangeTest.java b/src/test/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChangeTest.java index d7e18490..e9d3effc 100644 --- a/src/test/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChangeTest.java +++ b/src/test/java/liquibase/ext/bigquery/change/BigQueryMergeColumnChangeTest.java @@ -4,7 +4,7 @@ import liquibase.statement.SqlStatement; import liquibase.statement.core.AddColumnStatement; import liquibase.statement.core.DropColumnStatement; -import liquibase.statement.core.RawSqlStatement; +import liquibase.statement.core.RawParameterizedSqlStatement; import org.junit.Before; import org.junit.Test; @@ -35,7 +35,7 @@ public void generateStatements() { SqlStatement[] sqlStatements = change.generateStatements(database); assertEquals(4, sqlStatements.length); assertTrue(sqlStatements[0] instanceof AddColumnStatement); - assertTrue(sqlStatements[1] instanceof RawSqlStatement); + assertTrue(sqlStatements[1] instanceof RawParameterizedSqlStatement); assertTrue(sqlStatements[2] instanceof DropColumnStatement); assertTrue(sqlStatements[3] instanceof DropColumnStatement); @@ -43,8 +43,8 @@ public void generateStatements() { assertEquals("finalColumnName", addColumnStatement.getColumnName()); assertEquals("finalColumnName", addColumnStatement.getColumnType()); - RawSqlStatement rawSqlStatement = (RawSqlStatement) sqlStatements[1]; - assertEquals("UPDATE schemaName.tableName SET finalColumnName = column1Name || 'joinString' || column2Name WHERE 1 = 1 ", rawSqlStatement.getSql()); + RawParameterizedSqlStatement sqlStatement = (RawParameterizedSqlStatement) sqlStatements[1]; + assertEquals("UPDATE schemaName.tableName SET finalColumnName = column1Name || 'joinString' || column2Name WHERE 1 = 1 ", sqlStatement.getSql()); DropColumnStatement drop1ColumnStatement = (DropColumnStatement) sqlStatements[2]; assertEquals("column1Name", drop1ColumnStatement.getColumnName());