@Test public void testExpandForCopyingTable() { CopyTable operation = SchemaOperations.copyTable("users", "customers"); changelog.addChangeSet("Michael de Jong", "Copying 'users' table to 'customers'.", operation); migrator.migrate(catalog, refLog, changelog.getLastAdded(), operation); String refId = refLog.getTableRef(changelog.getLastAdded(), "customers").getRefId(); Table ghostTable = catalog.getTable(refId); Table expectedGhostTable = new Table(refId) .addColumn(new Column("id", integer(), IDENTITY, NOT_NULL, AUTO_INCREMENT)) .addColumn(new Column("name", varchar(255), NOT_NULL)); Table originalTable = catalog.getTable("users"); Table expectedOriginalTable = new Table("users") .addColumn(new Column("id", integer(), IDENTITY, NOT_NULL, AUTO_INCREMENT)) .addColumn(new Column("name", varchar(255), NOT_NULL)); assertEquals(expectedGhostTable, ghostTable); assertEquals(expectedOriginalTable, originalTable); assertEquals("customers", refLog.getTableRefById(ghostTable.getName()).getName()); }
@Override public void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation) { String refId = RandomHasher.generateRefId(refLog); String sourceTableName = operation.getSourceTableName(); String targetTableName = operation.getTargetTableName(); refLog.fork(version); TableRef sourceTableRef = refLog.getTableRef(version.getParent(), sourceTableName); refLog.addTable(targetTableName, refId, version, sourceTableRef.getColumns().entrySet().stream() .map(Entry::getValue) .map(ColumnRef::ghost) .collect(Collectors.toList())); Table sourceTable = catalog.getTable(sourceTableRef.getRefId()); Table targetTable = sourceTable.copy().rename(refId); for (ForeignKey foreignKey : sourceTable.getForeignKeys()) { String referredRefId = foreignKey.getReferredTableName(); Table referredTable = catalog.getTable(referredRefId); targetTable.addForeignKey(foreignKey.getReferencingColumns()) .named(foreignKey.getForeignKeyName()) .onUpdate(foreignKey.getOnUpdate()) .onDelete(foreignKey.getOnDelete()) .referencing(referredTable, foreignKey.getReferredColumns()); } catalog.addTable(targetTable); }
CopyTableMigrator implements SchemaOperationMigrator<CopyTable> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation) { String refId = RandomHasher.generateRefId(refLog); String sourceTableName = operation.getSourceTableName(); String targetTableName = operation.getTargetTableName(); refLog.fork(version); TableRef sourceTableRef = refLog.getTableRef(version.getParent(), sourceTableName); refLog.addTable(targetTableName, refId, version, sourceTableRef.getColumns().entrySet().stream() .map(Entry::getValue) .map(ColumnRef::ghost) .collect(Collectors.toList())); Table sourceTable = catalog.getTable(sourceTableRef.getRefId()); Table targetTable = sourceTable.copy().rename(refId); for (ForeignKey foreignKey : sourceTable.getForeignKeys()) { String referredRefId = foreignKey.getReferredTableName(); Table referredTable = catalog.getTable(referredRefId); targetTable.addForeignKey(foreignKey.getReferencingColumns()) .named(foreignKey.getForeignKeyName()) .onUpdate(foreignKey.getOnUpdate()) .onDelete(foreignKey.getOnDelete()) .referencing(referredTable, foreignKey.getReferredColumns()); } catalog.addTable(targetTable); } }
CopyTableMigrator implements SchemaOperationMigrator<CopyTable> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation) { String refId = RandomHasher.generateRefId(refLog); String sourceTableName = operation.getSourceTableName(); String targetTableName = operation.getTargetTableName(); refLog.fork(version); TableRef sourceTableRef = refLog.getTableRef(version.getParent(), sourceTableName); refLog.addTable(targetTableName, refId, version, sourceTableRef.getColumns().entrySet().stream() .map(Entry::getValue) .map(ColumnRef::ghost) .collect(Collectors.toList())); Table sourceTable = catalog.getTable(sourceTableRef.getRefId()); Table targetTable = sourceTable.copy().rename(refId); for (ForeignKey foreignKey : sourceTable.getForeignKeys()) { String referredRefId = foreignKey.getReferredTableName(); Table referredTable = catalog.getTable(referredRefId); targetTable.addForeignKey(foreignKey.getReferencingColumns()) .named(foreignKey.getForeignKeyName()) .onUpdate(foreignKey.getOnUpdate()) .onDelete(foreignKey.getOnDelete()) .referencing(referredTable, foreignKey.getReferredColumns()); } catalog.addTable(targetTable); }  }
CopyTableMigrator implements SchemaOperationMigrator<CopyTable> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation) { String refId = RandomHasher.generateRefId(refLog); String sourceTableName = operation.getSourceTableName(); String targetTableName = operation.getTargetTableName(); refLog.fork(version); TableRef sourceTableRef = refLog.getTableRef(version.getParent(), sourceTableName); refLog.addTable(targetTableName, refId, version, sourceTableRef.getColumns().entrySet().stream() .map(Entry::getValue) .map(ColumnRef::ghost) .collect(Collectors.toList())); Table sourceTable = catalog.getTable(sourceTableRef.getRefId()); Table targetTable = sourceTable.copy().rename(refId); for (ForeignKey foreignKey : sourceTable.getForeignKeys()) { String referredRefId = foreignKey.getReferredTableName(); Table referredTable = catalog.getTable(referredRefId); targetTable.addForeignKey(foreignKey.getReferencingColumns()) .named(foreignKey.getForeignKeyName()) .onUpdate(foreignKey.getOnUpdate()) .onDelete(foreignKey.getOnDelete()) .referencing(referredTable, foreignKey.getReferredColumns()); } catalog.addTable(targetTable); }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation); }
CopyTableMigrator implements SchemaOperationMigrator<CopyTable> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation) { String refId = RandomHasher.generateRefId(refLog); String sourceTableName = operation.getSourceTableName(); String targetTableName = operation.getTargetTableName(); refLog.fork(version); TableRef sourceTableRef = refLog.getTableRef(version.getParent(), sourceTableName); refLog.addTable(targetTableName, refId, version, sourceTableRef.getColumns().entrySet().stream() .map(Entry::getValue) .map(ColumnRef::ghost) .collect(Collectors.toList())); Table sourceTable = catalog.getTable(sourceTableRef.getRefId()); Table targetTable = sourceTable.copy().rename(refId); for (ForeignKey foreignKey : sourceTable.getForeignKeys()) { String referredRefId = foreignKey.getReferredTableName(); Table referredTable = catalog.getTable(referredRefId); targetTable.addForeignKey(foreignKey.getReferencingColumns()) .named(foreignKey.getForeignKeyName()) .onUpdate(foreignKey.getOnUpdate()) .onDelete(foreignKey.getOnDelete()) .referencing(referredTable, foreignKey.getReferredColumns()); } catalog.addTable(targetTable); }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, CopyTable operation);  }