mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactored DbTriggerBasedHistory: always use historyTableName
This commit is contained in:
1 parent
327babbfa2
commit
3db9a17c6a
2 files changed
+23
-18
No files matched your search
+16
-18
@@ -127,11 +127,15 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String historyTableName(String baseTableName) {
|
protected String historyTableName(String baseTableName) {
|
||||||
return baseTableName + historySuffix;
|
return normalise(baseTableName) + historySuffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String historyViewName(String baseTableName) {
|
||||||
|
return normalise(baseTableName) + viewSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String procedureName(String baseTableName) {
|
protected String procedureName(String baseTableName) {
|
||||||
return baseTableName + "_history_version";
|
return normalise(baseTableName) + "_history_version";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String triggerName(String baseTableName) {
|
protected String triggerName(String baseTableName) {
|
||||||
@@ -146,16 +150,6 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
|||||||
return normalise(baseTableName) + "_history_del";
|
return normalise(baseTableName) + "_history_del";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addHistoryTable(DdlWrite writer, MTable table, String whenCreatedColumn) {
|
|
||||||
|
|
||||||
String baseTableName = table.getName();
|
|
||||||
|
|
||||||
|
|
||||||
addSysPeriodColumns(writer, baseTableName, whenCreatedColumn);
|
|
||||||
createHistoryTable(writer.applyPostAlter(), table);
|
|
||||||
createWithHistoryView(writer.applyPostAlter(), baseTableName);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void addSysPeriodColumns(DdlWrite writer, String baseTableName, String whenCreatedColumn) {
|
protected void addSysPeriodColumns(DdlWrite writer, String baseTableName, String whenCreatedColumn) {
|
||||||
|
|
||||||
platformDdl.alterTableAddColumn(writer, baseTableName, sysPeriodStart, sysPeriodType, now);
|
platformDdl.alterTableAddColumn(writer, baseTableName, sysPeriodStart, sysPeriodType, now);
|
||||||
@@ -172,13 +166,17 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void createHistoryTableAs(DdlBuffer apply, MTable table) {
|
protected void createHistoryTableAs(DdlBuffer apply, MTable table) {
|
||||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(table.getName()).append(historySuffix).append("(").newLine();
|
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(historyTableName(table.getName())).append("(").newLine();
|
||||||
for (MColumn column : table.allColumns()) {
|
for (MColumn column : table.allColumns()) {
|
||||||
if (!column.isDraftOnly()) {
|
if (!column.isDraftOnly()) {
|
||||||
writeColumnDefinition(apply, column.getName(), column.getType());
|
writeColumnDefinition(apply, column.getName(), column.getType());
|
||||||
apply.append(",").newLine();
|
apply.append(",").newLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO: We must apply also pending dropped columns. Let's do that in a later step
|
||||||
|
if (table.hasDroppedColumns()) {
|
||||||
|
throw new IllegalStateException(table.getName() + " has dropped columns. Please generate drop script before enabling history");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void createHistoryTableWithPeriod(DdlBuffer apply) {
|
protected void createHistoryTableWithPeriod(DdlBuffer apply) {
|
||||||
@@ -202,9 +200,9 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
|||||||
protected void createWithHistoryView(DdlBuffer apply, String baseTableName) {
|
protected void createWithHistoryView(DdlBuffer apply, String baseTableName) {
|
||||||
|
|
||||||
apply
|
apply
|
||||||
.append("create view ").append(baseTableName).append(viewSuffix)
|
.append("create view ").append(historyViewName(baseTableName))
|
||||||
.append(" as select * from ").append(baseTableName)
|
.append(" as select * from ").append(baseTableName)
|
||||||
.append(" union all select * from ").append(baseTableName).append(historySuffix)
|
.append(" union all select * from ").append(historyTableName(baseTableName))
|
||||||
.endOfStatement();
|
.endOfStatement();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,11 +212,11 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void dropWithHistoryView(DdlBuffer apply, String baseTableName) {
|
protected void dropWithHistoryView(DdlBuffer apply, String baseTableName) {
|
||||||
apply.append("drop view ").append(baseTableName).append(viewSuffix).endOfStatement();
|
apply.append("drop view ").append(historyViewName(baseTableName)).endOfStatement();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void dropHistoryTable(DdlBuffer apply, String baseTableName) {
|
protected void dropHistoryTable(DdlBuffer apply, String baseTableName) {
|
||||||
apply.append("drop table ").append(baseTableName).append(historySuffix).endOfStatement().end();
|
apply.append("drop table ").append(historyTableName(baseTableName)).endOfStatement().end();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void dropSysPeriodColumns(DdlWrite writer, String baseTableName) {
|
protected void dropSysPeriodColumns(DdlWrite writer, String baseTableName) {
|
||||||
@@ -228,7 +226,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
|||||||
|
|
||||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String baseTable, List<String> columns) {
|
protected void appendInsertIntoHistory(DdlBuffer buffer, String baseTable, List<String> columns) {
|
||||||
|
|
||||||
buffer.append(" insert into ").append(baseTable).append(historySuffix).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
|
buffer.append(" insert into ").append(historyTableName(baseTable)).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
|
||||||
appendColumnNames(buffer, columns, "");
|
appendColumnNames(buffer, columns, "");
|
||||||
buffer.append(") values (OLD.").append(sysPeriodStart).append(", ").append(sysPeriodEndValue).append(",");
|
buffer.append(") values (OLD.").append(sysPeriodStart).append(", ").append(sysPeriodEndValue).append(",");
|
||||||
appendColumnNames(buffer, columns, "OLD.");
|
appendColumnNames(buffer, columns, "OLD.");
|
||||||
|
|||||||
@@ -455,6 +455,13 @@ public class MTable {
|
|||||||
return columnNames;
|
return columnNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true, if there are pending dropped columns.
|
||||||
|
*/
|
||||||
|
public boolean hasDroppedColumns() {
|
||||||
|
return !droppedColumns.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all the columns (excluding columns marked as dropped).
|
* Return all the columns (excluding columns marked as dropped).
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in new issue
Block a user