use of org.jumpmind.db.alter.AddColumnChange 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();
}
}
}
use of org.jumpmind.db.alter.AddColumnChange in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method processTableStructureChanges.
/**
* Processes the changes to the structure of a single table.
* Database-specific implementations might redefine this method, but it is
* usually sufficient to redefine the
* {@link #processTableStructureChanges(Database, Database, Table, Table, Map, List)}
* method instead.
*/
protected void processTableStructureChanges(Database currentModel, Database desiredModel, String tableName, List<TableChange> changes, StringBuilder ddl) {
StringBuilder tableDdl = new StringBuilder();
Database originalModel = copy(currentModel);
Table sourceTable = findTable(originalModel, tableName);
Table targetTable = findTable(desiredModel, tableName);
// we're enforcing a full rebuild in case of the addition of a required
// column without a default value that is not autoincrement
boolean requiresFullRebuild = false;
for (Iterator<TableChange> changeIt = changes.iterator(); !requiresFullRebuild && changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
if (addColumnChange.getNewColumn().isRequired() && (addColumnChange.getNewColumn().getDefaultValue() == null) && !addColumnChange.getNewColumn().isAutoIncrement()) {
requiresFullRebuild = true;
}
}
}
if (!requiresFullRebuild) {
processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, tableDdl);
}
if (!changes.isEmpty()) {
// we can only copy the data if no required columns without default
// value and non-autoincrement have been added
boolean canMigrateData = true;
Table tempTable = getTemporaryTableFor(sourceTable);
for (Iterator<TableChange> it = changes.iterator(); canMigrateData && it.hasNext(); ) {
TableChange change = it.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
if (addColumnChange.getNewColumn().isRequired() && !addColumnChange.getNewColumn().isAutoIncrement() && (addColumnChange.getNewColumn().getDefaultValue() == null)) {
log.warn("Data cannot be retained in table " + change.getChangedTable().getName() + " because of the addition of the required column " + addColumnChange.getNewColumn().getName() + " . The data is backed up in " + tempTable + ", consider manually migrating the data back or dropping this temp table.");
canMigrateData = false;
}
}
}
Table realTargetTable = getRealTargetTableFor(desiredModel, sourceTable, targetTable);
dropTemporaryTable(tempTable, ddl);
createTemporaryTable(tempTable, ddl);
writeCopyDataStatement(sourceTable, tempTable, ddl);
/*
* Note that we don't drop the indices here because the DROP
* TABLE will take care of that Likewise, foreign keys have
* already been dropped as necessary
*/
dropTable(sourceTable, ddl, false, true);
createTable(realTargetTable, ddl, false, true);
if (canMigrateData) {
writeCopyDataStatement(tempTable, targetTable, ddl);
dropTemporaryTable(tempTable, ddl);
writeFixLastIdentityValues(targetTable, ddl);
}
} else {
ddl.append(tableDdl);
}
}
use of org.jumpmind.db.alter.AddColumnChange 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);
}
use of org.jumpmind.db.alter.AddColumnChange in project symmetric-ds by JumpMind.
the class HsqlDbDdlBuilder method processTableStructureChanges.
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
// HsqlDb can only drop columns that are not part of a primary key
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if ((change instanceof RemoveColumnChange) && ((RemoveColumnChange) change).getColumn().isPrimaryKey()) {
return;
}
}
// 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 = (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();
}
}
}
use of org.jumpmind.db.alter.AddColumnChange in project symmetric-ds by JumpMind.
the class InterbaseDdlBuilder method processTableStructureChanges.
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
// TODO: Dropping of primary keys is currently not supported because we
// cannot
// determine the pk constraint names and drop them in one go
// (We could used a stored procedure if Interbase would allow them to
// use DDL)
// This will be easier once named primary keys are supported
boolean pkColumnAdded = false;
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
// add a new one afterwards which is not supported yet
if (addColumnChange.getNewColumn().isPrimaryKey()) {
pkColumnAdded = true;
} else {
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
}
} else if (change instanceof RemoveColumnChange) {
RemoveColumnChange removeColumnChange = (RemoveColumnChange) change;
// add a new one afterwards which is not supported yet
if (!removeColumnChange.getColumn().isPrimaryKey()) {
processChange(currentModel, desiredModel, removeColumnChange, 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();
// i.e. none was added during this alteration
if ((change instanceof AddPrimaryKeyChange) && !pkColumnAdded) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
}
}
}
Aggregations