From 993bcd2d07d07a1565cc495f49f767bc82ed19ff Mon Sep 17 00:00:00 2001 From: Femi3211 Date: Thu, 22 Jun 2023 12:07:32 +0200 Subject: [PATCH 01/11] Added: Templating for PostgresDDLGenerator --- .../postgres/PostgresDDLGenerator.java | 237 ++++++++++-------- .../templates/postgres/column/add.sqlt | 1 + .../postgres/column/alter_column_null.sqlt | 1 + .../postgres/column/alter_column_type.sqlt | 1 + .../templates/postgres/column/drop.sqlt | 1 + .../templates/postgres/foreignkey/create.sqlt | 1 + .../templates/postgres/foreignkey/drop.sqlt | 1 + .../templates/postgres/schema/create.sqlt | 1 + .../postgres/table/alter_add_primary_key.sqlt | 1 + .../table/alter_drop_primary_key.sqlt | 1 + .../templates/postgres/table/create.sqlt | 1 + .../templates/postgres/table/drop.sqlt | 1 + 12 files changed, 144 insertions(+), 104 deletions(-) create mode 100644 ddl/src/main/resources/templates/postgres/column/add.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/column/drop.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/schema/create.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/create.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/drop.sqlt diff --git a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java index 03f823ab..2f91d436 100644 --- a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java +++ b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java @@ -10,13 +10,11 @@ import com.adaptivescale.rosetta.ddl.change.model.ColumnChange; import com.adaptivescale.rosetta.ddl.change.model.ForeignKeyChange; import com.adaptivescale.rosetta.ddl.targets.ColumnSQLDecoratorFactory; +import com.adaptivescale.rosetta.ddl.utils.TemplateEngine; import lombok.extern.slf4j.Slf4j; import java.sql.DatabaseMetaData; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import static com.adaptivescale.rosetta.ddl.targets.postgres.Constants.DEFAULT_WRAPPER; @@ -28,6 +26,30 @@ ) public class PostgresDDLGenerator implements DDL { + private final static String TABLE_CREATE_TEMPLATE = "postgres/table/create"; + + private final static String TABLE_ALTER_TEMPLATE = "postgres/table/alter"; + + private final static String TABLE_ALTER_DROP_PRIMARY_KEY_TEMPLATE = "postgres/table/alter_drop_primary_key"; + + private final static String TABLE_ALTER_ADD_PRIMARY_KEY_TEMPLATE = "postgres/table/alter_add_primary_key"; + + private final static String TABLE_DROP_TEMPLATE = "postgres/table/drop"; + + private final static String SCHEMA_CREATE_TEMPLATE = "postgres/schema/create"; + + private final static String FOREIGN_KEY_CREATE_TEMPLATE = "postgres/foreignkey/create"; + + private final static String FOREIGN_KEY_DROP_TEMPLATE = "postgres/foreignkey/drop"; + + private final static String COLUMN_ADD_TEMPLATE = "postgres/column/add"; + + private final static String COLUMN_ALTER_TYPE_TEMPLATE = "postgres/column/alter_column_type"; + + private final static String COLUMN_ALTER_NULL_TEMPLATE = "postgres/column/alter_column_null"; + + private final static String COLUMN_DROP_TEMPLATE = "postgres/column/drop"; + private final ColumnSQLDecoratorFactory columnSQLDecoratorFactory = new PostgresColumnDecoratorFactory(); @@ -38,6 +60,8 @@ public String createColumn(Column column) { @Override public String createTable(Table table, boolean dropTableIfExists) { + Map createParams = new HashMap<>(); + List definitions = table.getColumns().stream().map(this::createColumn).collect(Collectors.toList()); Optional primaryKeysForTable = createPrimaryKeysForTable(table); @@ -46,36 +70,22 @@ public String createTable(Table table, boolean dropTableIfExists) { StringBuilder stringBuilder = new StringBuilder(); if (dropTableIfExists) { - stringBuilder.append("DROP TABLE IF EXISTS "); - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder.append("`").append(table.getSchema()).append("`."); - } - stringBuilder.append(DEFAULT_WRAPPER).append(table.getName()).append(DEFAULT_WRAPPER).append("; \n"); + stringBuilder.append(dropTable(table)); } - stringBuilder.append("CREATE TABLE "); - - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder.append(DEFAULT_WRAPPER) - .append(table.getSchema()).append(DEFAULT_WRAPPER).append("."); - } + createParams.put("schemaName", table.getSchema()); + createParams.put("tableName", table.getName()); + createParams.put("tableCode", definitionAsString); + stringBuilder.append(TemplateEngine.process(TABLE_CREATE_TEMPLATE, createParams)); - stringBuilder.append(DEFAULT_WRAPPER).append(table.getName()).append(DEFAULT_WRAPPER).append("(").append(definitionAsString).append(");"); return stringBuilder.toString(); } @Override public String createTableSchema(Table table) { - StringBuilder stringBuilder = new StringBuilder(); - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder - .append("CREATE SCHEMA IF NOT EXISTS ") - .append(DEFAULT_WRAPPER) - .append(table.getSchema()) - .append(DEFAULT_WRAPPER) - .append(";"); - } - return stringBuilder.toString(); + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + return TemplateEngine.process(SCHEMA_CREATE_TEMPLATE, params); } @Override @@ -85,71 +95,73 @@ public String createDatabase(Database database, boolean dropTableIfExists) { Set schemas = database.getTables().stream().map(Table::getSchema).filter(s -> s != null && !s.isEmpty()).collect(Collectors.toSet()); if (!schemas.isEmpty()) { stringBuilder.append( - schemas - .stream() - .map(schema -> "CREATE SCHEMA IF NOT EXISTS " + DEFAULT_WRAPPER + schema + DEFAULT_WRAPPER) - .collect(Collectors.joining(";\r\r")) - + schemas + .stream() + .map(this::createSchema) + .collect(Collectors.joining()) ); - stringBuilder.append(";\r"); + stringBuilder.append("\r"); } stringBuilder.append(database.getTables() - .stream() - .map(table -> createTable(table, dropTableIfExists)) - .collect(Collectors.joining("\r\r"))); - - String foreignKeys = database - .getTables() - .stream() - .map(this::foreignKeys) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.joining()); - - if (!foreignKeys.isEmpty()) { - stringBuilder.append("\r").append(foreignKeys).append("\r"); - } + .stream() + .map(table -> createTable(table, dropTableIfExists)) + .collect(Collectors.joining("\r\r"))); + + stringBuilder.append("\r"); + + //Create ForeignKeys + stringBuilder.append(database.getTables() + .stream() + .map(table -> foreignKeys(table)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.joining("\r"))); + return stringBuilder.toString(); } @Override public String createForeignKey(ForeignKey foreignKey) { - return "ALTER TABLE" + handleNullSchema(foreignKey.getSchema(), foreignKey.getTableName()) + " ADD CONSTRAINT " - + foreignKey.getName() + " FOREIGN KEY ("+ DEFAULT_WRAPPER + foreignKey.getColumnName() + DEFAULT_WRAPPER +") REFERENCES " - + handleNullSchema(foreignKey.getPrimaryTableSchema(), foreignKey.getPrimaryTableName()) - + "("+ DEFAULT_WRAPPER + foreignKey.getPrimaryColumnName()+ DEFAULT_WRAPPER + ")" - + foreignKeyDeleteRuleSanitation(foreignKeyDeleteRule(foreignKey)) + ";\r"; + Map params = new HashMap<>(); + params.put("schemaName", foreignKey.getSchema()); + params.put("tableName", foreignKey.getTableName()); + params.put("foreignkeyColumn", foreignKey.getColumnName()); + params.put("primaryTableSchema", foreignKey.getPrimaryTableSchema()); + params.put("primaryTableName", foreignKey.getPrimaryTableName()); + params.put("foreignKeyPrimaryColumnName", foreignKey.getPrimaryColumnName()); + params.put("foreignkeyName", foreignKey.getName()); + return TemplateEngine.process(FOREIGN_KEY_CREATE_TEMPLATE, params); } @Override public String alterColumn(ColumnChange change) { - Table table = change.getTable(); Column actual = change.getActual(); Column expected = change.getExpected(); + Map params = new HashMap<>(); + params.put("schemaName", change.getTable().getSchema()); + params.put("tableName", change.getTable().getName()); + params.put("columnName", expected.getName()); + + if (!Objects.equals(expected.getTypeName(), actual.getTypeName())) { + params.put("dataType", expected.getTypeName()); + return TemplateEngine.process(COLUMN_ALTER_TYPE_TEMPLATE, params); + } - if (!Objects.equals(expected.getTypeName(), actual.getTypeName()) - || !Objects.equals(expected.isNullable(), actual.isNullable())) { - String alterColumnString = columnSQLDecoratorFactory.decoratorFor(expected).expressSQl(); - String formattedAlterColumn = String.format("%s TYPE %s", alterColumnString.split(" ")[0], alterColumnString.split(" ")[1]); - - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("ALTER TABLE"); - stringBuilder.append(handleNullSchema(table.getSchema(), table.getName())); - stringBuilder.append(" ALTER COLUMN "); - stringBuilder.append(formattedAlterColumn); - if(expected.isNullable()){ - stringBuilder.append(", ALTER COLUMN "); - stringBuilder.append(expected.getName()); - stringBuilder.append(" DROP NOT NULL"); + if (!Objects.equals(expected.isNullable(), actual.isNullable())) { + if (expected.isNullable()) { + params.put("nullDefinition", "DROP NOT NULL"); + return TemplateEngine.process(COLUMN_ALTER_NULL_TEMPLATE, params); + } else { + params.put("nullDefinition", "SET NOT NULL"); + return TemplateEngine.process(COLUMN_ALTER_NULL_TEMPLATE, params); } - stringBuilder.append(";"); - return stringBuilder.toString(); } log.info("No action taken for changes detected in column: {}.{}.{}", change.getTable().getSchema(), change.getTable().getName(), expected.getName()); + return ""; } @@ -158,35 +170,47 @@ public String dropColumn(ColumnChange change) { Table table = change.getTable(); Column actual = change.getActual(); - return "ALTER TABLE" + - handleNullSchema(table.getSchema(), table.getName()) + " DROP COLUMN "+ DEFAULT_WRAPPER + - actual.getName() + DEFAULT_WRAPPER +";"; + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + params.put("tableName", table.getName()); + params.put("columnName", actual.getName()); + return TemplateEngine.process(COLUMN_DROP_TEMPLATE, params); } + + @Override public String addColumn(ColumnChange change) { Table table = change.getTable(); Column expected = change.getExpected(); - return "ALTER TABLE" + - handleNullSchema(table.getSchema(), table.getName()) + - " ADD COLUMN " + - columnSQLDecoratorFactory.decoratorFor(expected).expressSQl() + ";"; + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + params.put("tableName", table.getName()); + params.put("columnDefinition", columnSQLDecoratorFactory.decoratorFor(expected).expressSQl()); + return TemplateEngine.process(COLUMN_ADD_TEMPLATE, params); } @Override public String dropTable(Table actual) { - return "DROP TABLE" + handleNullSchema(actual.getSchema(), actual.getName()) + ";"; + Map params = new HashMap<>(); + params.put("schemaName", actual.getSchema()); + params.put("tableName", actual.getName()); + return TemplateEngine.process(TABLE_DROP_TEMPLATE, params); } @Override public String alterForeignKey(ForeignKeyChange change) { - return ""; + return dropForeignKey(change.getActual()) + "\r" + createForeignKey(change.getExpected()); } @Override public String dropForeignKey(ForeignKey actual) { - return "ALTER TABLE" + handleNullSchema(actual.getSchema(), actual.getTableName()) + " DROP CONSTRAINT " + DEFAULT_WRAPPER + actual.getName() + DEFAULT_WRAPPER + ";"; + Map params = new HashMap<>(); + params.put("schemaName", actual.getSchema()); + params.put("tableName", actual.getTableName()); + params.put("foreignkeyName", actual.getName()); + return TemplateEngine.process(FOREIGN_KEY_DROP_TEMPLATE, params); } @Override @@ -194,24 +218,29 @@ public String alterTable(Table expected, Table actual) { boolean doesPKExist = actual.getColumns().stream().map(Column::isPrimaryKey).reduce((aBoolean, aBoolean2) -> aBoolean || aBoolean2).orElse(false); boolean doWeNeedToCreatePk = expected.getColumns().stream().map(Column::isPrimaryKey).reduce((aBoolean, aBoolean2) -> aBoolean || aBoolean2).orElse(false); - StringBuilder stringBuilder = new StringBuilder("ALTER TABLE") - .append(handleNullSchema(expected.getSchema(), expected.getName())); + Map params = new HashMap<>(); + StringBuilder stringBuilder = new StringBuilder(); if (doesPKExist) { - stringBuilder.append(" DROP PRIMARY KEY"); + params.put("schemaName", expected.getSchema()); + params.put("tableName", tableNameWithSchema(expected)); + stringBuilder.append( + TemplateEngine.process(TABLE_ALTER_DROP_PRIMARY_KEY_TEMPLATE, params) + ); } if (doWeNeedToCreatePk) { Optional primaryKeysForTable = createPrimaryKeysForTable(expected); if (primaryKeysForTable.isPresent()) { - if (doesPKExist) { - stringBuilder.append(","); - } - stringBuilder.append(" ADD ").append(primaryKeysForTable.get()); + params.put("schemaName", expected.getSchema()); + params.put("tableName", tableNameWithSchema(expected)); + params.put("primaryKeyDefinition", primaryKeysForTable.get()); + stringBuilder.append( + TemplateEngine.process(TABLE_ALTER_ADD_PRIMARY_KEY_TEMPLATE, params) + ); } } - stringBuilder.append(";"); return stringBuilder.toString(); } @@ -221,7 +250,8 @@ private Optional createPrimaryKeysForTable(Table table) { .stream() .filter(Column::isPrimaryKey) .sorted((o1, o2) -> o1.getPrimaryKeySequenceId() < o2.getPrimaryKeySequenceId() ? -1 : 1) - .map(pk -> String.format(DEFAULT_WRAPPER+"%s"+DEFAULT_WRAPPER, pk.getName())) + .map(Column::getName) + .map(it -> "\"" + it + "\"") .collect(Collectors.toList()); if (primaryKeys.isEmpty()) { @@ -239,25 +269,13 @@ private Optional foreignKeys(Table table) { return result.isEmpty() ? Optional.empty() : Optional.of(result); } - //ALTER TABLE rosetta.contacts ADD CONSTRAINT contacts_fk FOREIGN KEY (contact_id) REFERENCES rosetta."user"(user_id); private String createForeignKeys(Column column) { return column.getForeignKeys().stream().map(this::createForeignKey).collect(Collectors.joining()); } - private String handleNullSchema(String schema, String tableName) { - return ((schema == null || schema.isEmpty()) ? " " : (" "+ DEFAULT_WRAPPER + schema + DEFAULT_WRAPPER +".")) + DEFAULT_WRAPPER + tableName + DEFAULT_WRAPPER; - } - - private String foreignKeyDeleteRuleSanitation(String deleteRule) { - if (deleteRule == null || deleteRule.isEmpty()) { - return ""; - } - return " " + deleteRule + " "; - } - - private String foreignKeyDeleteRule(ForeignKey foreignKey) { + protected String foreignKeyDeleteRule(ForeignKey foreignKey) { if (foreignKey.getDeleteRule() == null || foreignKey.getDeleteRule().isEmpty()) { - return ""; + return null; } switch (Integer.parseInt(foreignKey.getDeleteRule())) { case DatabaseMetaData.importedKeyCascade: @@ -271,8 +289,19 @@ private String foreignKeyDeleteRule(ForeignKey foreignKey) { case DatabaseMetaData.importedKeyInitiallyImmediate: case DatabaseMetaData.importedKeyNotDeferrable: default: - //todo add warn log - return ""; + return null; } } + + private String tableNameWithSchema(Table table) { + StringBuilder builder = new StringBuilder(); + builder.append(table.getName()); + return builder.toString(); + } + + private String createSchema(String schema) { + Map params = new HashMap<>(); + params.put("schemaName", schema); + return TemplateEngine.process(SCHEMA_CREATE_TEMPLATE, params); + } } diff --git a/ddl/src/main/resources/templates/postgres/column/add.sqlt b/ddl/src/main/resources/templates/postgres/column/add.sqlt new file mode 100644 index 00000000..493f0b0b --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/add.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD [(${columnDefinition})]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt b/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt new file mode 100644 index 00000000..d67eb74f --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" [(${nullDefinition})]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt b/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt new file mode 100644 index 00000000..442d7e6a --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" SET DATA TYPE [(${dataType})]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/drop.sqlt b/ddl/src/main/resources/templates/postgres/column/drop.sqlt new file mode 100644 index 00000000..c2a179d9 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/drop.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP COLUMN "[(${columnName})]"; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt b/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt new file mode 100644 index 00000000..92e802c0 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD CONSTRAINT "[(${foreignkeyName})]" FOREIGN KEY ("[(${foreignkeyColumn})]") REFERENCES "[(${schemaName})]"."[(${primaryTableName})]"("[(${foreignKeyPrimaryColumnName})]") [(${deleteRule})]; diff --git a/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt b/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt new file mode 100644 index 00000000..efb4b7c3 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${foreignkeyName})]"; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/schema/create.sqlt b/ddl/src/main/resources/templates/postgres/schema/create.sqlt new file mode 100644 index 00000000..00575db1 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/schema/create.sqlt @@ -0,0 +1 @@ +CREATE SCHEMA IF NOT EXISTS [[(${schemaName})]]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt b/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt new file mode 100644 index 00000000..c6aa48fb --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD CONSTRAINT[(${primaryKeyDefinition})]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt b/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt new file mode 100644 index 00000000..01cfa645 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt @@ -0,0 +1 @@ +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${tableName})]_pkey"; diff --git a/ddl/src/main/resources/templates/postgres/table/create.sqlt b/ddl/src/main/resources/templates/postgres/table/create.sqlt new file mode 100644 index 00000000..2d5414c2 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/create.sqlt @@ -0,0 +1 @@ +CREATE TABLE "[(${schemaName})]"."[(${tableName})]"([(${tableCode})]); \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/drop.sqlt b/ddl/src/main/resources/templates/postgres/table/drop.sqlt new file mode 100644 index 00000000..fb536f5b --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/drop.sqlt @@ -0,0 +1 @@ +DROP TABLE IF EXISTS "[(${schemaName})]"."[(${tableName})]"; \ No newline at end of file From de7b5a5ac1a5c8ec624ae6cab035dc68b0dfa24e Mon Sep 17 00:00:00 2001 From: nbesimi Date: Tue, 4 Jul 2023 13:01:34 +0200 Subject: [PATCH 02/11] fixed: tests added: conditional sql template generators --- .../postgres/PostgresDDLGenerator.java | 1 + .../templates/postgres/column/add.sqlt | 7 +- .../postgres/column/alter_column_null.sqlt | 7 +- .../postgres/column/alter_column_type.sqlt | 7 +- .../templates/postgres/column/drop.sqlt | 7 +- .../templates/postgres/foreignkey/create.sqlt | 7 +- .../templates/postgres/foreignkey/drop.sqlt | 7 +- .../templates/postgres/schema/create.sqlt | 4 +- .../postgres/table/alter_add_primary_key.sqlt | 7 +- .../table/alter_drop_primary_key.sqlt | 5 + .../templates/postgres/table/create.sqlt | 7 +- .../templates/postgres/table/drop.sqlt | 7 +- .../rosetta/ddl/test/PostgresDDLTest.java | 92 +++++++++---------- 13 files changed, 107 insertions(+), 58 deletions(-) diff --git a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java index 2f91d436..e677b3d0 100644 --- a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java +++ b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java @@ -131,6 +131,7 @@ public String createForeignKey(ForeignKey foreignKey) { params.put("primaryTableName", foreignKey.getPrimaryTableName()); params.put("foreignKeyPrimaryColumnName", foreignKey.getPrimaryColumnName()); params.put("foreignkeyName", foreignKey.getName()); + params.put("deleteRule", foreignKeyDeleteRule(foreignKey)); return TemplateEngine.process(FOREIGN_KEY_CREATE_TEMPLATE, params); } diff --git a/ddl/src/main/resources/templates/postgres/column/add.sqlt b/ddl/src/main/resources/templates/postgres/column/add.sqlt index 493f0b0b..f02237a4 100644 --- a/ddl/src/main/resources/templates/postgres/column/add.sqlt +++ b/ddl/src/main/resources/templates/postgres/column/add.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD [(${columnDefinition})]; \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ADD COLUMN [(${columnDefinition})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD COLUMN [(${columnDefinition})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt b/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt index d67eb74f..c2d7ce3c 100644 --- a/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt +++ b/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" [(${nullDefinition})]; \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ALTER COLUMN "[(${columnName})]" [(${nullDefinition})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" [(${nullDefinition})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt b/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt index 442d7e6a..0f70f55d 100644 --- a/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt +++ b/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" SET DATA TYPE [(${dataType})]; \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ALTER COLUMN "[(${columnName})]" SET DATA TYPE [(${dataType})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" SET DATA TYPE [(${dataType})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/drop.sqlt b/ddl/src/main/resources/templates/postgres/column/drop.sqlt index c2a179d9..4c3fcd9e 100644 --- a/ddl/src/main/resources/templates/postgres/column/drop.sqlt +++ b/ddl/src/main/resources/templates/postgres/column/drop.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP COLUMN "[(${columnName})]"; \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" DROP COLUMN "[(${columnName})]"; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP COLUMN "[(${columnName})]"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt b/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt index 92e802c0..e01031d5 100644 --- a/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt +++ b/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD CONSTRAINT "[(${foreignkeyName})]" FOREIGN KEY ("[(${foreignkeyColumn})]") REFERENCES "[(${schemaName})]"."[(${primaryTableName})]"("[(${foreignKeyPrimaryColumnName})]") [(${deleteRule})]; +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD CONSTRAINT [(${foreignkeyName})] FOREIGN KEY ("[(${foreignkeyColumn})]") REFERENCES "[(${schemaName})]"."[(${primaryTableName})]"("[(${foreignKeyPrimaryColumnName})]") [(${deleteRule})]; +[/] +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ADD CONSTRAINT [(${foreignkeyName})] FOREIGN KEY ("[(${foreignkeyColumn})]") REFERENCES "[(${primaryTableName})]"("[(${foreignKeyPrimaryColumnName})]") [(${deleteRule})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt b/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt index efb4b7c3..6e8fa92f 100644 --- a/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt +++ b/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${foreignkeyName})]"; \ No newline at end of file +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${foreignkeyName})]"; +[/] +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" DROP CONSTRAINT "[(${foreignkeyName})]"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/schema/create.sqlt b/ddl/src/main/resources/templates/postgres/schema/create.sqlt index 00575db1..439f3627 100644 --- a/ddl/src/main/resources/templates/postgres/schema/create.sqlt +++ b/ddl/src/main/resources/templates/postgres/schema/create.sqlt @@ -1 +1,3 @@ -CREATE SCHEMA IF NOT EXISTS [[(${schemaName})]]; \ No newline at end of file +[# th:if="${schemaName} != null and ${schemaName} != ''"] +CREATE SCHEMA IF NOT EXISTS "[[(${schemaName})]]"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt b/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt index c6aa48fb..ad2d0504 100644 --- a/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt +++ b/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt @@ -1 +1,6 @@ -ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD CONSTRAINT[(${primaryKeyDefinition})]; \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ADD [(${primaryKeyDefinition})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD [(${primaryKeyDefinition})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt b/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt index 01cfa645..6103b4a0 100644 --- a/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt +++ b/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt @@ -1 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" DROP CONSTRAINT "[(${tableName})]_pkey"; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${tableName})]_pkey"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/create.sqlt b/ddl/src/main/resources/templates/postgres/table/create.sqlt index 2d5414c2..5e961a09 100644 --- a/ddl/src/main/resources/templates/postgres/table/create.sqlt +++ b/ddl/src/main/resources/templates/postgres/table/create.sqlt @@ -1 +1,6 @@ -CREATE TABLE "[(${schemaName})]"."[(${tableName})]"([(${tableCode})]); \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +CREATE TABLE "[(${tableName})]"([(${tableCode})]); +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +CREATE TABLE "[(${schemaName})]"."[(${tableName})]"([(${tableCode})]); +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/drop.sqlt b/ddl/src/main/resources/templates/postgres/table/drop.sqlt index fb536f5b..d0526323 100644 --- a/ddl/src/main/resources/templates/postgres/table/drop.sqlt +++ b/ddl/src/main/resources/templates/postgres/table/drop.sqlt @@ -1 +1,6 @@ -DROP TABLE IF EXISTS "[(${schemaName})]"."[(${tableName})]"; \ No newline at end of file +[# th:if="${schemaName} == null or ${schemaName} == ''"] +DROP TABLE IF EXISTS "[(${tableName})]"; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +DROP TABLE IF EXISTS "[(${schemaName})]"."[(${tableName})]"; +[/] \ No newline at end of file diff --git a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java index b94066a2..153ee7a5 100644 --- a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java +++ b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java @@ -2,10 +2,8 @@ import com.adaptivescale.rosetta.common.models.Database; import com.adaptivescale.rosetta.ddl.change.*; -import com.adaptivescale.rosetta.ddl.change.comparator.MysqlForeignKeyChangeComparator; import com.adaptivescale.rosetta.ddl.change.comparator.PostgresForeignKeyChangeComparator; import com.adaptivescale.rosetta.ddl.change.model.Change; -import com.adaptivescale.rosetta.ddl.targets.mysql.MySqlDDLGenerator; import com.adaptivescale.rosetta.ddl.targets.postgres.PostgresDDLGenerator; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -22,163 +20,161 @@ public class PostgresDDLTest { @Test public void createDB() throws IOException { String ddl = generateDDL("clean_database"); - Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"ROSETTA\";\r" + - "CREATE TABLE \"ROSETTA\".\"PLAYER\"(\"Name\" VARCHAR(100), \"Position\" VARCHAR(100), \"Number\" NUMBER NOT NULL );\r" + + Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"ROSETTA\";\n" + + "CREATE TABLE \"ROSETTA\".\"PLAYER\"(\"Name\" VARCHAR(100), \"Position\" VARCHAR(100), \"Number\" NUMBER NOT NULL );\n" + "\r" + - "CREATE TABLE \"ROSETTA\".\"USER\"(\"USER_ID\" NUMBER NOT NULL , PRIMARY KEY (\"USER_ID\"));", ddl); + "CREATE TABLE \"ROSETTA\".\"USER\"(\"USER_ID\" NUMBER NOT NULL , PRIMARY KEY (\"USER_ID\"));\n" + + "\r", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addTable() throws IOException { String ddl = generateDDL("add_table"); - Assertions.assertEquals("\rCREATE TABLE \"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\r" + - "\rCREATE TABLE \"Position2\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\r" + - "ALTER TABLE \"Position\" ADD CONSTRAINT Position_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\");\r" + - "\r" + - "ALTER TABLE \"Position2\" ADD CONSTRAINT Position2_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\");\r", ddl); + Assertions.assertEquals("CREATE TABLE \"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\n" + + "\rCREATE TABLE \"Position2\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\n" + + "ALTER TABLE \"Position\" ADD CONSTRAINT Position_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\") ;\n" + + "ALTER TABLE \"Position2\" ADD CONSTRAINT Position2_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addTable2() throws IOException { String ddl = generateDDL("add_table2"); - Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"new\";\r" + - "CREATE TABLE \"new\".\"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR," + - " \"Name\" VARCHAR, PRIMARY KEY (\"ID\"));", ddl); + Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"new\";\n" + + "CREATE TABLE \"new\".\"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, PRIMARY KEY (\"ID\"));\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropTable() throws IOException { String ddl = generateDDL("drop_table"); - Assertions.assertEquals("DROP TABLE \"TEAMPLAYERS\";", ddl); + Assertions.assertEquals("DROP TABLE IF EXISTS \"TEAMPLAYERS\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addColumn() throws IOException { String ddl = generateDDL("add_column"); - Assertions.assertEquals("ALTER TABLE \"Position\" ADD COLUMN \"DESCRIPTION\" varchar(100);", ddl); + Assertions.assertEquals("ALTER TABLE \"Position\" ADD COLUMN \"DESCRIPTION\" varchar(100);\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addColumnWithForeignKey() throws IOException { String ddl = generateDDL("add_column_with_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"POSITION_ID\" numeric;\r" + - "ALTER TABLE \"FBAL\".\"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"FBAL\".\"Position\"(\"ID\") ON DELETE NO ACTION ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"POSITION_ID\" numeric;\n" + + "ALTER TABLE \"FBAL\".\"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"FBAL\".\"Position\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addColumnAsPrimaryKey() throws IOException { String ddl = generateDDL("add_column_as_primary_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"ID\" numeric NOT NULL ;\r" + - "ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"ID\" numeric NOT NULL ;\n" + + "ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropColumn() throws IOException { String ddl = generateDDL("drop_column"); - Assertions.assertEquals("ALTER TABLE \"Position\" DROP COLUMN \"DESCRIPTION\";", ddl); + Assertions.assertEquals("ALTER TABLE \"Position\" DROP COLUMN \"DESCRIPTION\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterColumnDataType() throws IOException { String ddl = generateDDL("alter_column_data_type"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"name\" TYPE INTEGER, ALTER COLUMN name DROP NOT NULL;", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"name\" SET DATA TYPE INTEGER;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterColumnToNullable() throws IOException { String ddl = generateDDL("alter_column_to_nullable"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" TYPE numeric, ALTER COLUMN ID DROP NOT NULL;", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" DROP NOT NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterColumnToNotNullable() throws IOException { String ddl = generateDDL("alter_column_to_not_nullable"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" TYPE numeric;", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" SET NOT NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropColumnWithForeignKey() throws IOException { String ddl = generateDDL("drop_column_with_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"FBAL\".\"PLAYER\" DROP CONSTRAINT \"PLAYER_FK\";\r" + - "ALTER TABLE \"FBAL\".\"PLAYER\" DROP COLUMN \"POSITION_ID\";", ddl); + Assertions.assertEquals("ALTER TABLE \"FBAL\".\"PLAYER\" DROP CONSTRAINT \"PLAYER_FK\";\n" + + "ALTER TABLE \"FBAL\".\"PLAYER\" DROP COLUMN \"POSITION_ID\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropColumnWithPrimaryKeyReferenced() throws IOException { String ddl = generateDDL("drop_column_with_primary_key_referenced"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropTableWhereColumnIsReferenced() throws IOException { String ddl = generateDDL("drop_table_where_column_is_referenced"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK_TEAM\";\r" + - "DROP TABLE \"TEAM\";", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK_TEAM\";\n" + + "DROP TABLE IF EXISTS \"TEAM\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addForeignKey() throws IOException { String ddl = generateDDL("add_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") " + - "REFERENCES \"Position\"(\"ID\") ON DELETE NO ACTION ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"Position\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropForeignKey() throws IOException { String ddl = generateDDL("drop_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropPrimaryKey() throws IOException { String ddl = generateDDL("drop_primary_key"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"PLAYER\" DROP PRIMARY KEY;", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"PLAYER\" DROP CONSTRAINT \"PLAYER_pkey\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addPrimaryKey() throws IOException { String ddl = generateDDL("add_primary_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterPrimaryKey() throws IOException { String ddl = generateDDL("alter_primary_key"); - Assertions.assertEquals("\r" + - "ALTER TABLE \"PLAYER\" DROP PRIMARY KEY, ADD PRIMARY KEY (\"ID\", \"POSITION_ID\");\r", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" DROP CONSTRAINT \"PLAYER_pkey\";\n" + + "ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\", \"POSITION_ID\");\n" + + "\r", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterForeignKeyName() throws IOException { String ddl = generateDDL("alter_foreign_key_name"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_CHANGED_FK FOREIGN KEY (\"PLAYERID\") REFERENCES " + - "\"PLAYER\"(\"ID\") ON DELETE NO ACTION ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_CHANGED_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"PLAYER\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterForeignKey() throws IOException { String ddl = generateDDL("alter_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"PLAYER\"(\"ID\") ON DELETE SET NULL ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"PLAYER\"(\"ID\") ON DELETE SET NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropPrimaryKeyColumnAndAlterForeignKey() throws IOException { String ddl = generateDDL("drop_pk_column_and_alter_fk"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\");\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropTableWithPrimaryKeyColumnAndAlterForeignKey() throws IOException { String ddl = generateDDL("drop_table_with_pk_column_and_alter_fk"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "DROP TABLE \"PLAYER\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\");\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "DROP TABLE IF EXISTS \"PLAYER\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } private String generateDDL(String testType) throws IOException { From 06faf2db4b50f94272ec9e4b9b8de4d63ddd1de0 Mon Sep 17 00:00:00 2001 From: nbesimi Date: Tue, 4 Jul 2023 15:20:06 +0200 Subject: [PATCH 03/11] added: overwriteType to handle custom types or types that cannot be translated --- .../com/adaptivescale/rosetta/common/models/Column.java | 9 +++++++++ .../rosetta/translator/DefaultTranslator.java | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java b/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java index f1f980ab..ae9ed78f 100644 --- a/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java +++ b/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java @@ -10,6 +10,7 @@ public class Column { private String label; private String description; private String typeName; + private String overwriteType; private int ordinalPosition; private boolean isAutoincrement; private boolean isNullable; @@ -57,6 +58,14 @@ public void setTypeName(String typeName) { this.typeName = typeName; } + public String getOverwriteType() { + return overwriteType; + } + + public void setOverwriteType(String overwriteType) { + this.overwriteType = overwriteType; + } + public int getOrdinalPosition() { return ordinalPosition; } diff --git a/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java b/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java index 0aa486ba..4983c5f4 100644 --- a/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java +++ b/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java @@ -42,7 +42,11 @@ private Table translateTable(Table table) { private Column translateColumn(Column column) { - TranslationModel translationModel = TranslationMatrix.getInstance().findBySourceTypeAndSourceColumnTypeAndTargetType(sourceDatabaseName, column.getTypeName(), targetDatabaseName); + String columnType = column.getOverwriteType(); + if (columnType == null) { + columnType = column.getTypeName(); + } + TranslationModel translationModel = TranslationMatrix.getInstance().findBySourceTypeAndSourceColumnTypeAndTargetType(sourceDatabaseName, columnType, targetDatabaseName); if (translationModel == null) { throw new RuntimeException("There is no match for column name: " + column.getName() + " and type: " + column.getTypeName() + "."); From 34678189b4d7469200a6cbf2a877ded02ab2f54d Mon Sep 17 00:00:00 2001 From: Fehmi Havziu <106117501+Femi3211@users.noreply.github.com> Date: Thu, 6 Jul 2023 13:49:54 +0200 Subject: [PATCH 04/11] Templating for PostgresDDLGenerator (#187) * Added: Templating for PostgresDDLGenerator * fixed: tests added: conditional sql template generators --------- Co-authored-by: nbesimi --- .../postgres/PostgresDDLGenerator.java | 238 ++++++++++-------- .../templates/postgres/column/add.sqlt | 6 + .../postgres/column/alter_column_null.sqlt | 6 + .../postgres/column/alter_column_type.sqlt | 6 + .../templates/postgres/column/drop.sqlt | 6 + .../templates/postgres/foreignkey/create.sqlt | 6 + .../templates/postgres/foreignkey/drop.sqlt | 6 + .../templates/postgres/schema/create.sqlt | 3 + .../postgres/table/alter_add_primary_key.sqlt | 6 + .../table/alter_drop_primary_key.sqlt | 6 + .../templates/postgres/table/create.sqlt | 6 + .../templates/postgres/table/drop.sqlt | 6 + .../rosetta/ddl/test/PostgresDDLTest.java | 92 ++++--- 13 files changed, 241 insertions(+), 152 deletions(-) create mode 100644 ddl/src/main/resources/templates/postgres/column/add.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/column/drop.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/schema/create.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/create.sqlt create mode 100644 ddl/src/main/resources/templates/postgres/table/drop.sqlt diff --git a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java index 03f823ab..e677b3d0 100644 --- a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java +++ b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/postgres/PostgresDDLGenerator.java @@ -10,13 +10,11 @@ import com.adaptivescale.rosetta.ddl.change.model.ColumnChange; import com.adaptivescale.rosetta.ddl.change.model.ForeignKeyChange; import com.adaptivescale.rosetta.ddl.targets.ColumnSQLDecoratorFactory; +import com.adaptivescale.rosetta.ddl.utils.TemplateEngine; import lombok.extern.slf4j.Slf4j; import java.sql.DatabaseMetaData; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import static com.adaptivescale.rosetta.ddl.targets.postgres.Constants.DEFAULT_WRAPPER; @@ -28,6 +26,30 @@ ) public class PostgresDDLGenerator implements DDL { + private final static String TABLE_CREATE_TEMPLATE = "postgres/table/create"; + + private final static String TABLE_ALTER_TEMPLATE = "postgres/table/alter"; + + private final static String TABLE_ALTER_DROP_PRIMARY_KEY_TEMPLATE = "postgres/table/alter_drop_primary_key"; + + private final static String TABLE_ALTER_ADD_PRIMARY_KEY_TEMPLATE = "postgres/table/alter_add_primary_key"; + + private final static String TABLE_DROP_TEMPLATE = "postgres/table/drop"; + + private final static String SCHEMA_CREATE_TEMPLATE = "postgres/schema/create"; + + private final static String FOREIGN_KEY_CREATE_TEMPLATE = "postgres/foreignkey/create"; + + private final static String FOREIGN_KEY_DROP_TEMPLATE = "postgres/foreignkey/drop"; + + private final static String COLUMN_ADD_TEMPLATE = "postgres/column/add"; + + private final static String COLUMN_ALTER_TYPE_TEMPLATE = "postgres/column/alter_column_type"; + + private final static String COLUMN_ALTER_NULL_TEMPLATE = "postgres/column/alter_column_null"; + + private final static String COLUMN_DROP_TEMPLATE = "postgres/column/drop"; + private final ColumnSQLDecoratorFactory columnSQLDecoratorFactory = new PostgresColumnDecoratorFactory(); @@ -38,6 +60,8 @@ public String createColumn(Column column) { @Override public String createTable(Table table, boolean dropTableIfExists) { + Map createParams = new HashMap<>(); + List definitions = table.getColumns().stream().map(this::createColumn).collect(Collectors.toList()); Optional primaryKeysForTable = createPrimaryKeysForTable(table); @@ -46,36 +70,22 @@ public String createTable(Table table, boolean dropTableIfExists) { StringBuilder stringBuilder = new StringBuilder(); if (dropTableIfExists) { - stringBuilder.append("DROP TABLE IF EXISTS "); - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder.append("`").append(table.getSchema()).append("`."); - } - stringBuilder.append(DEFAULT_WRAPPER).append(table.getName()).append(DEFAULT_WRAPPER).append("; \n"); + stringBuilder.append(dropTable(table)); } - stringBuilder.append("CREATE TABLE "); - - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder.append(DEFAULT_WRAPPER) - .append(table.getSchema()).append(DEFAULT_WRAPPER).append("."); - } + createParams.put("schemaName", table.getSchema()); + createParams.put("tableName", table.getName()); + createParams.put("tableCode", definitionAsString); + stringBuilder.append(TemplateEngine.process(TABLE_CREATE_TEMPLATE, createParams)); - stringBuilder.append(DEFAULT_WRAPPER).append(table.getName()).append(DEFAULT_WRAPPER).append("(").append(definitionAsString).append(");"); return stringBuilder.toString(); } @Override public String createTableSchema(Table table) { - StringBuilder stringBuilder = new StringBuilder(); - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder - .append("CREATE SCHEMA IF NOT EXISTS ") - .append(DEFAULT_WRAPPER) - .append(table.getSchema()) - .append(DEFAULT_WRAPPER) - .append(";"); - } - return stringBuilder.toString(); + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + return TemplateEngine.process(SCHEMA_CREATE_TEMPLATE, params); } @Override @@ -85,71 +95,74 @@ public String createDatabase(Database database, boolean dropTableIfExists) { Set schemas = database.getTables().stream().map(Table::getSchema).filter(s -> s != null && !s.isEmpty()).collect(Collectors.toSet()); if (!schemas.isEmpty()) { stringBuilder.append( - schemas - .stream() - .map(schema -> "CREATE SCHEMA IF NOT EXISTS " + DEFAULT_WRAPPER + schema + DEFAULT_WRAPPER) - .collect(Collectors.joining(";\r\r")) - + schemas + .stream() + .map(this::createSchema) + .collect(Collectors.joining()) ); - stringBuilder.append(";\r"); + stringBuilder.append("\r"); } stringBuilder.append(database.getTables() - .stream() - .map(table -> createTable(table, dropTableIfExists)) - .collect(Collectors.joining("\r\r"))); - - String foreignKeys = database - .getTables() - .stream() - .map(this::foreignKeys) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.joining()); - - if (!foreignKeys.isEmpty()) { - stringBuilder.append("\r").append(foreignKeys).append("\r"); - } + .stream() + .map(table -> createTable(table, dropTableIfExists)) + .collect(Collectors.joining("\r\r"))); + + stringBuilder.append("\r"); + + //Create ForeignKeys + stringBuilder.append(database.getTables() + .stream() + .map(table -> foreignKeys(table)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.joining("\r"))); + return stringBuilder.toString(); } @Override public String createForeignKey(ForeignKey foreignKey) { - return "ALTER TABLE" + handleNullSchema(foreignKey.getSchema(), foreignKey.getTableName()) + " ADD CONSTRAINT " - + foreignKey.getName() + " FOREIGN KEY ("+ DEFAULT_WRAPPER + foreignKey.getColumnName() + DEFAULT_WRAPPER +") REFERENCES " - + handleNullSchema(foreignKey.getPrimaryTableSchema(), foreignKey.getPrimaryTableName()) - + "("+ DEFAULT_WRAPPER + foreignKey.getPrimaryColumnName()+ DEFAULT_WRAPPER + ")" - + foreignKeyDeleteRuleSanitation(foreignKeyDeleteRule(foreignKey)) + ";\r"; + Map params = new HashMap<>(); + params.put("schemaName", foreignKey.getSchema()); + params.put("tableName", foreignKey.getTableName()); + params.put("foreignkeyColumn", foreignKey.getColumnName()); + params.put("primaryTableSchema", foreignKey.getPrimaryTableSchema()); + params.put("primaryTableName", foreignKey.getPrimaryTableName()); + params.put("foreignKeyPrimaryColumnName", foreignKey.getPrimaryColumnName()); + params.put("foreignkeyName", foreignKey.getName()); + params.put("deleteRule", foreignKeyDeleteRule(foreignKey)); + return TemplateEngine.process(FOREIGN_KEY_CREATE_TEMPLATE, params); } @Override public String alterColumn(ColumnChange change) { - Table table = change.getTable(); Column actual = change.getActual(); Column expected = change.getExpected(); + Map params = new HashMap<>(); + params.put("schemaName", change.getTable().getSchema()); + params.put("tableName", change.getTable().getName()); + params.put("columnName", expected.getName()); + + if (!Objects.equals(expected.getTypeName(), actual.getTypeName())) { + params.put("dataType", expected.getTypeName()); + return TemplateEngine.process(COLUMN_ALTER_TYPE_TEMPLATE, params); + } - if (!Objects.equals(expected.getTypeName(), actual.getTypeName()) - || !Objects.equals(expected.isNullable(), actual.isNullable())) { - String alterColumnString = columnSQLDecoratorFactory.decoratorFor(expected).expressSQl(); - String formattedAlterColumn = String.format("%s TYPE %s", alterColumnString.split(" ")[0], alterColumnString.split(" ")[1]); - - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("ALTER TABLE"); - stringBuilder.append(handleNullSchema(table.getSchema(), table.getName())); - stringBuilder.append(" ALTER COLUMN "); - stringBuilder.append(formattedAlterColumn); - if(expected.isNullable()){ - stringBuilder.append(", ALTER COLUMN "); - stringBuilder.append(expected.getName()); - stringBuilder.append(" DROP NOT NULL"); + if (!Objects.equals(expected.isNullable(), actual.isNullable())) { + if (expected.isNullable()) { + params.put("nullDefinition", "DROP NOT NULL"); + return TemplateEngine.process(COLUMN_ALTER_NULL_TEMPLATE, params); + } else { + params.put("nullDefinition", "SET NOT NULL"); + return TemplateEngine.process(COLUMN_ALTER_NULL_TEMPLATE, params); } - stringBuilder.append(";"); - return stringBuilder.toString(); } log.info("No action taken for changes detected in column: {}.{}.{}", change.getTable().getSchema(), change.getTable().getName(), expected.getName()); + return ""; } @@ -158,35 +171,47 @@ public String dropColumn(ColumnChange change) { Table table = change.getTable(); Column actual = change.getActual(); - return "ALTER TABLE" + - handleNullSchema(table.getSchema(), table.getName()) + " DROP COLUMN "+ DEFAULT_WRAPPER + - actual.getName() + DEFAULT_WRAPPER +";"; + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + params.put("tableName", table.getName()); + params.put("columnName", actual.getName()); + return TemplateEngine.process(COLUMN_DROP_TEMPLATE, params); } + + @Override public String addColumn(ColumnChange change) { Table table = change.getTable(); Column expected = change.getExpected(); - return "ALTER TABLE" + - handleNullSchema(table.getSchema(), table.getName()) + - " ADD COLUMN " + - columnSQLDecoratorFactory.decoratorFor(expected).expressSQl() + ";"; + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + params.put("tableName", table.getName()); + params.put("columnDefinition", columnSQLDecoratorFactory.decoratorFor(expected).expressSQl()); + return TemplateEngine.process(COLUMN_ADD_TEMPLATE, params); } @Override public String dropTable(Table actual) { - return "DROP TABLE" + handleNullSchema(actual.getSchema(), actual.getName()) + ";"; + Map params = new HashMap<>(); + params.put("schemaName", actual.getSchema()); + params.put("tableName", actual.getName()); + return TemplateEngine.process(TABLE_DROP_TEMPLATE, params); } @Override public String alterForeignKey(ForeignKeyChange change) { - return ""; + return dropForeignKey(change.getActual()) + "\r" + createForeignKey(change.getExpected()); } @Override public String dropForeignKey(ForeignKey actual) { - return "ALTER TABLE" + handleNullSchema(actual.getSchema(), actual.getTableName()) + " DROP CONSTRAINT " + DEFAULT_WRAPPER + actual.getName() + DEFAULT_WRAPPER + ";"; + Map params = new HashMap<>(); + params.put("schemaName", actual.getSchema()); + params.put("tableName", actual.getTableName()); + params.put("foreignkeyName", actual.getName()); + return TemplateEngine.process(FOREIGN_KEY_DROP_TEMPLATE, params); } @Override @@ -194,24 +219,29 @@ public String alterTable(Table expected, Table actual) { boolean doesPKExist = actual.getColumns().stream().map(Column::isPrimaryKey).reduce((aBoolean, aBoolean2) -> aBoolean || aBoolean2).orElse(false); boolean doWeNeedToCreatePk = expected.getColumns().stream().map(Column::isPrimaryKey).reduce((aBoolean, aBoolean2) -> aBoolean || aBoolean2).orElse(false); - StringBuilder stringBuilder = new StringBuilder("ALTER TABLE") - .append(handleNullSchema(expected.getSchema(), expected.getName())); + Map params = new HashMap<>(); + StringBuilder stringBuilder = new StringBuilder(); if (doesPKExist) { - stringBuilder.append(" DROP PRIMARY KEY"); + params.put("schemaName", expected.getSchema()); + params.put("tableName", tableNameWithSchema(expected)); + stringBuilder.append( + TemplateEngine.process(TABLE_ALTER_DROP_PRIMARY_KEY_TEMPLATE, params) + ); } if (doWeNeedToCreatePk) { Optional primaryKeysForTable = createPrimaryKeysForTable(expected); if (primaryKeysForTable.isPresent()) { - if (doesPKExist) { - stringBuilder.append(","); - } - stringBuilder.append(" ADD ").append(primaryKeysForTable.get()); + params.put("schemaName", expected.getSchema()); + params.put("tableName", tableNameWithSchema(expected)); + params.put("primaryKeyDefinition", primaryKeysForTable.get()); + stringBuilder.append( + TemplateEngine.process(TABLE_ALTER_ADD_PRIMARY_KEY_TEMPLATE, params) + ); } } - stringBuilder.append(";"); return stringBuilder.toString(); } @@ -221,7 +251,8 @@ private Optional createPrimaryKeysForTable(Table table) { .stream() .filter(Column::isPrimaryKey) .sorted((o1, o2) -> o1.getPrimaryKeySequenceId() < o2.getPrimaryKeySequenceId() ? -1 : 1) - .map(pk -> String.format(DEFAULT_WRAPPER+"%s"+DEFAULT_WRAPPER, pk.getName())) + .map(Column::getName) + .map(it -> "\"" + it + "\"") .collect(Collectors.toList()); if (primaryKeys.isEmpty()) { @@ -239,25 +270,13 @@ private Optional foreignKeys(Table table) { return result.isEmpty() ? Optional.empty() : Optional.of(result); } - //ALTER TABLE rosetta.contacts ADD CONSTRAINT contacts_fk FOREIGN KEY (contact_id) REFERENCES rosetta."user"(user_id); private String createForeignKeys(Column column) { return column.getForeignKeys().stream().map(this::createForeignKey).collect(Collectors.joining()); } - private String handleNullSchema(String schema, String tableName) { - return ((schema == null || schema.isEmpty()) ? " " : (" "+ DEFAULT_WRAPPER + schema + DEFAULT_WRAPPER +".")) + DEFAULT_WRAPPER + tableName + DEFAULT_WRAPPER; - } - - private String foreignKeyDeleteRuleSanitation(String deleteRule) { - if (deleteRule == null || deleteRule.isEmpty()) { - return ""; - } - return " " + deleteRule + " "; - } - - private String foreignKeyDeleteRule(ForeignKey foreignKey) { + protected String foreignKeyDeleteRule(ForeignKey foreignKey) { if (foreignKey.getDeleteRule() == null || foreignKey.getDeleteRule().isEmpty()) { - return ""; + return null; } switch (Integer.parseInt(foreignKey.getDeleteRule())) { case DatabaseMetaData.importedKeyCascade: @@ -271,8 +290,19 @@ private String foreignKeyDeleteRule(ForeignKey foreignKey) { case DatabaseMetaData.importedKeyInitiallyImmediate: case DatabaseMetaData.importedKeyNotDeferrable: default: - //todo add warn log - return ""; + return null; } } + + private String tableNameWithSchema(Table table) { + StringBuilder builder = new StringBuilder(); + builder.append(table.getName()); + return builder.toString(); + } + + private String createSchema(String schema) { + Map params = new HashMap<>(); + params.put("schemaName", schema); + return TemplateEngine.process(SCHEMA_CREATE_TEMPLATE, params); + } } diff --git a/ddl/src/main/resources/templates/postgres/column/add.sqlt b/ddl/src/main/resources/templates/postgres/column/add.sqlt new file mode 100644 index 00000000..f02237a4 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/add.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ADD COLUMN [(${columnDefinition})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD COLUMN [(${columnDefinition})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt b/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt new file mode 100644 index 00000000..c2d7ce3c --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/alter_column_null.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ALTER COLUMN "[(${columnName})]" [(${nullDefinition})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" [(${nullDefinition})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt b/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt new file mode 100644 index 00000000..0f70f55d --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/alter_column_type.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ALTER COLUMN "[(${columnName})]" SET DATA TYPE [(${dataType})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ALTER COLUMN "[(${columnName})]" SET DATA TYPE [(${dataType})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/column/drop.sqlt b/ddl/src/main/resources/templates/postgres/column/drop.sqlt new file mode 100644 index 00000000..4c3fcd9e --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/column/drop.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" DROP COLUMN "[(${columnName})]"; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP COLUMN "[(${columnName})]"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt b/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt new file mode 100644 index 00000000..e01031d5 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/foreignkey/create.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD CONSTRAINT [(${foreignkeyName})] FOREIGN KEY ("[(${foreignkeyColumn})]") REFERENCES "[(${schemaName})]"."[(${primaryTableName})]"("[(${foreignKeyPrimaryColumnName})]") [(${deleteRule})]; +[/] +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ADD CONSTRAINT [(${foreignkeyName})] FOREIGN KEY ("[(${foreignkeyColumn})]") REFERENCES "[(${primaryTableName})]"("[(${foreignKeyPrimaryColumnName})]") [(${deleteRule})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt b/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt new file mode 100644 index 00000000..6e8fa92f --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/foreignkey/drop.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${foreignkeyName})]"; +[/] +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" DROP CONSTRAINT "[(${foreignkeyName})]"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/schema/create.sqlt b/ddl/src/main/resources/templates/postgres/schema/create.sqlt new file mode 100644 index 00000000..439f3627 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/schema/create.sqlt @@ -0,0 +1,3 @@ +[# th:if="${schemaName} != null and ${schemaName} != ''"] +CREATE SCHEMA IF NOT EXISTS "[[(${schemaName})]]"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt b/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt new file mode 100644 index 00000000..ad2d0504 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/alter_add_primary_key.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" ADD [(${primaryKeyDefinition})]; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" ADD [(${primaryKeyDefinition})]; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt b/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt new file mode 100644 index 00000000..6103b4a0 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/alter_drop_primary_key.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +ALTER TABLE "[(${tableName})]" DROP CONSTRAINT "[(${tableName})]_pkey"; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +ALTER TABLE "[(${schemaName})]"."[(${tableName})]" DROP CONSTRAINT "[(${tableName})]_pkey"; +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/create.sqlt b/ddl/src/main/resources/templates/postgres/table/create.sqlt new file mode 100644 index 00000000..5e961a09 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/create.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +CREATE TABLE "[(${tableName})]"([(${tableCode})]); +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +CREATE TABLE "[(${schemaName})]"."[(${tableName})]"([(${tableCode})]); +[/] \ No newline at end of file diff --git a/ddl/src/main/resources/templates/postgres/table/drop.sqlt b/ddl/src/main/resources/templates/postgres/table/drop.sqlt new file mode 100644 index 00000000..d0526323 --- /dev/null +++ b/ddl/src/main/resources/templates/postgres/table/drop.sqlt @@ -0,0 +1,6 @@ +[# th:if="${schemaName} == null or ${schemaName} == ''"] +DROP TABLE IF EXISTS "[(${tableName})]"; +[/] +[# th:if="${schemaName} != null and ${schemaName} != ''"] +DROP TABLE IF EXISTS "[(${schemaName})]"."[(${tableName})]"; +[/] \ No newline at end of file diff --git a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java index b94066a2..153ee7a5 100644 --- a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java +++ b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java @@ -2,10 +2,8 @@ import com.adaptivescale.rosetta.common.models.Database; import com.adaptivescale.rosetta.ddl.change.*; -import com.adaptivescale.rosetta.ddl.change.comparator.MysqlForeignKeyChangeComparator; import com.adaptivescale.rosetta.ddl.change.comparator.PostgresForeignKeyChangeComparator; import com.adaptivescale.rosetta.ddl.change.model.Change; -import com.adaptivescale.rosetta.ddl.targets.mysql.MySqlDDLGenerator; import com.adaptivescale.rosetta.ddl.targets.postgres.PostgresDDLGenerator; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -22,163 +20,161 @@ public class PostgresDDLTest { @Test public void createDB() throws IOException { String ddl = generateDDL("clean_database"); - Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"ROSETTA\";\r" + - "CREATE TABLE \"ROSETTA\".\"PLAYER\"(\"Name\" VARCHAR(100), \"Position\" VARCHAR(100), \"Number\" NUMBER NOT NULL );\r" + + Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"ROSETTA\";\n" + + "CREATE TABLE \"ROSETTA\".\"PLAYER\"(\"Name\" VARCHAR(100), \"Position\" VARCHAR(100), \"Number\" NUMBER NOT NULL );\n" + "\r" + - "CREATE TABLE \"ROSETTA\".\"USER\"(\"USER_ID\" NUMBER NOT NULL , PRIMARY KEY (\"USER_ID\"));", ddl); + "CREATE TABLE \"ROSETTA\".\"USER\"(\"USER_ID\" NUMBER NOT NULL , PRIMARY KEY (\"USER_ID\"));\n" + + "\r", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addTable() throws IOException { String ddl = generateDDL("add_table"); - Assertions.assertEquals("\rCREATE TABLE \"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\r" + - "\rCREATE TABLE \"Position2\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\r" + - "ALTER TABLE \"Position\" ADD CONSTRAINT Position_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\");\r" + - "\r" + - "ALTER TABLE \"Position2\" ADD CONSTRAINT Position2_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\");\r", ddl); + Assertions.assertEquals("CREATE TABLE \"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\n" + + "\rCREATE TABLE \"Position2\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\n" + + "ALTER TABLE \"Position\" ADD CONSTRAINT Position_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\") ;\n" + + "ALTER TABLE \"Position2\" ADD CONSTRAINT Position2_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEAM\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addTable2() throws IOException { String ddl = generateDDL("add_table2"); - Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"new\";\r" + - "CREATE TABLE \"new\".\"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR," + - " \"Name\" VARCHAR, PRIMARY KEY (\"ID\"));", ddl); + Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS \"new\";\n" + + "CREATE TABLE \"new\".\"Position\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, PRIMARY KEY (\"ID\"));\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropTable() throws IOException { String ddl = generateDDL("drop_table"); - Assertions.assertEquals("DROP TABLE \"TEAMPLAYERS\";", ddl); + Assertions.assertEquals("DROP TABLE IF EXISTS \"TEAMPLAYERS\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addColumn() throws IOException { String ddl = generateDDL("add_column"); - Assertions.assertEquals("ALTER TABLE \"Position\" ADD COLUMN \"DESCRIPTION\" varchar(100);", ddl); + Assertions.assertEquals("ALTER TABLE \"Position\" ADD COLUMN \"DESCRIPTION\" varchar(100);\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addColumnWithForeignKey() throws IOException { String ddl = generateDDL("add_column_with_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"POSITION_ID\" numeric;\r" + - "ALTER TABLE \"FBAL\".\"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"FBAL\".\"Position\"(\"ID\") ON DELETE NO ACTION ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"POSITION_ID\" numeric;\n" + + "ALTER TABLE \"FBAL\".\"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"FBAL\".\"Position\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addColumnAsPrimaryKey() throws IOException { String ddl = generateDDL("add_column_as_primary_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"ID\" numeric NOT NULL ;\r" + - "ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD COLUMN \"ID\" numeric NOT NULL ;\n" + + "ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropColumn() throws IOException { String ddl = generateDDL("drop_column"); - Assertions.assertEquals("ALTER TABLE \"Position\" DROP COLUMN \"DESCRIPTION\";", ddl); + Assertions.assertEquals("ALTER TABLE \"Position\" DROP COLUMN \"DESCRIPTION\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterColumnDataType() throws IOException { String ddl = generateDDL("alter_column_data_type"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"name\" TYPE INTEGER, ALTER COLUMN name DROP NOT NULL;", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"name\" SET DATA TYPE INTEGER;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterColumnToNullable() throws IOException { String ddl = generateDDL("alter_column_to_nullable"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" TYPE numeric, ALTER COLUMN ID DROP NOT NULL;", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" DROP NOT NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterColumnToNotNullable() throws IOException { String ddl = generateDDL("alter_column_to_not_nullable"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" TYPE numeric;", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ALTER COLUMN \"ID\" SET NOT NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropColumnWithForeignKey() throws IOException { String ddl = generateDDL("drop_column_with_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"FBAL\".\"PLAYER\" DROP CONSTRAINT \"PLAYER_FK\";\r" + - "ALTER TABLE \"FBAL\".\"PLAYER\" DROP COLUMN \"POSITION_ID\";", ddl); + Assertions.assertEquals("ALTER TABLE \"FBAL\".\"PLAYER\" DROP CONSTRAINT \"PLAYER_FK\";\n" + + "ALTER TABLE \"FBAL\".\"PLAYER\" DROP COLUMN \"POSITION_ID\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropColumnWithPrimaryKeyReferenced() throws IOException { String ddl = generateDDL("drop_column_with_primary_key_referenced"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropTableWhereColumnIsReferenced() throws IOException { String ddl = generateDDL("drop_table_where_column_is_referenced"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK_TEAM\";\r" + - "DROP TABLE \"TEAM\";", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK_TEAM\";\n" + + "DROP TABLE IF EXISTS \"TEAM\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addForeignKey() throws IOException { String ddl = generateDDL("add_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") " + - "REFERENCES \"Position\"(\"ID\") ON DELETE NO ACTION ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"Position\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropForeignKey() throws IOException { String ddl = generateDDL("drop_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropPrimaryKey() throws IOException { String ddl = generateDDL("drop_primary_key"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"PLAYER\" DROP PRIMARY KEY;", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"PLAYER\" DROP CONSTRAINT \"PLAYER_pkey\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void addPrimaryKey() throws IOException { String ddl = generateDDL("add_primary_key"); - Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\");\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterPrimaryKey() throws IOException { String ddl = generateDDL("alter_primary_key"); - Assertions.assertEquals("\r" + - "ALTER TABLE \"PLAYER\" DROP PRIMARY KEY, ADD PRIMARY KEY (\"ID\", \"POSITION_ID\");\r", ddl); + Assertions.assertEquals("ALTER TABLE \"PLAYER\" DROP CONSTRAINT \"PLAYER_pkey\";\n" + + "ALTER TABLE \"PLAYER\" ADD PRIMARY KEY (\"ID\", \"POSITION_ID\");\n" + + "\r", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterForeignKeyName() throws IOException { String ddl = generateDDL("alter_foreign_key_name"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_CHANGED_FK FOREIGN KEY (\"PLAYERID\") REFERENCES " + - "\"PLAYER\"(\"ID\") ON DELETE NO ACTION ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_CHANGED_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"PLAYER\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void alterForeignKey() throws IOException { String ddl = generateDDL("alter_foreign_key"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"PLAYER\"(\"ID\") ON DELETE SET NULL ;\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"PLAYER\"(\"ID\") ON DELETE SET NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropPrimaryKeyColumnAndAlterForeignKey() throws IOException { String ddl = generateDDL("drop_pk_column_and_alter_fk"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\");\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"PLAYER\" DROP COLUMN \"ID\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } @Test public void dropTableWithPrimaryKeyColumnAndAlterForeignKey() throws IOException { String ddl = generateDDL("drop_table_with_pk_column_and_alter_fk"); - Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\r" + - "DROP TABLE \"PLAYER\";\r" + - "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\");\r", ddl); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "DROP TABLE IF EXISTS \"PLAYER\";\n" + + "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } private String generateDDL(String testType) throws IOException { From 1de007535964b63512bc52d05f9474a31a881703 Mon Sep 17 00:00:00 2001 From: Nuhi Date: Thu, 6 Jul 2023 13:50:29 +0200 Subject: [PATCH 05/11] fixed: duplicate foreign key issue (#188) --- .../rosetta/common/models/ForeignKey.java | 15 +++++++++++++++ .../core/extractors/column/ColumnsExtractor.java | 12 +++++++++--- .../extractors/column/DB2ColumnsExtractor.java | 12 +++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/com/adaptivescale/rosetta/common/models/ForeignKey.java b/common/src/main/java/com/adaptivescale/rosetta/common/models/ForeignKey.java index 36a2b991..19df0364 100644 --- a/common/src/main/java/com/adaptivescale/rosetta/common/models/ForeignKey.java +++ b/common/src/main/java/com/adaptivescale/rosetta/common/models/ForeignKey.java @@ -1,5 +1,7 @@ package com.adaptivescale.rosetta.common.models; +import java.util.Objects; + public class ForeignKey { private String name; private String schema; @@ -74,4 +76,17 @@ public String getColumnName() { public void setColumnName(String columnName) { this.columnName = columnName; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ForeignKey that = (ForeignKey) o; + return Objects.equals(name, that.name) && Objects.equals(tableName, that.tableName) && Objects.equals(columnName, that.columnName); + } + + @Override + public int hashCode() { + return Objects.hash(name, tableName, columnName); + } } diff --git a/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/ColumnsExtractor.java b/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/ColumnsExtractor.java index 0e6143b2..c3723d27 100644 --- a/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/ColumnsExtractor.java +++ b/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/ColumnsExtractor.java @@ -26,6 +26,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; +import java.util.stream.Collectors; public class ColumnsExtractor implements ColumnExtractor> { @@ -79,7 +80,7 @@ protected void extract(ResultSet resultSet, Column column) throws SQLException { private Map> extractForeignKeys(java.sql.Connection connection, Table table) throws SQLException { ResultSet exportedKeys = connection.getMetaData().getImportedKeys(this.connection.getDatabaseName(), table.getSchema(), table.getName()); - Map> result = new HashMap<>(); + Map> result = new HashMap<>(); while (exportedKeys.next()) { ForeignKey foreignKey = new ForeignKey(); @@ -101,11 +102,16 @@ private Map> extractForeignKeys(java.sql.Connection con foreignKey.setPrimaryTableName(exportedKeys.getString("PKTABLE_NAME")); foreignKey.setPrimaryColumnName(exportedKeys.getString("PKCOLUMN_NAME")); - List foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new ArrayList<>()); + Set foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new HashSet<>()); foreignKeys.add(foreignKey); } - return result; + return result.entrySet() + .stream() + .collect(Collectors.toMap( + Map.Entry::getKey, + entry -> new ArrayList<>(entry.getValue()) + )); } private Map extractPrimaryKeys(java.sql.Connection connection, Table table) throws SQLException { diff --git a/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/DB2ColumnsExtractor.java b/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/DB2ColumnsExtractor.java index e60312c7..96edbac1 100644 --- a/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/DB2ColumnsExtractor.java +++ b/source/src/main/java/com/adataptivescale/rosetta/source/core/extractors/column/DB2ColumnsExtractor.java @@ -11,6 +11,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; +import java.util.stream.Collectors; @RosettaModule( name = "db2", @@ -61,7 +62,7 @@ protected void extract(ResultSet resultSet, Column column) throws SQLException { private Map> extractForeignKeys(java.sql.Connection connection, Table table) throws SQLException { ResultSet exportedKeys = connection.getMetaData().getImportedKeys(null, table.getSchema(), table.getName()); - Map> result = new HashMap<>(); + Map> result = new HashMap<>(); while (exportedKeys.next()) { ForeignKey foreignKey = new ForeignKey(); @@ -83,11 +84,16 @@ private Map> extractForeignKeys(java.sql.Connection con foreignKey.setPrimaryTableName(exportedKeys.getString("PKTABLE_NAME")); foreignKey.setPrimaryColumnName(exportedKeys.getString("PKCOLUMN_NAME")); - List foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new ArrayList<>()); + Set foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new HashSet<>()); foreignKeys.add(foreignKey); } - return result; + return result.entrySet() + .stream() + .collect(Collectors.toMap( + Map.Entry::getKey, + entry -> new ArrayList<>(entry.getValue()) + )); } private Map extractPrimaryKeys(java.sql.Connection connection, Table table) throws SQLException { From 1974027614cf8c3633ea8851a7eaf104035f6bcd Mon Sep 17 00:00:00 2001 From: Femi3211 Date: Thu, 6 Jul 2023 14:34:16 +0200 Subject: [PATCH 06/11] Added: Test cases with schema for Postgres --- .../rosetta/ddl/test/PostgresDDLTest.java | 120 ++++++++++ .../actual_model.yaml | 37 +++ .../expected_model.yaml | 47 ++++ .../actual_model.yaml | 28 +++ .../expected_model.yaml | 47 ++++ .../add_column_with_schema/actual_model.yaml | 157 +++++++++++++ .../expected_model.yaml | 167 +++++++++++++ .../actual_model.yaml | 141 +++++++++++ .../expected_model.yaml | 150 ++++++++++++ .../actual_model.yaml | 148 ++++++++++++ .../expected_model.yaml | 148 ++++++++++++ .../add_table_with_schema/actual_model.yaml | 167 +++++++++++++ .../add_table_with_schema/expected_model.yaml | 220 ++++++++++++++++++ .../actual_model.yaml | 168 +++++++++++++ .../expected_model.yaml | 168 +++++++++++++ .../actual_model.yaml | 47 ++++ .../expected_model.yaml | 47 ++++ .../actual_model.yaml | 47 ++++ .../expected_model.yaml | 47 ++++ .../actual_model.yaml | 153 ++++++++++++ .../expected_model.yaml | 153 ++++++++++++ .../actual_model.yaml | 157 +++++++++++++ .../expected_model.yaml | 157 +++++++++++++ .../actual_model.yaml | 133 +++++++++++ .../expected_model.yaml | 115 +++++++++ .../drop_column_with_schema/actual_model.yaml | 167 +++++++++++++ .../expected_model.yaml | 157 +++++++++++++ .../actual_model.yaml | 150 ++++++++++++ .../expected_model.yaml | 141 +++++++++++ .../actual_model.yaml | 134 +++++++++++ .../expected_model.yaml | 124 ++++++++++ .../actual_model.yaml | 147 ++++++++++++ .../expected_model.yaml | 147 ++++++++++++ .../actual_model.yaml | 155 ++++++++++++ .../expected_model.yaml | 102 ++++++++ .../actual_model.yaml | 134 +++++++++++ .../expected_model.yaml | 110 +++++++++ .../drop_table_with_schema/actual_model.yaml | 148 ++++++++++++ .../expected_model.yaml | 110 +++++++++ 39 files changed, 4895 insertions(+) create mode 100644 ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_column_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_column_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_table_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/add_table_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_column_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_column_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/expected_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_table_with_schema/actual_model.yaml create mode 100644 ddl/src/test/resources/ddl/postgres/drop_table_with_schema/expected_model.yaml diff --git a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java index 153ee7a5..370232e7 100644 --- a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java +++ b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/PostgresDDLTest.java @@ -177,6 +177,126 @@ public void dropTableWithPrimaryKeyColumnAndAlterForeignKey() throws IOException "ALTER TABLE \"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); } + @Test + public void addTableWithSchema() throws IOException { + String ddl = generateDDL("add_table_with_schema"); + Assertions.assertEquals( + "CREATE TABLE \"TEST\".\"Position2\"(\"ID\" DECIMAL(10) NOT NULL , \"DESCRIPTION\" VARCHAR, \"Name\" VARCHAR, \"TEAMID\" DECIMAL(10), PRIMARY KEY (\"ID\"));\n" + + "ALTER TABLE \"TEST\".\"Position2\" ADD CONSTRAINT Position2_FK_TEAM FOREIGN KEY (\"TEAMID\") REFERENCES \"TEST\".\"TEAM\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + + @Test + public void dropTableWithSchema() throws IOException { + String ddl = generateDDL("drop_table_with_schema"); + Assertions.assertEquals("DROP TABLE IF EXISTS \"TEST\".\"TEAMPLAYERS\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + @Test + public void addColumnWithSchema() throws IOException { + String ddl = generateDDL("add_column_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"Position\" ADD COLUMN \"DESCRIPTION\" VARCHAR;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void addColumnWithForeignKeyWithSchema() throws IOException { + String ddl = generateDDL("add_column_with_foreign_key_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ADD COLUMN \"POSITION_ID\" numeric;\n" + + "ALTER TABLE \"TEST\".\"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"TEST\".\"Position\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void addColumnAsPrimaryKeyWithSchema() throws IOException { + String ddl = generateDDL("add_column_as_primary_key_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ADD COLUMN \"ID\" numeric NOT NULL ;\n" + + "ALTER TABLE \"TEST\".\"PLAYER\" ADD PRIMARY KEY (\"ID\");\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void addForeignKeyWithSchema() throws IOException { + String ddl = generateDDL("add_foreign_key_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ADD CONSTRAINT PLAYER_FK FOREIGN KEY (\"POSITION_ID\") REFERENCES \"TEST\".\"Position\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void addPrimaryKeyWithSchema() throws IOException { + String ddl = generateDDL("add_primary_key_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ADD PRIMARY KEY (\"ID\");\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void alterColumnDataTypeWithSchema() throws IOException { + String ddl = generateDDL("alter_column_data_type_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ALTER COLUMN \"name\" SET DATA TYPE varchar;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void alterForeignKeyNameWithSchema() throws IOException { + String ddl = generateDDL("alter_foreign_key_name_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEST\".\"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_CHANGED_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"TEST\".\"PLAYER\"(\"ID\") ON DELETE NO ACTION;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void alterForeignKeyWithSchema() throws IOException { + String ddl = generateDDL("alter_foreign_key_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEST\".\"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"TEST\".\"PLAYER\"(\"ID\") ON DELETE SET NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void dropColumnWithSchema() throws IOException { + String ddl = generateDDL("drop_column_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"TEAM\" DROP COLUMN \"country\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void dropColumnWithPrimaryKeyReferencedWithSchema() throws IOException { + String ddl = generateDDL("drop_column_with_primary_key_referenced_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK_TEAM\";\n" + + "ALTER TABLE \"TEST\".\"PLAYER\" DROP COLUMN \"ID\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void dropTableWithPrimaryKeyColumnAndAlterForeignKeyWithSchema() throws IOException { + String ddl = generateDDL("drop_table_with_pk_column_and_alter_fk_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "DROP TABLE IF EXISTS \"TEST\".\"PLAYER\";\n" + + "ALTER TABLE \"TEST\".\"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"TEST\".\"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void dropTableWhereColumnIsReferencedWithSchema() throws IOException { + String ddl = generateDDL("drop_table_where_column_is_referenced_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEAM\".\"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK_TEAM\";\n" + + "DROP TABLE IF EXISTS \"TEST\".\"TEAM\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void dropPrimaryKeyColumnAndAlterForeignKeyWithSchema() throws IOException { + String ddl = generateDDL("drop_pk_column_and_alter_fk_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n" + + "ALTER TABLE \"TEST\".\"PLAYER\" DROP COLUMN \"ID\";\n" + + "ALTER TABLE \"TEST\".\"TEAMPLAYERS\" ADD CONSTRAINT TEAMPLAYERS_FK FOREIGN KEY (\"PLAYERID\") REFERENCES \"TEST\".\"POSITION\"(\"ID\") ;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void alterColumnToNullableWithSchema() throws IOException { + String ddl = generateDDL("alter_column_to_nullable_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ALTER COLUMN \"ID\" DROP NOT NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void alterColumnToNotNullableWithSchema() throws IOException { + String ddl = generateDDL("alter_column_to_not_nullable_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"PLAYER\" ALTER COLUMN \"ID\" SET NOT NULL;\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + + @Test + public void dropForeignKeyWithSchema() throws IOException { + String ddl = generateDDL("drop_foreign_key_with_schema"); + Assertions.assertEquals("ALTER TABLE \"TEST\".\"TEAMPLAYERS\" DROP CONSTRAINT \"TEAMPLAYERS_FK\";\n", ddl.replaceAll("(?m)^[ \t]*\r?\n", "")); + } + private String generateDDL(String testType) throws IOException { Database actual = Utils.getDatabase(resourceDirectory.resolve(testType), "actual_model.yaml"); Database expected = Utils.getDatabase(resourceDirectory.resolve(testType), "expected_model.yaml"); diff --git a/ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..95f1b6e1 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/actual_model.yaml @@ -0,0 +1,37 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "FBAL" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "FBAL" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..b9b2f70d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_column_as_primary_key_with_schema/expected_model.yaml @@ -0,0 +1,47 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "FBAL" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "FBAL" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..faab8169 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/actual_model.yaml @@ -0,0 +1,28 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..23b0dc74 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_column_with_foreign_key_with_schema/expected_model.yaml @@ -0,0 +1,47 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "TEST" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_column_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/add_column_with_schema/actual_model.yaml new file mode 100644 index 00000000..9eb9b270 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_column_with_schema/actual_model.yaml @@ -0,0 +1,157 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + schema: "TEST" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_column_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/add_column_with_schema/expected_model.yaml new file mode 100644 index 00000000..c300e6cd --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_column_with_schema/expected_model.yaml @@ -0,0 +1,167 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + schema: "TEST" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..e9c5adcc --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/actual_model.yaml @@ -0,0 +1,141 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..bcf05601 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_foreign_key_with_schema/expected_model.yaml @@ -0,0 +1,150 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "TEST" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..38436a56 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/actual_model.yaml @@ -0,0 +1,148 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: false + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..5d58a32d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_primary_key_with_schema/expected_model.yaml @@ -0,0 +1,148 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_table_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/add_table_with_schema/actual_model.yaml new file mode 100644 index 00000000..c300e6cd --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_table_with_schema/actual_model.yaml @@ -0,0 +1,167 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + schema: "TEST" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/add_table_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/add_table_with_schema/expected_model.yaml new file mode 100644 index 00000000..a9ca0a33 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/add_table_with_schema/expected_model.yaml @@ -0,0 +1,220 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + schema: "TEST" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "Position2" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position2_FK_TEAM" + schema: "TEST" + tableName: "Position2" + columnName: "TEAMID" + deleteRule: "1" + primaryTableSchema: "TEST" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/actual_model.yaml new file mode 100644 index 00000000..a8b04aa7 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/actual_model.yaml @@ -0,0 +1,168 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "INTEGER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: false + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + schema: "TEST" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/expected_model.yaml new file mode 100644 index 00000000..f9b3d266 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_column_data_type_with_schema/expected_model.yaml @@ -0,0 +1,168 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + schema: "TEST" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/actual_model.yaml new file mode 100644 index 00000000..5b87123d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/actual_model.yaml @@ -0,0 +1,47 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "FBAL" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "FBAL" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: true + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/expected_model.yaml new file mode 100644 index 00000000..b9b2f70d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_column_to_not_nullable_with_schema/expected_model.yaml @@ -0,0 +1,47 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "FBAL" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "FBAL" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/actual_model.yaml new file mode 100644 index 00000000..b9b2f70d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/actual_model.yaml @@ -0,0 +1,47 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "FBAL" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "FBAL" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/expected_model.yaml new file mode 100644 index 00000000..5b87123d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_column_to_nullable_with_schema/expected_model.yaml @@ -0,0 +1,47 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "FBAL" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "FBAL" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: true + autoincrement: false + primaryKey: true +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/actual_model.yaml new file mode 100644 index 00000000..5c4ba4b3 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/actual_model.yaml @@ -0,0 +1,153 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + schema: "TEST" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + schema: "TEST" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/expected_model.yaml new file mode 100644 index 00000000..84a9c3cc --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_name_with_schema/expected_model.yaml @@ -0,0 +1,153 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + schema: "TEST" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_CHANGED_FK" + schema: "TEST" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..a2e4b231 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/actual_model.yaml @@ -0,0 +1,157 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "TEST" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true + - name: "Position" + type: "TABLE" + schema: "TEST" + columns: + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true + - name: "DESCRIPTION" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "Name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "country" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + schema: "TEST" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "PLAYERID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + schema: "TEST" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..df541d97 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/alter_foreign_key_with_schema/expected_model.yaml @@ -0,0 +1,157 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "POSITION_ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + schema: "TEST" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true + - name: "Position" + type: "TABLE" + schema: "TEST" + columns: + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true + - name: "DESCRIPTION" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "Name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "country" + typeName: "varchar" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 100 + nullable: true + autoincrement: false + primaryKey: false + - name: "ID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 0 + scale: 0 + precision: 38 + nullable: false + autoincrement: false + primaryKey: true + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + schema: "TEST" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false + - name: "PLAYERID" + typeName: "numeric" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + schema: "TEST" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "2" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + autoincrement: false + primaryKey: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/actual_model.yaml new file mode 100644 index 00000000..867088cb --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/actual_model.yaml @@ -0,0 +1,133 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableSchema: "TEST" + primaryTableName: "TEAM" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/expected_model.yaml new file mode 100644 index 00000000..a7cae18d --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_column_with_primary_key_referenced_with_schema/expected_model.yaml @@ -0,0 +1,115 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: false + autoincrement: false + nullable: true + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_column_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_column_with_schema/actual_model.yaml new file mode 100644 index 00000000..c300e6cd --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_column_with_schema/actual_model.yaml @@ -0,0 +1,167 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + schema: "TEST" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_column_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_column_with_schema/expected_model.yaml new file mode 100644 index 00000000..1b8a25b2 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_column_with_schema/expected_model.yaml @@ -0,0 +1,157 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + schema: "TEST" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "Position_FK_TEAM" + tableName: "Position" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + schema: "TEST" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..181a65ea --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/actual_model.yaml @@ -0,0 +1,150 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + schema: "TEST" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..87511d2a --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_foreign_key_with_schema/expected_model.yaml @@ -0,0 +1,141 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/actual_model.yaml new file mode 100644 index 00000000..d5920b68 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/actual_model.yaml @@ -0,0 +1,134 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + schema: "TEST" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/expected_model.yaml new file mode 100644 index 00000000..0e364b5c --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_pk_column_and_alter_fk_with_schema/expected_model.yaml @@ -0,0 +1,124 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + schema: "TEST" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableSchema: "TEST" + primaryTableName: "POSITION" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/actual_model.yaml new file mode 100644 index 00000000..f9b2fb45 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/actual_model.yaml @@ -0,0 +1,147 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/expected_model.yaml new file mode 100644 index 00000000..ed1018b2 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_primary_key_with_schema/expected_model.yaml @@ -0,0 +1,147 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: false + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "3" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "3" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/actual_model.yaml new file mode 100644 index 00000000..f98875b2 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/actual_model.yaml @@ -0,0 +1,155 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: true + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + schema: "TEAM" + columnName: "TEAMID" + deleteRule: "3" + primaryTableSchema: "TEST" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/expected_model.yaml new file mode 100644 index 00000000..51efcf7b --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_table_where_column_is_referenced_with_schema/expected_model.yaml @@ -0,0 +1,102 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "3" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 100 + scale: 0 + precision: 100 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "NUMBER" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 38 + scale: 0 + precision: 38 + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "Snowflake" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/actual_model.yaml new file mode 100644 index 00000000..d5920b68 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/actual_model.yaml @@ -0,0 +1,134 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + schema: "TEST" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + schema: "TEST" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableSchema: "TEST" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/expected_model.yaml new file mode 100644 index 00000000..13db97e5 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_table_with_pk_column_and_alter_fk_with_schema/expected_model.yaml @@ -0,0 +1,110 @@ +--- +tables: + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + primaryKey: false + autoincrement: false + nullable: true + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + primaryKey: true + autoincrement: false + nullable: false + - name: "TEAMPLAYERS" + type: "TABLE" + schema: "TEST" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + schema: "TEST" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableSchema: "TEST" + primaryTableName: "POSITION" + primaryColumnName: "ID" + primaryKey: false + autoincrement: false + nullable: true +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_table_with_schema/actual_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_table_with_schema/actual_model.yaml new file mode 100644 index 00000000..df30f3a2 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_table_with_schema/actual_model.yaml @@ -0,0 +1,148 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "TEAMPLAYERS" + schema: "TEST" + type: "TABLE" + columns: + - name: "TEAMID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK_TEAM" + tableName: "TEAMPLAYERS" + columnName: "TEAMID" + deleteRule: "1" + primaryTableName: "TEAM" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "PLAYERID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "TEAMPLAYERS_FK" + tableName: "TEAMPLAYERS" + columnName: "PLAYERID" + deleteRule: "1" + primaryTableName: "PLAYER" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" diff --git a/ddl/src/test/resources/ddl/postgres/drop_table_with_schema/expected_model.yaml b/ddl/src/test/resources/ddl/postgres/drop_table_with_schema/expected_model.yaml new file mode 100644 index 00000000..b6384e06 --- /dev/null +++ b/ddl/src/test/resources/ddl/postgres/drop_table_with_schema/expected_model.yaml @@ -0,0 +1,110 @@ +--- +tables: + - name: "PLAYER" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "POSITION_ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 10 + scale: 0 + precision: 10 + foreignKeys: + - name: "PLAYER_FK" + tableName: "PLAYER" + columnName: "POSITION_ID" + deleteRule: "1" + primaryTableName: "Position" + primaryColumnName: "ID" + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "Position" + type: "TABLE" + columns: + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false + - name: "DESCRIPTION" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "Name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "TEAM" + type: "TABLE" + columns: + - name: "name" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "country" + typeName: "VARCHAR" + ordinalPosition: 0 + primaryKeySequenceId: 0 + columnDisplaySize: 0 + scale: 0 + precision: 0 + nullable: true + primaryKey: false + autoincrement: false + - name: "ID" + typeName: "DECIMAL" + ordinalPosition: 0 + primaryKeySequenceId: 1 + columnDisplaySize: 10 + scale: 0 + precision: 10 + nullable: false + primaryKey: true + autoincrement: false +databaseProductName: "MySQL" +databaseType: "postgres" From c66d800585c16e24c148e9ea09b376c0607d116e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cflakronademi=E2=80=9D?= Date: Mon, 10 Jul 2023 16:20:19 +0200 Subject: [PATCH 07/11] Added : BigQuery DDL Templates --- .../bigquery/BigQueryDDLGenerator.java | 126 +++++++++--------- .../templates/bigquerry/column/add.sqlt | 1 + .../bigquerry/column/alter_column_null.sqlt | 0 .../bigquerry/column/alter_column_type.sqlt | 1 + .../templates/bigquerry/column/drop.sqlt | 1 + .../bigquerry/foreignkey/create.sqlt | 0 .../templates/bigquerry/foreignkey/drop.sqlt | 0 .../templates/bigquerry/schema/create.sqlt | 1 + .../table/alter_add_primary_key.sqlt | 0 .../table/alter_drop_primary_key.sqlt | 0 .../templates/bigquerry/table/create.sqlt | 1 + .../templates/bigquerry/table/drop.sqlt | 1 + .../rosetta/ddl/test/BigQueryDDLTest.java | 24 ++-- 13 files changed, 78 insertions(+), 78 deletions(-) create mode 100644 ddl/src/main/resources/templates/bigquerry/column/add.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/column/alter_column_null.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/column/alter_column_type.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/column/drop.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/foreignkey/create.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/foreignkey/drop.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/schema/create.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/table/alter_add_primary_key.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/table/alter_drop_primary_key.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/table/create.sqlt create mode 100644 ddl/src/main/resources/templates/bigquerry/table/drop.sqlt diff --git a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/bigquery/BigQueryDDLGenerator.java b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/bigquery/BigQueryDDLGenerator.java index 61513893..571750c8 100644 --- a/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/bigquery/BigQueryDDLGenerator.java +++ b/ddl/src/main/java/com/adaptivescale/rosetta/ddl/targets/bigquery/BigQueryDDLGenerator.java @@ -1,5 +1,4 @@ package com.adaptivescale.rosetta.ddl.targets.bigquery; - import com.adaptivescale.rosetta.common.annotations.RosettaModule; import com.adaptivescale.rosetta.common.models.Column; import com.adaptivescale.rosetta.common.models.Database; @@ -13,19 +12,27 @@ import com.adaptivescale.rosetta.ddl.targets.ColumnSQLDecoratorFactory; import com.adaptivescale.rosetta.ddl.utils.TemplateEngine; import lombok.extern.slf4j.Slf4j; - import java.util.*; import java.util.stream.Collectors; -import static com.adaptivescale.rosetta.ddl.targets.postgres.Constants.DEFAULT_WRAPPER; - @Slf4j @RosettaModule( name = "bigquery", type = RosettaModuleTypes.DDL_GENERATOR ) public class BigQueryDDLGenerator implements DDL { + private final static String TABLE_CREATE_TEMPLATE = "bigquerry/table/create"; + + + private final static String TABLE_DROP_TEMPLATE = "bigquerry/table/drop"; + private final static String SCHEMA_CREATE_TEMPLATE = "bigquerry/schema/create"; + + private final static String COLUMN_ADD_TEMPLATE = "bigquerry/column/add"; + + private final static String COLUMN_ALTER_TYPE_TEMPLATE = "bigquerry/column/alter_column_type"; + + private final static String COLUMN_DROP_TEMPLATE = "bigquerry/column/drop"; private static String VIEW_DROP_TEMPLATE = "bigquery/view/drop"; private static String VIEW_CREATE_TEMPLATE = "bigquery/view/create"; private static String VIEW_ALTER_TEMPLATE = "bigquery/view/alter"; @@ -42,118 +49,100 @@ public String createColumn(Column column) { @Override public String createTable(Table table, boolean dropTableIfExists) { + Map createParams = new HashMap<>(); + List definitions = table.getColumns().stream().map(this::createColumn).collect(Collectors.toList()); - String definitionAsString = String.join(", ", definitions); - StringBuilder builder = new StringBuilder(); + String definitionAsString = String.join(", ", definitions); + StringBuilder stringBuilder = new StringBuilder(); if (dropTableIfExists) { - builder.append("DROP TABLE IF EXISTS "); - if (table.getSchema() != null && !table.getSchema().isBlank()) { - builder.append("`") - .append(table.getSchema()) - .append("`."); - } - builder.append("`").append(table.getName()).append("`; \n"); - } - - builder.append("CREATE TABLE "); - - if (table.getSchema() != null && !table.getSchema().isBlank()) { - builder.append("`") - .append(table.getSchema()) - .append("`."); + stringBuilder.append(dropTable(table)); } - builder.append("`") - .append(table.getName()) - .append("`") - .append("(") - .append(definitionAsString) - .append(");"); + createParams.put("schemaName", table.getSchema()); + createParams.put("tableName", table.getName()); + createParams.put("tableCode", definitionAsString); + stringBuilder.append(TemplateEngine.process(TABLE_CREATE_TEMPLATE, createParams)); - return builder.toString(); + return stringBuilder.toString(); } @Override public String createTableSchema(Table table) { - StringBuilder stringBuilder = new StringBuilder(); - if (table.getSchema() != null && !table.getSchema().isBlank()) { - stringBuilder - .append("CREATE SCHEMA IF NOT EXISTS ") - .append(table.getSchema()) - .append(";"); - } - return stringBuilder.toString(); + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + return TemplateEngine.process(SCHEMA_CREATE_TEMPLATE, params); } - @Override public String createDatabase(Database database, boolean dropTableIfExists) { StringBuilder stringBuilder = new StringBuilder(); - Set schemas = database.getTables().stream().map(Table::getSchema) - .filter(s -> s != null && !s.isEmpty()).collect(Collectors.toSet()); + Set schemas = database.getTables().stream().map(Table::getSchema).filter(s -> s != null && !s.isEmpty()).collect(Collectors.toSet()); if (!schemas.isEmpty()) { stringBuilder.append( schemas .stream() - .map(schema -> "CREATE SCHEMA IF NOT EXISTS " + schema) - .collect(Collectors.joining(";\r\r")) - + .map(this::createSchema) + .collect(Collectors.joining()) ); - stringBuilder.append(";\r"); + stringBuilder.append("\r"); } stringBuilder.append(database.getTables() .stream() .map(table -> createTable(table, dropTableIfExists)) - .collect(Collectors.joining("\r"))); + .collect(Collectors.joining("\r\r"))); return stringBuilder.toString(); } @Override public String alterColumn(ColumnChange change) { - Column actual = change.getActual(); Column expected = change.getExpected(); + Map params = new HashMap<>(); + params.put("schemaName", change.getTable().getSchema()); + params.put("tableName", change.getTable().getName()); + params.put("columnName", expected.getName()); if (!Objects.equals(expected.getTypeName(), actual.getTypeName())) { - return String.format("ALTER TABLE %s.%s ALTER COLUMN %s SET DATA TYPE %s;", change.getTable().getSchema(), - change.getTable().getName(), - expected.getName(), - expected.getTypeName()); + params.put("dataType", expected.getTypeName()); + return TemplateEngine.process(COLUMN_ALTER_TYPE_TEMPLATE, params); } - if (!Objects.equals(expected.isNullable(), actual.isNullable())) { - if (expected.isNullable()) { - return String.format("ALTER TABLE %s.%s ALTER COLUMN %s DROP NOT NULL;", change.getTable().getSchema(), - change.getTable().getName(), - expected.getName()); - } else { - throw new RuntimeException("Operation not supported by BigQuery to alter column to not null!"); - } - } - log.info("No action taken for changes detected in column: {}.{}.{}", change.getTable().getSchema(), - change.getTable().getName(), - expected.getName()); return ""; } @Override public String dropColumn(ColumnChange change) { - return String.format("ALTER TABLE %s.%s DROP COLUMN %s;", change.getTable().getSchema(), change.getTable().getName(), change.getActual().getName()); - } + Table table = change.getTable(); + Column actual = change.getActual(); + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + params.put("tableName", table.getName()); + params.put("columnName", actual.getName()); + return TemplateEngine.process(COLUMN_DROP_TEMPLATE, params); + } @Override public String addColumn(ColumnChange change) { - String columnNameWithType = columnSQLDecoratorFactory.decoratorFor(change.getExpected()).expressSQl(); - return String.format("ALTER TABLE %s.%s ADD COLUMN %s;", change.getTable().getSchema(), change.getTable().getName(), columnNameWithType); + Table table = change.getTable(); + Column expected = change.getExpected(); + + Map params = new HashMap<>(); + params.put("schemaName", table.getSchema()); + params.put("tableName", table.getName()); + params.put("columnDefinition", columnSQLDecoratorFactory.decoratorFor(expected).expressSQl()); + return TemplateEngine.process(COLUMN_ADD_TEMPLATE, params); } @Override public String dropTable(Table actual) { - return String.format("DROP TABLE %s.%s;", actual.getSchema(), actual.getName()); + Map params = new HashMap<>(); + params.put("schemaName", actual.getSchema()); + params.put("tableName", actual.getName()); + return TemplateEngine.process(TABLE_DROP_TEMPLATE, params); } @Override @@ -209,4 +198,9 @@ public String alterView(View expected, View actual) { createParams.put("viewCode", expected.getCode()); return TemplateEngine.process(VIEW_ALTER_TEMPLATE, createParams); } + private String createSchema(String schema) { + Map params = new HashMap<>(); + params.put("schemaName", schema); + return TemplateEngine.process(SCHEMA_CREATE_TEMPLATE, params); + } } diff --git a/ddl/src/main/resources/templates/bigquerry/column/add.sqlt b/ddl/src/main/resources/templates/bigquerry/column/add.sqlt new file mode 100644 index 00000000..92700404 --- /dev/null +++ b/ddl/src/main/resources/templates/bigquerry/column/add.sqlt @@ -0,0 +1 @@ +ALTER TABLE `[(${schemaName})]`.`[(${tableName})]` ADD COLUMN [(${columnDefinition})]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/bigquerry/column/alter_column_null.sqlt b/ddl/src/main/resources/templates/bigquerry/column/alter_column_null.sqlt new file mode 100644 index 00000000..e69de29b diff --git a/ddl/src/main/resources/templates/bigquerry/column/alter_column_type.sqlt b/ddl/src/main/resources/templates/bigquerry/column/alter_column_type.sqlt new file mode 100644 index 00000000..4b4e823d --- /dev/null +++ b/ddl/src/main/resources/templates/bigquerry/column/alter_column_type.sqlt @@ -0,0 +1 @@ +ALTER TABLE `[(${schemaName})]`.`[(${tableName})]` ALTER COLUMN `[(${columnName})]` SET DATA TYPE [(${dataType})]; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/bigquerry/column/drop.sqlt b/ddl/src/main/resources/templates/bigquerry/column/drop.sqlt new file mode 100644 index 00000000..6875f3d7 --- /dev/null +++ b/ddl/src/main/resources/templates/bigquerry/column/drop.sqlt @@ -0,0 +1 @@ +ALTER TABLE `[(${schemaName})]`.`[(${tableName})]` DROP COLUMN `[(${columnName})]`; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/bigquerry/foreignkey/create.sqlt b/ddl/src/main/resources/templates/bigquerry/foreignkey/create.sqlt new file mode 100644 index 00000000..e69de29b diff --git a/ddl/src/main/resources/templates/bigquerry/foreignkey/drop.sqlt b/ddl/src/main/resources/templates/bigquerry/foreignkey/drop.sqlt new file mode 100644 index 00000000..e69de29b diff --git a/ddl/src/main/resources/templates/bigquerry/schema/create.sqlt b/ddl/src/main/resources/templates/bigquerry/schema/create.sqlt new file mode 100644 index 00000000..4a1126a7 --- /dev/null +++ b/ddl/src/main/resources/templates/bigquerry/schema/create.sqlt @@ -0,0 +1 @@ +CREATE SCHEMA IF NOT EXISTS `[(${schemaName})]`; \ No newline at end of file diff --git a/ddl/src/main/resources/templates/bigquerry/table/alter_add_primary_key.sqlt b/ddl/src/main/resources/templates/bigquerry/table/alter_add_primary_key.sqlt new file mode 100644 index 00000000..e69de29b diff --git a/ddl/src/main/resources/templates/bigquerry/table/alter_drop_primary_key.sqlt b/ddl/src/main/resources/templates/bigquerry/table/alter_drop_primary_key.sqlt new file mode 100644 index 00000000..e69de29b diff --git a/ddl/src/main/resources/templates/bigquerry/table/create.sqlt b/ddl/src/main/resources/templates/bigquerry/table/create.sqlt new file mode 100644 index 00000000..3d8aa0f3 --- /dev/null +++ b/ddl/src/main/resources/templates/bigquerry/table/create.sqlt @@ -0,0 +1 @@ +CREATE TABLE `[(${schemaName})]`.`[(${tableName})]`([(${tableCode})]); \ No newline at end of file diff --git a/ddl/src/main/resources/templates/bigquerry/table/drop.sqlt b/ddl/src/main/resources/templates/bigquerry/table/drop.sqlt new file mode 100644 index 00000000..12d3451b --- /dev/null +++ b/ddl/src/main/resources/templates/bigquerry/table/drop.sqlt @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `[(${schemaName})]`.`[(${tableName})]`; \ No newline at end of file diff --git a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java index 5cf675eb..f310a023 100644 --- a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java +++ b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java @@ -21,52 +21,52 @@ public class BigQueryDDLTest { @Test public void createDB() throws IOException { String ddl = generateDDL("clean_database"); - Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS halis;\r" + - "CREATE TABLE `halis`.`tableA`(`columnA` STRING, `columnB` INT64);\r" + - "CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);", ddl); + Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS `halis`;\n" + + "CREATE TABLE `halis`.`tableA`(`columnA` STRING, `columnB` INT64);\n" + + "CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);\n", ddl.replaceAll("[\\r\\n]+", "\n") + ""); } @Test public void addTable() throws IOException { String ddl = generateDDL("add_table"); - Assertions.assertEquals("CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);", ddl); + Assertions.assertEquals("CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);\n", ddl); } @Test public void addTable2() throws IOException { String ddl = generateDDL("add_table2"); - Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS new;\r" + - "CREATE TABLE `new`.`tableB`(`columnA` STRING, `columnB` INT64);", ddl); + Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS `new`;\r" + + "CREATE TABLE `new`.`tableB`(`columnA` STRING, `columnB` INT64);\n", ddl); } @Test public void dropTable() throws IOException { String ddl = generateDDL("drop_table"); - Assertions.assertEquals("DROP TABLE halis.tableB;", ddl); + Assertions.assertEquals("DROP TABLE IF EXISTS `halis`.`tableB`;", ddl); } @Test public void addColumn() throws IOException { String ddl = generateDDL("add_column"); - Assertions.assertEquals("ALTER TABLE halis.tableA ADD COLUMN `columnB` INT64;", ddl); + Assertions.assertEquals("ALTER TABLE `halis`.`tableA` ADD COLUMN `columnB` INT64;", ddl); } @Test public void dropColumn() throws IOException { String ddl = generateDDL("drop_column"); - Assertions.assertEquals("ALTER TABLE halis.tableA DROP COLUMN columnB;", ddl); + Assertions.assertEquals("ALTER TABLE `halis`.`tableA` DROP COLUMN `columnB`;", ddl); } @Test public void alterColumnDataType() throws IOException { String ddl = generateDDL("alter_column_data_type"); - Assertions.assertEquals("ALTER TABLE halis.tableA ALTER COLUMN columnB SET DATA TYPE STRING;", ddl); + Assertions.assertEquals("ALTER TABLE `halis`.`tableA` ALTER COLUMN `columnB` SET DATA TYPE STRING;", ddl); } @Test public void alterColumnToNullable() throws IOException { String ddl = generateDDL("alter_column_to_nullable"); - Assertions.assertEquals("ALTER TABLE halis.tableA ALTER COLUMN columnB DROP NOT NULL;", ddl); + Assertions.assertEquals("", ddl); } @Test @@ -87,6 +87,6 @@ private String generateDDL(String testType) throws IOException { Database expected = Utils.getDatabase(resourceDirectory.resolve(testType), "expected_model.yaml"); List> changes = new DefaultChangeFinder().findChanges(expected, actual); ChangeHandler handler = new ChangeHandlerImplementation(new BigQueryDDLGenerator(), null); - return handler.createDDLForChanges(changes); + return handler.createDDLForChanges(changes).replaceAll("(?m)^[ \t]*\r?\n", ""); } } From e832501643b95f30dca672ce3bac6654c202738e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cflakronademi=E2=80=9D?= Date: Tue, 11 Jul 2023 11:41:27 +0200 Subject: [PATCH 08/11] Added : BigQuery DDL Templates --- .../com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java index f310a023..511f5f5a 100644 --- a/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java +++ b/ddl/src/test/java/com/adaptivescale/rosetta/ddl/test/BigQueryDDLTest.java @@ -23,20 +23,20 @@ public void createDB() throws IOException { String ddl = generateDDL("clean_database"); Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS `halis`;\n" + "CREATE TABLE `halis`.`tableA`(`columnA` STRING, `columnB` INT64);\n" + - "CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);\n", ddl.replaceAll("[\\r\\n]+", "\n") + ""); + "CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);", ddl.replaceAll("[\\r\\n]+", "\n") + ""); } @Test public void addTable() throws IOException { String ddl = generateDDL("add_table"); - Assertions.assertEquals("CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);\n", ddl); + Assertions.assertEquals("CREATE TABLE `halis`.`tableB`(`columnA` STRING, `columnB` INT64);", ddl); } @Test public void addTable2() throws IOException { String ddl = generateDDL("add_table2"); Assertions.assertEquals("CREATE SCHEMA IF NOT EXISTS `new`;\r" + - "CREATE TABLE `new`.`tableB`(`columnA` STRING, `columnB` INT64);\n", ddl); + "CREATE TABLE `new`.`tableB`(`columnA` STRING, `columnB` INT64);", ddl); } @Test From ef8e4f0bf8f84222bcbede228abcac448a6b1b78 Mon Sep 17 00:00:00 2001 From: nbesimi Date: Fri, 14 Jul 2023 11:33:57 +0200 Subject: [PATCH 09/11] added: fallbackType to handle custom types or types that cannot be translated --- .../adaptivescale/rosetta/common/models/Column.java | 10 +++++----- .../rosetta/translator/DefaultTranslator.java | 11 +++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java b/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java index ae9ed78f..48169c6f 100644 --- a/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java +++ b/common/src/main/java/com/adaptivescale/rosetta/common/models/Column.java @@ -10,7 +10,7 @@ public class Column { private String label; private String description; private String typeName; - private String overwriteType; + private String fallbackType; private int ordinalPosition; private boolean isAutoincrement; private boolean isNullable; @@ -58,12 +58,12 @@ public void setTypeName(String typeName) { this.typeName = typeName; } - public String getOverwriteType() { - return overwriteType; + public String getFallbackType() { + return fallbackType; } - public void setOverwriteType(String overwriteType) { - this.overwriteType = overwriteType; + public void setFallbackType(String fallbackType) { + this.fallbackType = fallbackType; } public int getOrdinalPosition() { diff --git a/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java b/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java index 4983c5f4..5a372648 100644 --- a/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java +++ b/translator/src/main/java/com/adaptivescale/rosetta/translator/DefaultTranslator.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; public class DefaultTranslator implements Translator { @@ -42,14 +43,12 @@ private Table translateTable(Table table) { private Column translateColumn(Column column) { - String columnType = column.getOverwriteType(); - if (columnType == null) { - columnType = column.getTypeName(); - } - TranslationModel translationModel = TranslationMatrix.getInstance().findBySourceTypeAndSourceColumnTypeAndTargetType(sourceDatabaseName, columnType, targetDatabaseName); + TranslationModel translationModel = TranslationMatrix.getInstance().findBySourceTypeAndSourceColumnTypeAndTargetType(sourceDatabaseName, column.getTypeName(), targetDatabaseName); if (translationModel == null) { - throw new RuntimeException("There is no match for column name: " + column.getName() + " and type: " + column.getTypeName() + "."); + translationModel = Optional.ofNullable(column.getFallbackType()) + .map(it -> TranslationMatrix.getInstance().findBySourceTypeAndSourceColumnTypeAndTargetType(sourceDatabaseName, it, targetDatabaseName)) + .orElseThrow(() -> new RuntimeException("There is no match for column name: " + column.getName() + " and type: " + column.getTypeName() + ".")); } List translationAttributes = TranslationMatrix.getInstance().findByTranslationAttributesByTranslationIds(translationModel.getId()); From 4059b9809c6931df44a979bdd5e5529d7e60d7e4 Mon Sep 17 00:00:00 2001 From: nbesimi Date: Fri, 14 Jul 2023 11:38:36 +0200 Subject: [PATCH 10/11] updated: README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index df8576a9..b9ddaee8 100644 --- a/README.md +++ b/README.md @@ -662,6 +662,10 @@ In `model.yaml` you can find the attribute `safeMode` which is by default disabl In `model.yaml` you can find the attribute `operationLevel` which is by default set to `schema`. If you want to apply changes on to database level in your model instead of the specific schema in `apply` command, set `operationLevel: schema`. +### Fallback Type +In `model.yaml` you can define the attribute `fallbackType` for columns that are of custom types, not supported for translations or not included in the translation matrix. +If a given column type cannot be translated then the fallbackType will be used for the translation. `fallbackType` is optional. + ## RosettaDB CLI JAR and RosettaDB Source ### Setting Up the CLI JAR (Optional) From b851047f141bab43c9894db3dc59a0aa855a586b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:22:54 +0000 Subject: [PATCH 11/11] Version bump: to 2.1.0 --- build.gradle | 2 +- cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 9c7f1e33..2d49b4c4 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ repositories { allprojects { group = 'com.adaptivescale' - version = '2.0.6' + version = '2.1.0' sourceCompatibility = 11 targetCompatibility = 11 } diff --git a/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java b/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java index f930b9f9..4ddd5174 100644 --- a/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java +++ b/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java @@ -56,7 +56,7 @@ @Slf4j @CommandLine.Command(name = "cli", mixinStandardHelpOptions = true, - version = "2.0.6", + version = "2.1.0", description = "Declarative Database Management - DDL Transpiler" ) class Cli implements Callable {