@Test public void testExpandForDroppingForeignKey() { DropForeignKey operation = dropForeignKey("posts", "post_author"); changelog.addChangeSet("Michael de Jong", "Drop author foreign key from posts table.", operation); migrator.migrate(catalog, refLog, changelog.getLastAdded(), operation); Table originalTable = catalog.getTable("posts"); Table ghostTable = getGhostTable(originalTable); Table expectedGhostTable = new Table(ghostTable.getName()) .addColumn(new Column("id", integer(), IDENTITY, NOT_NULL, AUTO_INCREMENT)) .addColumn(new Column("author", integer(), NOT_NULL)); assertEquals(expectedGhostTable, ghostTable); }
@Override public void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); ForeignKey matchedForeignKey = table.getForeignKeys().stream() .filter(foreignKey -> foreignKey.getForeignKeyName().equals(operation.getForeignKeyName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No foreign key exists in table: " + tableName + " with name: " + operation.getForeignKeyName())); matchedForeignKey.drop(); }
DropForeignKeyMigrator implements SchemaOperationMigrator<DropForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); ForeignKey matchedForeignKey = table.getForeignKeys().stream() .filter(foreignKey -> foreignKey.getForeignKeyName().equals(operation.getForeignKeyName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No foreign key exists in table: " + tableName + " with name: " + operation.getForeignKeyName())); matchedForeignKey.drop(); } }
DropForeignKeyMigrator implements SchemaOperationMigrator<DropForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); ForeignKey matchedForeignKey = table.getForeignKeys().stream() .filter(foreignKey -> foreignKey.getForeignKeyName().equals(operation.getForeignKeyName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No foreign key exists in table: " + tableName + " with name: " + operation.getForeignKeyName())); matchedForeignKey.drop(); }  }
DropForeignKeyMigrator implements SchemaOperationMigrator<DropForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); ForeignKey matchedForeignKey = table.getForeignKeys().stream() .filter(foreignKey -> foreignKey.getForeignKeyName().equals(operation.getForeignKeyName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No foreign key exists in table: " + tableName + " with name: " + operation.getForeignKeyName())); matchedForeignKey.drop(); }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation); }
DropForeignKeyMigrator implements SchemaOperationMigrator<DropForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); ForeignKey matchedForeignKey = table.getForeignKeys().stream() .filter(foreignKey -> foreignKey.getForeignKeyName().equals(operation.getForeignKeyName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No foreign key exists in table: " + tableName + " with name: " + operation.getForeignKeyName())); matchedForeignKey.drop(); }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, DropForeignKey operation);  }