Search in sources :

Example 1 with ColumnDataTypeChange

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

the class AbstractDdlBuilder method processTableStructureChanges.

/**
     * Allows database-specific implementations to handle changes in a database
     * specific manner. Any handled change should be applied to the given
     * current model (which is a copy of the real original model) and be removed
     * from the list of changes.<br/>
     * In the default implementation, all {@link AddPrimaryKeyChange} changes
     * are applied via an <code>ALTER TABLE ADD CONSTRAINT</code> statement.
     *
     * @param currentModel
     *            The current database schema
     * @param desiredModel
     *            The desired database schema
     * @param sourceTable
     *            The original table
     * @param targetTable
     *            The desired table
     * @param changes
     *            The change objects for the target table
     */
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
    if (changes.size() == 1) {
        TableChange change = changes.get(0);
        if (change instanceof AddPrimaryKeyChange) {
            processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
            changes.clear();
        }
    }
    Iterator<TableChange> it = changes.iterator();
    while (it.hasNext()) {
        TableChange change = (TableChange) it.next();
        // Check to see if we can generate alters for type changes
        if (change instanceof ColumnDataTypeChange) {
            ColumnDataTypeChange typeChange = (ColumnDataTypeChange) change;
            if (typeChange.getNewTypeCode() == Types.BIGINT) {
                if (writeAlterColumnDataTypeToBigInt(typeChange, ddl)) {
                    it.remove();
                }
            }
        }
    }
}
Also used : ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) RemoveTableChange(org.jumpmind.db.alter.RemoveTableChange) TableChange(org.jumpmind.db.alter.TableChange) AddTableChange(org.jumpmind.db.alter.AddTableChange)

Example 2 with ColumnDataTypeChange

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

the class HsqlDb2DdlBuilder 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();
        // HsqlDb can only drop columns that are not part of a primary key
        if ((change instanceof RemoveColumnChange) && ((RemoveColumnChange) change).getColumn().isPrimaryKey()) {
            changeIt.remove();
        }
        // LONGVARCHAR columns always report changes
        if (change instanceof ColumnSizeChange) {
            ColumnSizeChange sizeChange = (ColumnSizeChange) change;
            if (sizeChange.getChangedColumn().getMappedTypeCode() == Types.VARCHAR && sizeChange.getNewSize() == 0) {
                changeIt.remove();
            }
        }
        if (change instanceof ColumnDataTypeChange) {
            ColumnDataTypeChange dataTypeChange = (ColumnDataTypeChange) change;
            // LONGVARCHAR columns always report changes
            if (dataTypeChange.getChangedColumn().getMappedTypeCode() == Types.VARCHAR && dataTypeChange.getNewTypeCode() == Types.LONGVARCHAR) {
                changeIt.remove();
            } else if (dataTypeChange.getNewTypeCode() == Types.BIGINT) {
                if (writeAlterColumnDataTypeToBigInt(dataTypeChange, ddl)) {
                    changeIt.remove();
                }
            }
        }
    }
    // in order to utilize the ALTER TABLE ADD COLUMN BEFORE statement
    // we have to apply the add column changes in the correct order
    // thus we first gather all add column changes and then execute them
    // Since we get them in target table column order, we can simply
    // iterate backwards
    ArrayList<AddColumnChange> addColumnChanges = new ArrayList<AddColumnChange>();
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof AddColumnChange) {
            addColumnChanges.add((AddColumnChange) change);
            changeIt.remove();
        }
    }
    for (ListIterator<AddColumnChange> changeIt = addColumnChanges.listIterator(addColumnChanges.size()); changeIt.hasPrevious(); ) {
        AddColumnChange addColumnChange = changeIt.previous();
        processChange(currentModel, desiredModel, addColumnChange, ddl);
        changeIt.remove();
    }
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof RemoveColumnChange) {
            RemoveColumnChange removeColumnChange = (RemoveColumnChange) change;
            processChange(currentModel, desiredModel, removeColumnChange, ddl);
            changeIt.remove();
        } else if (change instanceof CopyColumnValueChange) {
            CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
            processChange(currentModel, desiredModel, copyColumnChange, ddl);
            changeIt.remove();
        }
    }
}
Also used : ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) CopyColumnValueChange(org.jumpmind.db.alter.CopyColumnValueChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) ArrayList(java.util.ArrayList) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) TableChange(org.jumpmind.db.alter.TableChange)

Example 3 with ColumnDataTypeChange

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

the class FirebirdDialect1DdlBuilder method processTableStructureChanges.

protected void processTableStructureChanges(Database currentModel, Database desiredModel, Collection<TableChange> changes, StringBuilder ddl) {
    Iterator<TableChange> iter = changes.iterator();
    while (iter.hasNext()) {
        TableChange change = iter.next();
        if (change instanceof ColumnDataTypeChange) {
            ColumnDataTypeChange dataTypeChange = (ColumnDataTypeChange) change;
            if (dataTypeChange.getNewTypeCode() == Types.BIGINT && dataTypeChange.getChangedColumn().getMappedTypeCode() == Types.DOUBLE) {
                iter.remove();
            }
        } else if (change instanceof ColumnDefaultValueChange) {
            ColumnDefaultValueChange defaultValueChange = (ColumnDefaultValueChange) change;
            if (defaultValueChange.getChangedColumn().getMappedTypeCode() == Types.DOUBLE && new BigDecimal(defaultValueChange.getNewDefaultValue()).equals(new BigDecimal(defaultValueChange.getChangedColumn().getDefaultValue()))) {
                iter.remove();
            }
        }
    }
    super.processTableStructureChanges(currentModel, desiredModel, changes, ddl);
}
Also used : ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) ColumnDefaultValueChange(org.jumpmind.db.alter.ColumnDefaultValueChange) BigDecimal(java.math.BigDecimal) TableChange(org.jumpmind.db.alter.TableChange)

