@Test public void testExpandForAddingSingleColumn() { AddForeignKey operation = SchemaOperations.addForeignKey("posts", "author").referencing("users", "id"); changelog.addChangeSet("Michael de Jong", "Added 'date_of_birth' column to 'users' table.", operation); migrator.migrate(catalog, refLog, changelog.getLastAdded(), operation); Table usersTable = catalog.getTable("users"); 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)); expectedGhostTable.addForeignKey("author").referencing(usersTable, "id"); assertEquals(expectedGhostTable, ghostTable); }
@Override public void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation) { String tableName = operation.getReferringTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); String referencedTableName = operation.getReferencedTableName(); TableRef referencedTableRef = refLog.getTableRef(version, referencedTableName); Table referencedTable = catalog.getTable(referencedTableRef.getRefId()); table.addForeignKey(operation.getReferringColumnNames()) .named(operation.getName()) .onDelete(operation.getOnDelete()) .onUpdate(operation.getOnUpdate()) .referencing(referencedTable, operation.getReferencedColumnNames()); }
AddForeignKeyMigrator implements SchemaOperationMigrator<AddForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation) { String tableName = operation.getReferringTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); String referencedTableName = operation.getReferencedTableName(); TableRef referencedTableRef = refLog.getTableRef(version, referencedTableName); Table referencedTable = catalog.getTable(referencedTableRef.getRefId()); table.addForeignKey(operation.getReferringColumnNames()) .named(operation.getName()) .onDelete(operation.getOnDelete()) .onUpdate(operation.getOnUpdate()) .referencing(referencedTable, operation.getReferencedColumnNames()); } }
AddForeignKeyMigrator implements SchemaOperationMigrator<AddForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation) { String tableName = operation.getReferringTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); String referencedTableName = operation.getReferencedTableName(); TableRef referencedTableRef = refLog.getTableRef(version, referencedTableName); Table referencedTable = catalog.getTable(referencedTableRef.getRefId()); table.addForeignKey(operation.getReferringColumnNames()) .named(operation.getName()) .onDelete(operation.getOnDelete()) .onUpdate(operation.getOnUpdate()) .referencing(referencedTable, operation.getReferencedColumnNames()); }  }
AddForeignKeyMigrator implements SchemaOperationMigrator<AddForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation) { String tableName = operation.getReferringTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); String referencedTableName = operation.getReferencedTableName(); TableRef referencedTableRef = refLog.getTableRef(version, referencedTableName); Table referencedTable = catalog.getTable(referencedTableRef.getRefId()); table.addForeignKey(operation.getReferringColumnNames()) .named(operation.getName()) .onDelete(operation.getOnDelete()) .onUpdate(operation.getOnUpdate()) .referencing(referencedTable, operation.getReferencedColumnNames()); }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation); }
AddForeignKeyMigrator implements SchemaOperationMigrator<AddForeignKey> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation) { String tableName = operation.getReferringTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); String referencedTableName = operation.getReferencedTableName(); TableRef referencedTableRef = refLog.getTableRef(version, referencedTableName); Table referencedTable = catalog.getTable(referencedTableRef.getRefId()); table.addForeignKey(operation.getReferringColumnNames()) .named(operation.getName()) .onDelete(operation.getOnDelete()) .onUpdate(operation.getOnUpdate()) .referencing(referencedTable, operation.getReferencedColumnNames()); }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, AddForeignKey operation);  }