Search in sources :

Example 1 with AddForeignKeyChange

use of org.jumpmind.db.alter.AddForeignKeyChange 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);
}
Also used : IIndex(org.jumpmind.db.model.IIndex) ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) Table(org.jumpmind.db.model.Table) AddIndexChange(org.jumpmind.db.alter.AddIndexChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) ArrayList(java.util.ArrayList) IModelChange(org.jumpmind.db.alter.IModelChange) AddForeignKeyChange(org.jumpmind.db.alter.AddForeignKeyChange) RemoveForeignKeyChange(org.jumpmind.db.alter.RemoveForeignKeyChange) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) ForeignKey(org.jumpmind.db.model.ForeignKey) Column(org.jumpmind.db.model.Column) ColumnChange(org.jumpmind.db.alter.ColumnChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) RemoveIndexChange(org.jumpmind.db.alter.RemoveIndexChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) HashSet(java.util.HashSet) TableChange(org.jumpmind.db.alter.TableChange)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AddColumnChange (org.jumpmind.db.alter.AddColumnChange)1 AddForeignKeyChange (org.jumpmind.db.alter.AddForeignKeyChange)1 AddIndexChange (org.jumpmind.db.alter.AddIndexChange)1 AddPrimaryKeyChange (org.jumpmind.db.alter.AddPrimaryKeyChange)1 ColumnChange (org.jumpmind.db.alter.ColumnChange)1 ColumnDataTypeChange (org.jumpmind.db.alter.ColumnDataTypeChange)1 ColumnSizeChange (org.jumpmind.db.alter.ColumnSizeChange)1 IModelChange (org.jumpmind.db.alter.IModelChange)1 RemoveColumnChange (org.jumpmind.db.alter.RemoveColumnChange)1 RemoveForeignKeyChange (org.jumpmind.db.alter.RemoveForeignKeyChange)1 RemoveIndexChange (org.jumpmind.db.alter.RemoveIndexChange)1 RemovePrimaryKeyChange (org.jumpmind.db.alter.RemovePrimaryKeyChange)1 TableChange (org.jumpmind.db.alter.TableChange)1 Column (org.jumpmind.db.model.Column)1 ForeignKey (org.jumpmind.db.model.ForeignKey)1 IIndex (org.jumpmind.db.model.IIndex)1 Table (org.jumpmind.db.model.Table)1