use of org.jumpmind.db.alter.IModelChange in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method alterDatabase.
/**
* Generates the DDL to modify an existing database so the schema matches
* the specified database schema by using drops, modifications and
* additions. Database-specific implementations can change aspect of this
* algorithm by redefining the individual methods that compromise it.
*/
public void alterDatabase(Database currentModel, Database desiredModel, StringBuilder ddl, IAlterDatabaseInterceptor... alterDatabaseInterceptors) {
currentModel = currentModel.copy();
mergeOrRemovePlatformTypes(currentModel, desiredModel);
ModelComparator comparator = new ModelComparator(databaseName, databaseInfo, caseSensitive);
List<IModelChange> detectedChanges = comparator.compare(currentModel, desiredModel);
if (alterDatabaseInterceptors != null) {
for (IAlterDatabaseInterceptor interceptor : alterDatabaseInterceptors) {
detectedChanges = interceptor.intercept(detectedChanges, currentModel, desiredModel);
}
}
processChanges(currentModel, desiredModel, detectedChanges, ddl);
}
use of org.jumpmind.db.alter.IModelChange in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method isAlterDatabase.
public boolean isAlterDatabase(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) {
ModelComparator comparator = new ModelComparator(databaseName, databaseInfo, caseSensitive);
List<IModelChange> detectedChanges = comparator.compare(currentModel, desiredModel);
if (alterDatabaseInterceptors != null) {
for (IAlterDatabaseInterceptor interceptor : alterDatabaseInterceptors) {
detectedChanges = interceptor.intercept(detectedChanges, currentModel, desiredModel);
}
}
return detectedChanges.size() > 0;
}
use of org.jumpmind.db.alter.IModelChange 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