Search in sources :

Example 6 with RemovePrimaryKeyChange

use of org.jumpmind.db.alter.RemovePrimaryKeyChange 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)

Example 7 with RemovePrimaryKeyChange

use of org.jumpmind.db.alter.RemovePrimaryKeyChange in project symmetric-ds by JumpMind.

the class MySqlDdlBuilder method processTableStructureChanges.

@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof AddColumnChange) {
            processChange(currentModel, desiredModel, (AddColumnChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof ColumnAutoIncrementChange) {
            /**
             * This has to happen before any primary key changes because if
             * a column is bring dropped as auto increment and being dropped
             * from the primary key, an auto increment column can't be a non
             * primary key column on mysql.
             */
            try {
                Column sourceColumn = ((ColumnAutoIncrementChange) change).getColumn();
                Column targetColumn = (Column) sourceColumn.clone();
                targetColumn.setAutoIncrement(!sourceColumn.isAutoIncrement());
                processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
                changeIt.remove();
            } catch (CloneNotSupportedException e) {
                log.error("", e);
            }
        }
    }
    List<Column> changedColumns = new ArrayList<Column>();
    // columns are present
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof RemoveColumnChange) {
            processChange(currentModel, desiredModel, (RemoveColumnChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof CopyColumnValueChange) {
            CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
            processChange(currentModel, desiredModel, copyColumnChange, ddl);
            changeIt.remove();
        } else if (change instanceof AddPrimaryKeyChange) {
            processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof PrimaryKeyChange) {
            processChange(currentModel, desiredModel, (PrimaryKeyChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof RemovePrimaryKeyChange) {
            processChange(currentModel, desiredModel, (RemovePrimaryKeyChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof ColumnChange) {
            /*
                 * we gather all changed columns because we can use the ALTER
                 * TABLE MODIFY COLUMN statement for them
                 */
            Column column = ((ColumnChange) change).getChangedColumn();
            if (!changedColumns.contains(column)) {
                changedColumns.add(column);
            }
            changeIt.remove();
        }
    }
    for (Iterator<Column> columnIt = changedColumns.iterator(); columnIt.hasNext(); ) {
        Column sourceColumn = columnIt.next();
        Column targetColumn = targetTable.findColumn(sourceColumn.getName(), delimitedIdentifierModeOn);
        processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
    }
}
Also used : CopyColumnValueChange(org.jumpmind.db.alter.CopyColumnValueChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) ArrayList(java.util.ArrayList) Column(org.jumpmind.db.model.Column) ColumnChange(org.jumpmind.db.alter.ColumnChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) PrimaryKeyChange(org.jumpmind.db.alter.PrimaryKeyChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) ColumnAutoIncrementChange(org.jumpmind.db.alter.ColumnAutoIncrementChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) TableChange(org.jumpmind.db.alter.TableChange)

Aggregations

AddColumnChange (org.jumpmind.db.alter.AddColumnChange)7 AddPrimaryKeyChange (org.jumpmind.db.alter.AddPrimaryKeyChange)7 RemoveColumnChange (org.jumpmind.db.alter.RemoveColumnChange)7 RemovePrimaryKeyChange (org.jumpmind.db.alter.RemovePrimaryKeyChange)7 TableChange (org.jumpmind.db.alter.TableChange)7 CopyColumnValueChange (org.jumpmind.db.alter.CopyColumnValueChange)6 PrimaryKeyChange (org.jumpmind.db.alter.PrimaryKeyChange)6 ArrayList (java.util.ArrayList)5 ColumnAutoIncrementChange (org.jumpmind.db.alter.ColumnAutoIncrementChange)5 ColumnChange (org.jumpmind.db.alter.ColumnChange)5 Column (org.jumpmind.db.model.Column)5 ColumnDefaultValueChange (org.jumpmind.db.alter.ColumnDefaultValueChange)3 ColumnSizeChange (org.jumpmind.db.alter.ColumnSizeChange)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ColumnDataTypeChange (org.jumpmind.db.alter.ColumnDataTypeChange)2 AddForeignKeyChange (org.jumpmind.db.alter.AddForeignKeyChange)1 AddIndexChange (org.jumpmind.db.alter.AddIndexChange)1