#369 - Rollback of base column attributes

This commit is contained in:
Robin Bygrave
2015-08-18 08:02:37 +12:00
parent ad97ac2be9
commit bbf469bd54
3 changed files with 35 additions and 10 deletions
@@ -603,6 +603,22 @@ public class BaseTableDdl implements TableDdl {
String ddl = platformDdl.alterColumnBaseAttributes(alter);
if (hasValue(ddl)) {
writer.apply().append(ddl).endOfStatement();
// reverse and generate the rollback statement
String currentType = alter.getCurrentType();
String type = alter.getType();
Boolean currentNotnull = alter.isCurrentNotnull();
Boolean notnull = alter.isNotnull();
alter.setCurrentType(type);
alter.setType(currentType);
alter.setNotnull(currentNotnull);
alter.setCurrentNotnull(notnull);
// write the rollback
ddl = platformDdl.alterColumnBaseAttributes(alter);
writer.rollback().append(ddl).endOfStatement();
if (isTrue(alter.isWithHistory()) && alter.getType() != null) {
// mysql and sql server column type change allowing nulls in the history table column
AlterColumn alterHistoryColumn = new AlterColumn();
@@ -610,7 +626,14 @@ public class BaseTableDdl implements TableDdl {
alterHistoryColumn.setColumnName(alter.getColumnName());
alterHistoryColumn.setType(alter.getType());
String histColumnDdl = platformDdl.alterColumnBaseAttributes(alterHistoryColumn);
// write the apply to history table
writer.apply().append(histColumnDdl).endOfStatement();
// write the rollback from history table
alterHistoryColumn.setType(currentType);
histColumnDdl = platformDdl.alterColumnBaseAttributes(alterHistoryColumn);
writer.rollback().append(histColumnDdl).endOfStatement();
}
}
}
@@ -628,6 +651,8 @@ public class BaseTableDdl implements TableDdl {
String ddl = platformDdl.alterColumnNotnull(alter.getTableName(), alter.getColumnName(), alter.isNotnull());
if (hasValue(ddl)) {
writer.apply().append(ddl).endOfStatement();
ddl = platformDdl.alterColumnNotnull(alter.getTableName(), alter.getColumnName(), alter.isCurrentNotnull());
writer.rollback().append(ddl).endOfStatement();
}
}
@@ -636,10 +661,14 @@ public class BaseTableDdl implements TableDdl {
String ddl = platformDdl.alterColumnType(alter.getTableName(), alter.getColumnName(), alter.getType());
if (hasValue(ddl)) {
writer.apply().append(ddl).endOfStatement();
ddl = platformDdl.alterColumnType(alter.getTableName(), alter.getColumnName(), alter.getCurrentType());
writer.rollback().append(ddl).endOfStatement();
if (isTrue(alter.isWithHistory())) {
// apply same type change to matching column in the history table
ddl = platformDdl.alterColumnType(historyTable(alter.getTableName()), alter.getColumnName(), alter.getType());
writer.apply().append(ddl).endOfStatement();
ddl = platformDdl.alterColumnType(historyTable(alter.getTableName()), alter.getColumnName(), alter.getCurrentType());
writer.rollback().append(ddl).endOfStatement();
}
}
}
@@ -66,7 +66,6 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
addBeforeUpdate(rollback, updateTriggerName(baseTableName), baseTableName, historyTableName, includedColumns);
addBeforeDelete(rollback, deleteTriggerName(baseTableName), baseTableName, historyTableName, includedColumns);
rollback.append("unlock tables").endOfStatement();
}
private void addBeforeUpdate(DdlBuffer apply, String triggerName, String baseTable, String historyTable, List<String> includedColumns) throws IOException {
@@ -77,7 +76,7 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
.append(" for each row begin").newLine();
appendInsertIntoHistory(apply, historyTable, includedColumns);
apply
.append(" set NEW.").append(sysPeriod).append("_start = now(6)").endOfStatement().newLine()
.append(" set NEW.").append(sysPeriod).append("_start = now(6)").endOfStatement()
.append("end$$").newLine();
}
@@ -230,19 +230,18 @@ public class MColumn {
// set to null and check at the end
this.alterColumn = null;
boolean changeType = false;
boolean changeNotnull = false;
boolean changeBaseAttribute = false;
if (historyExclude != newColumn.historyExclude) {
getAlterColumn(tableName, tableWithHistory).setHistoryExclude(newColumn.historyExclude);
}
if (different(type, newColumn.type)) {
changeType = true;
changeBaseAttribute = true;
getAlterColumn(tableName, tableWithHistory).setType(newColumn.type);
}
if (notnull != newColumn.notnull) {
changeNotnull = true;
changeBaseAttribute = true;
getAlterColumn(tableName, tableWithHistory).setNotnull(newColumn.notnull);
}
if (different(defaultValue, newColumn.defaultValue)) {
@@ -302,11 +301,9 @@ public class MColumn {
if (alterColumn != null) {
modelDiff.addAlterColumn(alterColumn);
// we need the current type, notnull together for some db's
if (!changeType) {
if (changeBaseAttribute) {
// support reverting these changes
alterColumn.setCurrentType(type);
}
if (!changeNotnull) {
alterColumn.setCurrentNotnull(notnull);
}
}