Example 4 with ColumnDataTypeChange

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

the class MsSql2000DdlBuilder 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 ColumnAutoIncrementChange) {
            /*
             * Sql Server has no way of adding or removing an IDENTITY
             * constraint thus we have to rebuild the table anyway and can
             * ignore all the other column changes
             */
            return;
        }
    }
    // First we drop primary keys as necessary
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof RemovePrimaryKeyChange) {
            processChange(currentModel, desiredModel, (RemovePrimaryKeyChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof PrimaryKeyChange) {
            PrimaryKeyChange pkChange = (PrimaryKeyChange) change;
            RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(), pkChange.getOldPrimaryKeyColumns());
            processChange(currentModel, desiredModel, removePkChange, ddl);
        }
    }
    ArrayList<ColumnChange> columnChanges = new ArrayList<ColumnChange>();
    // Next we add/remove columns
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof AddColumnChange) {
            AddColumnChange addColumnChange = (AddColumnChange) change;
            processChange(currentModel, desiredModel, addColumnChange, ddl);
            changeIt.remove();
        } else 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 ColumnChange) && (columnChanges != null)) {
            /*
                 * We gather all changed columns because we can use the ALTER
                 * TABLE ALTER COLUMN statement for them
                 */
            columnChanges.add((ColumnChange) change);
        }
    }
    if (columnChanges != null) {
        HashSet<Column> processedColumns = new HashSet<Column>();
        for (Iterator<ColumnChange> changeIt = columnChanges.iterator(); changeIt.hasNext(); ) {
            ColumnChange change = changeIt.next();
            Column sourceColumn = change.getChangedColumn();
            if (!sourceColumn.isPrimaryKey()) {
                Column targetColumn = targetTable.findColumn(sourceColumn.getName(), delimitedIdentifierModeOn);
                if (!processedColumns.contains(targetColumn)) {
                    processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, (change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange), ddl);
                    processedColumns.add(targetColumn);
                }
                changes.remove(change);
                change.apply(currentModel, delimitedIdentifierModeOn);
            } else {
                log.debug("Cannot alter a primay key column on sql server (azure).  Just let the table rebuild.");
            }
        }
    }
    // Finally we add primary keys
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof AddPrimaryKeyChange) {
            processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof PrimaryKeyChange) {
            PrimaryKeyChange pkChange = (PrimaryKeyChange) change;
            AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange(pkChange.getChangedTable(), pkChange.getNewPrimaryKeyColumns());
            processChange(currentModel, desiredModel, addPkChange, ddl);
            changeIt.remove();
        }
    }
}
Also used : ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) CopyColumnValueChange(org.jumpmind.db.alter.CopyColumnValueChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) ArrayList(java.util.ArrayList) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) ColumnChange(org.jumpmind.db.alter.ColumnChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) Column(org.jumpmind.db.model.Column) PrimaryKeyChange(org.jumpmind.db.alter.PrimaryKeyChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) 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) HashSet(java.util.HashSet)

Example 5 with ColumnDataTypeChange

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

ColumnDataTypeChange (org.jumpmind.db.alter.ColumnDataTypeChange)5 TableChange (org.jumpmind.db.alter.TableChange)5 ArrayList (java.util.ArrayList)3 AddColumnChange (org.jumpmind.db.alter.AddColumnChange)3 AddPrimaryKeyChange (org.jumpmind.db.alter.AddPrimaryKeyChange)3 ColumnSizeChange (org.jumpmind.db.alter.ColumnSizeChange)3 RemoveColumnChange (org.jumpmind.db.alter.RemoveColumnChange)3 HashSet (java.util.HashSet)2 ColumnChange (org.jumpmind.db.alter.ColumnChange)2 CopyColumnValueChange (org.jumpmind.db.alter.CopyColumnValueChange)2 RemovePrimaryKeyChange (org.jumpmind.db.alter.RemovePrimaryKeyChange)2 Column (org.jumpmind.db.model.Column)2 BigDecimal (java.math.BigDecimal)1 AddForeignKeyChange (org.jumpmind.db.alter.AddForeignKeyChange)1 AddIndexChange (org.jumpmind.db.alter.AddIndexChange)1 AddTableChange (org.jumpmind.db.alter.AddTableChange)1 ColumnAutoIncrementChange (org.jumpmind.db.alter.ColumnAutoIncrementChange)1 ColumnDefaultValueChange (org.jumpmind.db.alter.ColumnDefaultValueChange)1 IModelChange (org.jumpmind.db.alter.IModelChange)1 PrimaryKeyChange (org.jumpmind.db.alter.PrimaryKeyChange)1