Search in sources :

Example 6 with TableChange

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

the class Db2DdlBuilder 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) {
            AddColumnChange addColumnChange = (AddColumnChange) change;
            /*
                 * DB2 does not allow the GENERATED BY DEFAULT AS IDENTITY
                 * clause in the ALTER TABLE ADD COLUMN statement, so we have to
                 * rebuild the table instead
                 */
            if (!addColumnChange.getNewColumn().isAutoIncrement()) {
                processChange(currentModel, desiredModel, addColumnChange, ddl);
                changeIt.remove();
            } else {
                return;
            }
        }
    }
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        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();
        }
    }
    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();
        }
    }
    for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        TableChange change = changeIt.next();
        if (change instanceof AddPrimaryKeyChange) {
            processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
            changeIt.remove();
        }
    }
    super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, ddl);
}
Also used : CopyColumnValueChange(org.jumpmind.db.alter.CopyColumnValueChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) PrimaryKeyChange(org.jumpmind.db.alter.PrimaryKeyChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) TableChange(org.jumpmind.db.alter.TableChange)

Example 7 with TableChange

use of org.jumpmind.db.alter.TableChange 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 8 with TableChange

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

the class OracleDdlBuilder 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) {
            AddColumnChange addColumnChange = (AddColumnChange) change;
            if (addColumnChange.getNewColumn().isRequired() && (addColumnChange.getNewColumn().getDefaultValue() == null)) {
                // we need to rebuild the full table
                return;
            }
        } else if (change instanceof ColumnSizeChange) {
            processChange(currentModel, desiredModel, (ColumnSizeChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof ColumnDefaultValueChange) {
            processChange(currentModel, desiredModel, (ColumnDefaultValueChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof ColumnRequiredChange) {
            processChange(currentModel, desiredModel, (ColumnRequiredChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof ColumnAutoIncrementChange) {
            if (processChange(currentModel, desiredModel, (ColumnAutoIncrementChange) change, ddl)) {
                changeIt.remove();
            }
        }
    }
    // 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);
        }
    }
    /*
         * Next we add/remove columns While Oracle has an ALTER TABLE MODIFY
         * statement, it is somewhat limited esp. if there is data in the table,
         * so we don't use it
         */
    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 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();
        }
    }
    // 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();
        }
    }
    super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, ddl);
}
Also used : ColumnDefaultValueChange(org.jumpmind.db.alter.ColumnDefaultValueChange) CopyColumnValueChange(org.jumpmind.db.alter.CopyColumnValueChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) PrimaryKeyChange(org.jumpmind.db.alter.PrimaryKeyChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) ColumnRequiredChange(org.jumpmind.db.alter.ColumnRequiredChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) ColumnAutoIncrementChange(org.jumpmind.db.alter.ColumnAutoIncrementChange) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) TableChange(org.jumpmind.db.alter.TableChange)

Example 9 with TableChange

use of org.jumpmind.db.alter.TableChange 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 10 with TableChange

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

Aggregations

TableChange (org.jumpmind.db.alter.TableChange)18 AddColumnChange (org.jumpmind.db.alter.AddColumnChange)15 RemoveColumnChange (org.jumpmind.db.alter.RemoveColumnChange)13 CopyColumnValueChange (org.jumpmind.db.alter.CopyColumnValueChange)12 AddPrimaryKeyChange (org.jumpmind.db.alter.AddPrimaryKeyChange)10 ArrayList (java.util.ArrayList)8 ColumnAutoIncrementChange (org.jumpmind.db.alter.ColumnAutoIncrementChange)7 PrimaryKeyChange (org.jumpmind.db.alter.PrimaryKeyChange)7 RemovePrimaryKeyChange (org.jumpmind.db.alter.RemovePrimaryKeyChange)7 ColumnDefaultValueChange (org.jumpmind.db.alter.ColumnDefaultValueChange)6 ColumnSizeChange (org.jumpmind.db.alter.ColumnSizeChange)6 ColumnChange (org.jumpmind.db.alter.ColumnChange)5 ColumnDataTypeChange (org.jumpmind.db.alter.ColumnDataTypeChange)5 Column (org.jumpmind.db.model.Column)5 ColumnRequiredChange (org.jumpmind.db.alter.ColumnRequiredChange)4 Map (java.util.Map)3 AddTableChange (org.jumpmind.db.alter.AddTableChange)3 RemoveTableChange (org.jumpmind.db.alter.RemoveTableChange)3 Table (org.jumpmind.db.model.Table)3 HashMap (java.util.HashMap)2