mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#553 - DDL - DB Migration with @History and Postgres needs to re-create the "with_history" view when columns dropped
This commit is contained in:
+29
-2
@@ -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.
|
||||
*/
|
||||
|
||||
+19
-1
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user