use of org.jumpmind.db.alter.RemoveForeignKeyChange in project symmetric-ds by JumpMind.
the class MsSql2000DdlBuilder method processChanges.
@Override
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) {
if (!changes.isEmpty()) {
writeQuotationOnStatement(ddl);
}
/*
* For column data type and size changes, we need to drop and then
* re-create indexes and foreign keys using the column, as well as any
* primary keys containg these columns However, if the index/foreign
* key/primary key is already slated for removal or change, then we
* don't want to generate change duplication
*/
HashSet<IIndex> removedIndexes = new HashSet<IIndex>();
HashSet<ForeignKey> removedForeignKeys = new HashSet<ForeignKey>();
HashSet<Table> removedPKs = new HashSet<Table>();
for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
IModelChange change = changeIt.next();
if (change instanceof RemoveIndexChange) {
removedIndexes.add(((RemoveIndexChange) change).getIndex());
} else if (change instanceof RemoveForeignKeyChange) {
removedForeignKeys.add(((RemoveForeignKeyChange) change).getForeignKey());
} else if (change instanceof RemovePrimaryKeyChange) {
removedPKs.add(((RemovePrimaryKeyChange) change).getChangedTable());
}
}
ArrayList<TableChange> additionalChanges = new ArrayList<TableChange>();
for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
IModelChange change = changeIt.next();
if ((change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange)) {
Column column = ((ColumnChange) change).getChangedColumn();
Table table = ((ColumnChange) change).getChangedTable();
if (column.isPrimaryKey() && !removedPKs.contains(table)) {
Column[] pk = table.getPrimaryKeyColumns();
additionalChanges.add(new RemovePrimaryKeyChange(table, pk));
additionalChanges.add(new AddPrimaryKeyChange(table, pk));
removedPKs.add(table);
}
for (int idx = 0; idx < table.getIndexCount(); idx++) {
IIndex index = table.getIndex(idx);
if (index.hasColumn(column) && !removedIndexes.contains(index)) {
additionalChanges.add(new RemoveIndexChange(table, index));
additionalChanges.add(new AddIndexChange(table, index));
removedIndexes.add(index);
}
}
for (int tableIdx = 0; tableIdx < currentModel.getTableCount(); tableIdx++) {
Table curTable = currentModel.getTable(tableIdx);
for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++) {
ForeignKey curFk = curTable.getForeignKey(fkIdx);
if ((curFk.hasLocalColumn(column) || curFk.hasForeignColumn(column)) && !removedForeignKeys.contains(curFk)) {
additionalChanges.add(new RemoveForeignKeyChange(curTable, curFk));
additionalChanges.add(new AddForeignKeyChange(curTable, curFk));
removedForeignKeys.add(curFk);
}
}
}
}
}
changes.addAll(additionalChanges);
super.processChanges(currentModel, desiredModel, changes, ddl);
}
Aggregations