Search in sources :

Example 11 with CopyColumnValueChange

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

Example 12 with CopyColumnValueChange

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

the class H2DdlBuilder 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;
            processChange(currentModel, desiredModel, addColumnChange, ddl);
            changeIt.remove();
        } else if (change instanceof CopyColumnValueChange) {
            CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
            processChange(currentModel, desiredModel, copyColumnChange, ddl);
            changeIt.remove();
        } else if (change instanceof RemoveColumnChange) {
            processChange(currentModel, desiredModel, (RemoveColumnChange) change, ddl);
            changeIt.remove();
        } else if (change instanceof ColumnDefaultValueChange) {
            ColumnDefaultValueChange defaultChange = (ColumnDefaultValueChange) change;
            defaultChange.getChangedColumn().setDefaultValue(defaultChange.getNewDefaultValue());
            writeAlterColumn(change.getChangedTable(), defaultChange.getChangedColumn(), ddl);
            changeIt.remove();
        } else if (change instanceof ColumnRequiredChange) {
            ColumnRequiredChange defaultChange = (ColumnRequiredChange) change;
            defaultChange.getChangedColumn().setRequired(!defaultChange.getChangedColumn().isRequired());
            writeAlterColumn(change.getChangedTable(), defaultChange.getChangedColumn(), ddl);
            changeIt.remove();
        } else if (change instanceof ColumnSizeChange) {
            ColumnSizeChange sizeChange = (ColumnSizeChange) change;
            sizeChange.getChangedColumn().setSizeAndScale(sizeChange.getNewSize(), sizeChange.getNewScale());
            writeAlterColumn(change.getChangedTable(), sizeChange.getChangedColumn(), ddl);
            changeIt.remove();
        } else if (change instanceof ColumnAutoIncrementChange) {
            ColumnAutoIncrementChange defaultChange = (ColumnAutoIncrementChange) change;
            defaultChange.getColumn().setAutoIncrement(!defaultChange.getColumn().isAutoIncrement());
            writeAlterColumn(change.getChangedTable(), defaultChange.getColumn(), ddl);
            changeIt.remove();
        }
    }
    super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, ddl);
}
Also used : CopyColumnValueChange(org.jumpmind.db.alter.CopyColumnValueChange) ColumnDefaultValueChange(org.jumpmind.db.alter.ColumnDefaultValueChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) ColumnRequiredChange(org.jumpmind.db.alter.ColumnRequiredChange) ColumnAutoIncrementChange(org.jumpmind.db.alter.ColumnAutoIncrementChange) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) TableChange(org.jumpmind.db.alter.TableChange)

Aggregations

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