#553 - DDL - DB Migration with @History and Postgres needs to re-create the "with_history" view when columns dropped

This commit is contained in:
Robin Bygrave
2016-02-05 00:11:25 +13:00
parent a1bd0b469a
commit f65ea96b89
2 changed files with 48 additions and 3 deletions
@@ -191,6 +191,25 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
.endOfStatement().end();
}
/**
* Create or replace the with_history view with explicit columns.
*/
protected void createWithHistoryView(DdlBuffer apply, String baseTableName, List<String> columns) throws IOException {
apply.append("create or replace view ").append(baseTableName).append(viewSuffix).append(" as select ");
appendColumnNames(apply, columns, "");
appendSysPeriodColumns(apply, ", ");
apply.append(" from ").append(baseTableName).append(" union all select ");
appendColumnNames(apply, columns, "");
appendSysPeriodColumns(apply, ", ");
apply.append(" from ").append(baseTableName).append(historySuffix).endOfStatement().end();
}
protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) throws IOException {
appendColumnName(apply, prefix, sysPeriodStart);
appendColumnName(apply, prefix, sysPeriodEnd);
}
protected void dropHistoryTableEtc(DdlBuffer buffer, String baseTableName) throws IOException {
buffer.append("drop view ").append(baseTableName).append(viewSuffix).endOfStatement();
@@ -203,8 +222,6 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
buffer.append("alter table ").append(baseTableName).append(" drop column ").append(sysPeriodEnd).endOfStatement();
}
//protected abstract void addFunction(DdlBuffer apply, String procedureName, String historyTable, List<String> includedColumns) throws IOException;
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) throws IOException {
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
@@ -225,6 +242,16 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
}
}
/**
* Append a single column to the buffer if it is not null.
*/
protected void appendColumnName(DdlBuffer buffer, String prefix, String columnName) throws IOException {
if (columnName != null) {
buffer.append(prefix).append(columnName);
}
}
/**
* Return the list of included columns in order.
*/
@@ -45,6 +45,11 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
}
}
@Override
protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) throws IOException {
appendColumnName(apply, prefix, sysPeriod);
}
@Override
protected void dropSysPeriodColumns(DdlBuffer buffer, String baseTableName) throws IOException {
buffer.append("alter table ").append(baseTableName).append(" drop column ").append(sysPeriod).endOfStatement();
@@ -115,11 +120,12 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
if (update != null) {
apply.append("-- Regenerated ").append(procedureName).newLine();
apply.append("-- changes: ").append(update.description()).newLine();
recreateHistoryView(apply, table.getName(), includedColumns);
}
addFunction(apply, procedureName, historyTable, includedColumns);
if (update != null) {
// put a reverted version into the rollback buffer
update.toRevertedColumns(includedColumns);
@@ -128,10 +134,22 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
rollback.append("-- Revert regenerated ").append(procedureName).newLine();
rollback.append("-- revert changes: ").append(update.description()).newLine();
recreateHistoryView(rollback, table.getName(), includedColumns);
addFunction(rollback, procedureName, historyTable, includedColumns);
}
}
/**
* For postgres we need to drop and recreate the view. Well, we could add columns to the end of the view
* but otherwise we need to drop and create it.
*/
private void recreateHistoryView(DdlBuffer buffer, String baseTableName, List<String> includedColumns) throws IOException {
buffer.append("drop view if exists ").append(baseTableName).append(viewSuffix).endOfStatement();
createWithHistoryView(buffer, baseTableName, includedColumns);
}
@Override
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) throws IOException {