mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactored Hisotry generation
This commit is contained in:
1 parent
0ea375b4fc
commit
cfad20c8e1
21 files changed
+181
-700
No files matched your search
-30
@@ -38,10 +38,6 @@ public class DdlWrite {
|
||||
|
||||
private final DdlBuffer applyForeignKeys = new BaseDdlBuffer();
|
||||
|
||||
private final DdlBuffer applyHistoryView = new BaseDdlBuffer();
|
||||
|
||||
private final DdlBuffer applyHistoryTrigger = new BaseDdlBuffer();
|
||||
|
||||
private final DdlBuffer dropAllForeignKeys = new BaseDdlBuffer();
|
||||
|
||||
private final DdlBuffer dropAll = new BaseDdlBuffer();
|
||||
@@ -89,8 +85,6 @@ public class DdlWrite {
|
||||
&& applyAlterTables.isEmpty()
|
||||
&& applyPostAlter.getBuffer().isEmpty()
|
||||
&& applyForeignKeys.getBuffer().isEmpty()
|
||||
&& applyHistoryView.getBuffer().isEmpty()
|
||||
&& applyHistoryTrigger.getBuffer().isEmpty()
|
||||
&& applyDropDependencies.getBuffer().isEmpty();
|
||||
}
|
||||
|
||||
@@ -139,20 +133,6 @@ public class DdlWrite {
|
||||
return applyForeignKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer that apply history-view DDL is written to.
|
||||
*/
|
||||
public DdlBuffer applyHistoryView() {
|
||||
return applyHistoryView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer that apply history-trigger DDL is written to.
|
||||
*/
|
||||
public DdlBuffer applyHistoryTrigger() {
|
||||
return applyHistoryTrigger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer used for the 'drop all DDL' for dropping foreign keys and associated indexes.
|
||||
*/
|
||||
@@ -193,16 +173,6 @@ public class DdlWrite {
|
||||
target.append("-- foreign keys and indices\n");
|
||||
target.append(applyForeignKeys.getBuffer());
|
||||
}
|
||||
|
||||
if (!applyHistoryView.isEmpty()) {
|
||||
target.append("-- apply history view\n");
|
||||
target.append(applyHistoryView.getBuffer());
|
||||
}
|
||||
|
||||
if (!applyHistoryTrigger.isEmpty()) {
|
||||
target.append("-- apply history trigger\n");
|
||||
target.append(applyHistoryTrigger.getBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
-8
@@ -1,15 +1,12 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.config.dbplatform.DbPlatformType;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -75,11 +72,6 @@ public abstract class AbstractHanaDdl extends PlatformDdl {
|
||||
alterTable(writer, tableName).append(alterColumn, columnName).append(type).append(defaultValueClause).append(notnullClause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DdlHandler createDdlHandler(DatabaseConfig config) {
|
||||
return new HanaDdlHandler(config, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String convertArrayType(String logicalArrayType) {
|
||||
Matcher matcher = ARRAY_PATTERN.matcher(logicalArrayType);
|
||||
|
||||
+48
-57
@@ -33,9 +33,7 @@ import io.ebeaninternal.server.deploy.IdentityMode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.ebean.util.StringHelper.replace;
|
||||
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
|
||||
@@ -46,12 +44,6 @@ import static io.ebeaninternal.dbmigration.ddlgeneration.platform.SplitColumns.s
|
||||
*/
|
||||
public class BaseTableDdl implements TableDdl {
|
||||
|
||||
enum HistorySupport {
|
||||
NONE,
|
||||
SQL2011,
|
||||
TRIGGER_BASED
|
||||
}
|
||||
|
||||
protected final DbConstraintNaming naming;
|
||||
|
||||
protected final NamingConvention namingConvention;
|
||||
@@ -67,15 +59,9 @@ public class BaseTableDdl implements TableDdl {
|
||||
|
||||
protected final List<UniqueConstraint> externalCompoundUnique = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Base tables that have associated history tables that need their triggers/functions regenerated as
|
||||
* columns have been added, removed, included or excluded.
|
||||
*/
|
||||
protected final Map<String, HistoryTableUpdate> regenerateHistoryTriggers = new LinkedHashMap<>();
|
||||
|
||||
private final boolean strictMode;
|
||||
|
||||
private final HistorySupport historySupport;
|
||||
private final boolean alterHistoryTables;
|
||||
|
||||
/**
|
||||
* Helper class that is used to execute the migration ddl before and after the migration action.
|
||||
@@ -196,9 +182,9 @@ public class BaseTableDdl implements TableDdl {
|
||||
this.strictMode = config.isDdlStrictMode();
|
||||
DbHistorySupport hist = platformDdl.getPlatform().getHistorySupport();
|
||||
if (hist == null) {
|
||||
this.historySupport = HistorySupport.NONE;
|
||||
this.alterHistoryTables = false;
|
||||
} else {
|
||||
this.historySupport = hist.isStandardsBased() ? HistorySupport.SQL2011 : HistorySupport.TRIGGER_BASED;
|
||||
this.alterHistoryTables = platformDdl.historyDdl.alterHistoryTables();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -622,15 +608,6 @@ public class BaseTableDdl implements TableDdl {
|
||||
*/
|
||||
@Override
|
||||
public void generateEpilog(DdlWrite writer) {
|
||||
if (!regenerateHistoryTriggers.isEmpty()) {
|
||||
platformDdl.lockTables(writer.applyHistoryTrigger(), regenerateHistoryTriggers.keySet());
|
||||
|
||||
for (HistoryTableUpdate update : this.regenerateHistoryTriggers.values()) {
|
||||
platformDdl.regenerateHistoryTriggers(writer, update);
|
||||
}
|
||||
|
||||
platformDdl.unlockTables(writer.applyHistoryTrigger(), regenerateHistoryTriggers.keySet());
|
||||
}
|
||||
platformDdl.generateEpilog(writer);
|
||||
}
|
||||
|
||||
@@ -651,12 +628,14 @@ public class BaseTableDdl implements TableDdl {
|
||||
for (Column column : columns) {
|
||||
alterTableAddColumn(writer, tableName, column, false, isTrue(addColumn.isWithHistory()));
|
||||
}
|
||||
if (isTrue(addColumn.isWithHistory()) && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
if (isTrue(addColumn.isWithHistory())) {
|
||||
platformDdl.regenerateHistoryTriggers(writer, tableName);
|
||||
// make same changes to the history table
|
||||
String historyTable = historyTable(tableName);
|
||||
for (Column column : columns) {
|
||||
regenerateHistoryTriggers(tableName, HistoryTableUpdate.Change.ADD, column.getName());
|
||||
alterTableAddColumn(writer, historyTable, column, true, true);
|
||||
if (alterHistoryTables) {
|
||||
String historyTable = historyTable(tableName);
|
||||
for (Column column : columns) {
|
||||
alterTableAddColumn(writer, historyTable, column, true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Column column : columns) {
|
||||
@@ -690,10 +669,12 @@ public class BaseTableDdl implements TableDdl {
|
||||
String tableName = dropColumn.getTableName();
|
||||
alterTableDropColumn(writer, tableName, dropColumn.getColumnName());
|
||||
|
||||
if (isTrue(dropColumn.isWithHistory()) && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
if (isTrue(dropColumn.isWithHistory())) {
|
||||
platformDdl.regenerateHistoryTriggers(writer, tableName);
|
||||
// also drop from the history table
|
||||
regenerateHistoryTriggers(tableName, HistoryTableUpdate.Change.DROP, dropColumn.getColumnName());
|
||||
alterTableDropColumn(writer, historyTable(tableName), dropColumn.getColumnName());
|
||||
if (alterHistoryTables) {
|
||||
alterTableDropColumn(writer, historyTable(tableName), dropColumn.getColumnName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -705,10 +686,11 @@ public class BaseTableDdl implements TableDdl {
|
||||
DdlMigrationHelp ddlHelp = new DdlMigrationHelp(alterColumn);
|
||||
ddlHelp.write(writer);
|
||||
|
||||
if (isTrue(alterColumn.isHistoryExclude())) {
|
||||
regenerateHistoryTriggers(alterColumn.getTableName(), HistoryTableUpdate.Change.EXCLUDE, alterColumn.getColumnName());
|
||||
} else if (isFalse(alterColumn.isHistoryExclude())) {
|
||||
regenerateHistoryTriggers(alterColumn.getTableName(), HistoryTableUpdate.Change.INCLUDE, alterColumn.getColumnName());
|
||||
if (alterColumn.isHistoryExclude() != null) {
|
||||
// CHECKME should we drop a small comment, if historyExclude changes, as not all DB systems will support this
|
||||
// writer.apply().append("-- historyExclude of ").append(alterColumn.getTableName()).append(".").append(alterColumn.getColumnName())
|
||||
// .append(" altered to ").append(String.valueOf(alterColumn.isHistoryExclude())).end();
|
||||
platformDdl.regenerateHistoryTriggers(writer, alterColumn.getTableName());
|
||||
}
|
||||
|
||||
if (hasValue(alterColumn.getDropForeignKey())) {
|
||||
@@ -765,30 +747,39 @@ public class BaseTableDdl implements TableDdl {
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the base table that we need to regenerate the history triggers on.
|
||||
*/
|
||||
protected void regenerateHistoryTriggers(String baseTableName, HistoryTableUpdate.Change change, String column) {
|
||||
HistoryTableUpdate update = regenerateHistoryTriggers.computeIfAbsent(baseTableName, HistoryTableUpdate::new);
|
||||
update.add(change, column);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is mysql specific - alter all the base attributes of the column together.
|
||||
* Will be called, if there is a type, dbdefault or notnull change.
|
||||
* alter all the base attributes (type/default/notnull) of the column together.
|
||||
* Some platforms (like mysql/sqlserver/hana) must do that in one statement,
|
||||
* other platforms may use several statements for altering one of the base
|
||||
* attributes.
|
||||
*/
|
||||
protected void alterColumn(DdlWrite writer, AlterColumn alter) {
|
||||
platformDdl.alterColumn(writer, alter);
|
||||
|
||||
if (isTrue(alter.isWithHistory()) && alter.getType() != null && historySupport == HistorySupport.TRIGGER_BASED) {
|
||||
// mysql and sql server column type change allowing nulls in the history table column
|
||||
regenerateHistoryTriggers(alter.getTableName(), HistoryTableUpdate.Change.ALTER, alter.getColumnName());
|
||||
AlterColumn alterHistoryColumn = new AlterColumn();
|
||||
alterHistoryColumn.setTableName(historyTable(alter.getTableName()));
|
||||
alterHistoryColumn.setColumnName(alter.getColumnName());
|
||||
alterHistoryColumn.setType(alter.getType());
|
||||
platformDdl.alterColumn(writer, alterHistoryColumn);
|
||||
if (isTrue(alter.isWithHistory())) {
|
||||
// we will apply only type changes or notNull -> null transition
|
||||
boolean isNull = false; // TODO: Boolean.FALSE.equals(alter.isNotnull());
|
||||
boolean applyToHistory = alter.getType() != null || isNull;
|
||||
if (applyToHistory) {
|
||||
platformDdl.regenerateHistoryTriggers(writer, alter.getTableName());
|
||||
if (alterHistoryTables) {
|
||||
AlterColumn alterHistoryColumn = new AlterColumn();
|
||||
alterHistoryColumn.setTableName(historyTable(alter.getTableName()));
|
||||
alterHistoryColumn.setColumnName(alter.getColumnName());
|
||||
// ignore default value (not needed on history tables)
|
||||
alterHistoryColumn.setCurrentType(alter.getCurrentType());
|
||||
alterHistoryColumn.setType(alter.getType());
|
||||
if (isNull) {
|
||||
// do transition from notNull to null
|
||||
alterHistoryColumn.setCurrentNotnull(Boolean.TRUE);
|
||||
alterHistoryColumn.setNotnull(Boolean.FALSE);
|
||||
} else {
|
||||
// assume that the column contains null values from the past (required for platforms like mysql/sqlserver/hana)
|
||||
alterHistoryColumn.setCurrentNotnull(Boolean.FALSE);
|
||||
}
|
||||
platformDdl.alterColumn(writer, alterHistoryColumn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void dropCheckConstraint(DdlWrite writer, AlterColumn alter, String constraintName) {
|
||||
|
||||
+37
-58
@@ -2,6 +2,7 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.DbConstraintNaming;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
@@ -46,39 +47,6 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
this.sysPeriodEnd = sysPeriod + "_end";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, HistoryTableUpdate update) {
|
||||
|
||||
MTable table = writer.getTable(update.getBaseTable());
|
||||
if (table == null) {
|
||||
throw new IllegalStateException("MTable " + update.getBaseTable() + " not found in writer? (required for history DDL)");
|
||||
}
|
||||
updateTriggers(writer, table, update);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the existing triggers/stored procedures/views for history table support given the included columns.
|
||||
*/
|
||||
protected abstract void updateHistoryTriggers(DbTriggerUpdate triggerUpdate);
|
||||
|
||||
/**
|
||||
* Process the HistoryTableUpdate which can result in changes to the apply, rollback and drop scripts.
|
||||
*/
|
||||
protected void updateTriggers(DdlWrite writer, MTable table, HistoryTableUpdate update) {
|
||||
|
||||
writer.applyHistoryTrigger().append("-- changes: ").append(update.description()).newLine();
|
||||
|
||||
updateHistoryTriggers(createDbTriggerUpdate(writer, table));
|
||||
}
|
||||
|
||||
protected DbTriggerUpdate createDbTriggerUpdate(DdlWrite writer, MTable table) {
|
||||
|
||||
List<String> columns = columnNamesForApply(table);
|
||||
String baseTableName = table.getName();
|
||||
String historyTableName = historyTableName(baseTableName);
|
||||
return new DbTriggerUpdate(baseTableName, historyTableName, writer, columns);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
|
||||
|
||||
@@ -112,8 +80,9 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
addSysPeriodColumns(writer, baseTable, table.getWhenCreatedColumn());
|
||||
createHistoryTable(writer.applyPostAlter(), table);
|
||||
|
||||
createTriggers(writer, table);
|
||||
createWithHistoryView(writer.applyPostAlter(), table.getName());
|
||||
createTriggers(writer.applyPostAlter(), baseTable, columnNamesForApply(table));
|
||||
writer.applyPostAlter().end();
|
||||
|
||||
// drop all scripts
|
||||
dropTriggers(writer.dropAll(), baseTable);
|
||||
@@ -123,13 +92,35 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
}
|
||||
|
||||
protected abstract void createTriggers(DdlWrite writer, MTable table);
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, String tableName) {
|
||||
MTable table = writer.getTable(tableName);
|
||||
if (table != null && table.isWithHistory()) {
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
// this code effectively disables history support before the table alter and enables it again
|
||||
// immediately after the table alter. As all alters per table are altogether now, this can done here
|
||||
dropTriggers(writer.apply(), tableName);
|
||||
dropWithHistoryView(writer.apply(), tableName);
|
||||
// here are the alter commands
|
||||
createWithHistoryView(writer.applyPostAlter(), tableName);
|
||||
createTriggers(writer.applyPostAlter(), tableName, columnNamesForApply(table));
|
||||
|
||||
alter.setHistoryHandled();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Will add a history trigger to the buffer. The config
|
||||
*
|
||||
* @param buffer
|
||||
* @param table
|
||||
*/
|
||||
protected abstract void createTriggers(DdlBuffer buffer, String baseTable, List<String> columnNames);
|
||||
|
||||
protected abstract void dropTriggers(DdlBuffer buffer, String baseTable);
|
||||
|
||||
protected void createStoredFunction(DdlWrite writer, MTable table) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
protected String normalise(String tableName) {
|
||||
return constraintNaming.normaliseTable(tableName);
|
||||
@@ -159,11 +150,10 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
String baseTableName = table.getName();
|
||||
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
|
||||
addSysPeriodColumns(writer, baseTableName, whenCreatedColumn);
|
||||
createHistoryTable(writer.applyPostAlter(), table);
|
||||
createWithHistoryView(apply, baseTableName);
|
||||
createWithHistoryView(writer.applyPostAlter(), baseTableName);
|
||||
}
|
||||
|
||||
protected void addSysPeriodColumns(DdlWrite writer, String baseTableName, String whenCreatedColumn) {
|
||||
@@ -215,22 +205,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
.append("create view ").append(baseTableName).append(viewSuffix)
|
||||
.append(" as select * from ").append(baseTableName)
|
||||
.append(" union all select * from ").append(baseTableName).append(historySuffix)
|
||||
.endOfStatement().end();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For postgres/h2/mysql 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.
|
||||
*/
|
||||
protected void recreateHistoryView(DbTriggerUpdate update) {
|
||||
|
||||
DdlBuffer buffer = update.dropDependencyBuffer();
|
||||
// we need to drop the view early/first before any changes to the tables etc
|
||||
buffer.append("drop view if exists ").append(update.getBaseTable()).append(viewSuffix).endOfStatement();
|
||||
|
||||
// recreate the view after all ddl modifications - the view requires ALL columns, also the historyExclude ones.
|
||||
createWithHistoryView(update.historyViewBuffer(), update.getBaseTable());
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
protected void appendSysPeriodColumns(DdlBuffer apply, String prefix) {
|
||||
@@ -251,9 +226,9 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
platformDdl.alterTableDropColumn(writer, baseTableName, sysPeriodEnd);
|
||||
}
|
||||
|
||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) {
|
||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String baseTable, List<String> columns) {
|
||||
|
||||
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
|
||||
buffer.append(" insert into ").append(baseTable).append(historySuffix).append(" (").append(sysPeriodStart).append(",").append(sysPeriodEnd).append(",");
|
||||
appendColumnNames(buffer, columns, "");
|
||||
buffer.append(") values (OLD.").append(sysPeriodStart).append(", ").append(sysPeriodEndValue).append(",");
|
||||
appendColumnNames(buffer, columns, "OLD.");
|
||||
@@ -291,4 +266,8 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
|
||||
return table.allHistoryColumns(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean alterHistoryTables() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DB trigger update when a change occurs on a table with history.
|
||||
*/
|
||||
public class DbTriggerUpdate {
|
||||
|
||||
private final String baseTableName;
|
||||
|
||||
private final String historyTableName;
|
||||
|
||||
private final DdlWrite writer;
|
||||
|
||||
private final List<String> columns;
|
||||
|
||||
public DbTriggerUpdate(String baseTableName, String historyTableName, DdlWrite writer, List<String> columns) {
|
||||
this.baseTableName = baseTableName;
|
||||
this.historyTableName = historyTableName;
|
||||
this.writer = writer;
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the appropriate buffer for the current mode.
|
||||
*/
|
||||
public DdlBuffer historyViewBuffer() {
|
||||
return writer.applyHistoryView();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the appropriate buffer for the current mode.
|
||||
*/
|
||||
public DdlBuffer historyTriggerBuffer() {
|
||||
return writer.applyHistoryTrigger();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the appropriate drop dependency buffer for the current mode.
|
||||
*/
|
||||
public DdlBuffer dropDependencyBuffer() {
|
||||
return writer.applyDropDependencies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base table name.
|
||||
*/
|
||||
public String getBaseTable() {
|
||||
return baseTableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the history table name.
|
||||
*/
|
||||
public String getHistoryTable() {
|
||||
return historyTableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the included columns.
|
||||
*/
|
||||
public List<String> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
}
|
||||
+4
-14
@@ -1,8 +1,8 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
/**
|
||||
* H2 history support using DB triggers to maintain a history table.
|
||||
@@ -23,18 +23,8 @@ public class H2HistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createTriggers(DdlWrite writer, MTable table) {
|
||||
String baseTableName = table.getName();
|
||||
DdlBuffer apply = writer.applyHistoryTrigger();
|
||||
addCreateTrigger(apply, updateTriggerName(baseTableName), baseTableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHistoryTriggers(DbTriggerUpdate update) {
|
||||
recreateHistoryView(update);
|
||||
DdlBuffer buffer = update.historyTriggerBuffer();
|
||||
dropTriggers(buffer, update.getBaseTable());
|
||||
addCreateTrigger(buffer, updateTriggerName(update.getBaseTable()), update.getBaseTable());
|
||||
protected void createTriggers(DdlBuffer buffer, String baseTable, List<String> columnNames) {
|
||||
addCreateTrigger(buffer, updateTriggerName(baseTable), baseTable);
|
||||
}
|
||||
|
||||
private void addCreateTrigger(DdlBuffer apply, String triggerName, String baseTable) {
|
||||
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.BaseDdlHandler;
|
||||
|
||||
public class HanaDdlHandler extends BaseDdlHandler {
|
||||
|
||||
public HanaDdlHandler(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
super(config, platformDdl, new HanaTableDdl(config, platformDdl));
|
||||
}
|
||||
}
|
||||
+15
-15
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
@@ -9,9 +10,6 @@ import io.ebeaninternal.dbmigration.model.MColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
@@ -19,7 +17,6 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
private String systemPeriodEnd;
|
||||
private PlatformDdl platformDdl;
|
||||
private String historySuffix;
|
||||
private Map<String, String> createdHistoryTables = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void configure(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
@@ -33,10 +30,7 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
public void createWithHistory(DdlWrite writer, MTable table) {
|
||||
String tableName = table.getName();
|
||||
String historyTableName = tableName + historySuffix;
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
if (apply.isEmpty()) {
|
||||
createdHistoryTables.clear();
|
||||
}
|
||||
DdlBuffer apply = writer.applyPostAlter();
|
||||
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(historyTableName).append(" (").newLine();
|
||||
|
||||
@@ -66,8 +60,6 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
enableSystemVersioning(apply, tableName, true);
|
||||
platformDdl.alterTable(writer, tableName).setHistoryHandled();
|
||||
|
||||
createdHistoryTables.put(tableName, historyTableName);
|
||||
|
||||
dropHistoryTable(writer.dropAll(), tableName, historyTableName);
|
||||
}
|
||||
|
||||
@@ -101,8 +93,19 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) {
|
||||
// nothing to do
|
||||
public boolean alterHistoryTables() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, String tableName) {
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
MTable table = writer.getTable(tableName);
|
||||
if (table.isWithHistory() && !alter.isHistoryHandled()) {
|
||||
disableSystemVersioning(writer.apply(), tableName);
|
||||
enableSystemVersioning(writer.applyPostAlter(), tableName, false);
|
||||
alter.setHistoryHandled();
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type, String defaultValue,
|
||||
@@ -134,7 +137,4 @@ public class HanaHistoryDdl implements PlatformHistoryDdl {
|
||||
apply.endOfStatement();
|
||||
}
|
||||
|
||||
public boolean isSystemVersioningEnabled(String tableName) {
|
||||
return !createdHistoryTables.containsKey(tableName);
|
||||
}
|
||||
}
|
||||
-127
@@ -1,127 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.PropertiesWrapper;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
import io.ebeaninternal.dbmigration.migration.DropColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class HanaTableDdl extends BaseTableDdl {
|
||||
|
||||
private final HanaHistoryDdl historyDdl;
|
||||
|
||||
public HanaTableDdl(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
super(config, platformDdl);
|
||||
this.historyDdl = (HanaHistoryDdl) platformDdl.historyDdl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AddColumn addColumn) {
|
||||
String tableName = addColumn.getTableName();
|
||||
MTable table = writer.getTable(tableName);
|
||||
if (table == null) {
|
||||
super.generate(writer, addColumn);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
alter.setHistoryHandled();
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName());
|
||||
historyDdl.enableSystemVersioning(writer.applyPostAlter(), table.getName(), false);
|
||||
}
|
||||
}
|
||||
|
||||
super.generate(writer, addColumn);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
// make same changes to the history table
|
||||
String historyTable = historyTable(tableName);
|
||||
List<Column> columns = addColumn.getColumn();
|
||||
for (Column column : columns) {
|
||||
alterTableAddColumn(writer, historyTable, column, true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(DdlWrite writer, AlterColumn alterColumn) {
|
||||
String tableName = alterColumn.getTableName();
|
||||
MTable table = writer.getTable(tableName);
|
||||
if (table == null) {
|
||||
super.generate(writer, alterColumn);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
alter.setHistoryHandled();
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName());
|
||||
historyDdl.enableSystemVersioning(writer.applyPostAlter(), table.getName(), false);
|
||||
}
|
||||
}
|
||||
|
||||
super.generate(writer, alterColumn);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
// make same changes to the history table
|
||||
String historyTable = historyTable(tableName);
|
||||
if (hasValue(alterColumn.getType()) || hasValue(alterColumn.getDefaultValue()) || alterColumn.isNotnull() != null) {
|
||||
AlterColumn alterHistoryColumn = new AlterColumn();
|
||||
alterHistoryColumn.setTableName(historyTable);
|
||||
alterHistoryColumn.setColumnName(alterColumn.getColumnName());
|
||||
alterHistoryColumn.setType(alterColumn.getType());
|
||||
alterHistoryColumn.setDefaultValue(alterColumn.getDefaultValue());
|
||||
alterHistoryColumn.setNotnull(alterColumn.isNotnull());
|
||||
alterHistoryColumn.setCurrentType(alterColumn.getCurrentType());
|
||||
alterHistoryColumn.setCurrentDefaultValue(alterColumn.getCurrentDefaultValue());
|
||||
alterHistoryColumn.setCurrentNotnull(alterColumn.isCurrentNotnull());
|
||||
platformDdl.alterColumn(writer, alterHistoryColumn);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(DdlWrite writer, DropColumn dropColumn) {
|
||||
String tableName = dropColumn.getTableName();
|
||||
MTable table = writer.getTable(tableName);
|
||||
if (table == null) {
|
||||
super.generate(writer, dropColumn);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
alter.setHistoryHandled();
|
||||
historyDdl.disableSystemVersioning(writer.apply(), table.getName());
|
||||
historyDdl.enableSystemVersioning(writer.applyPostAlter(), tableName, false);
|
||||
}
|
||||
}
|
||||
|
||||
super.generate(writer, dropColumn);
|
||||
|
||||
if (manageSystemVersioning) {
|
||||
// also drop from the history table
|
||||
String historyTable = historyTable(tableName);
|
||||
alterTableDropColumn(writer, historyTable, dropColumn.getColumnName());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bean holding comments relating to a history table that needs to have it's
|
||||
* associated triggers/function updated due to columns added/dropped/included
|
||||
* or excluded.
|
||||
*/
|
||||
public class HistoryTableUpdate {
|
||||
|
||||
/**
|
||||
* Column change type.
|
||||
*/
|
||||
public enum Change {
|
||||
ADD,
|
||||
DROP,
|
||||
INCLUDE,
|
||||
EXCLUDE,
|
||||
ALTER
|
||||
}
|
||||
|
||||
private static class Column {
|
||||
|
||||
final Change change;
|
||||
|
||||
final String column;
|
||||
|
||||
public Column(Change change, String column) {
|
||||
this.change = change;
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return description();
|
||||
}
|
||||
|
||||
public String description() {
|
||||
return change.name().toLowerCase() + " " + column;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final String baseTable;
|
||||
|
||||
private final List<Column> columnChanges = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Construct with a given base table name.
|
||||
*/
|
||||
public HistoryTableUpdate(String baseTable) {
|
||||
this.baseTable = baseTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a description of the changes that cause the history trigger/function
|
||||
* to be regenerated (added, included, excluded and dropped columns).
|
||||
*/
|
||||
public String description() {
|
||||
return columnChanges.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a comment for column added, dropped, included or excluded.
|
||||
*/
|
||||
public void add(Change change, String column) {
|
||||
columnChanges.add(new Column(change, column));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base table name.
|
||||
*/
|
||||
public String getBaseTable() {
|
||||
return baseTable;
|
||||
}
|
||||
|
||||
}
|
||||
+15
-7
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
@@ -12,9 +13,11 @@ import io.ebeaninternal.dbmigration.model.MTable;
|
||||
*/
|
||||
public class MariaDbHistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
private PlatformDdl platformDdl;
|
||||
|
||||
@Override
|
||||
public void configure(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
// do nothing
|
||||
this.platformDdl = platformDdl;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -24,8 +27,7 @@ public class MariaDbHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
private void enableSystemVersioning(DdlWrite writer, String baseTable) {
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
apply.append("alter table ").append(baseTable).append(" add system versioning").endOfStatement();
|
||||
platformDdl.alterTable(writer, baseTable).append("add system versioning", null);
|
||||
|
||||
DdlBuffer drop = writer.dropAll();
|
||||
drop.append("alter table ").append(baseTable).append(" drop system versioning").endOfStatement();
|
||||
@@ -34,8 +36,7 @@ public class MariaDbHistoryDdl implements PlatformHistoryDdl {
|
||||
@Override
|
||||
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
|
||||
String baseTable = dropHistoryTable.getBaseTable();
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
apply.append("alter table ").append(baseTable).append(" drop system versioning").endOfStatement();
|
||||
platformDdl.alterTable(writer, baseTable).append("drop system versioning", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,7 +46,14 @@ public class MariaDbHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) {
|
||||
// do nothing
|
||||
public void updateTriggers(DdlWrite writer, String tableName) {
|
||||
MTable table = writer.getTable(tableName);
|
||||
// For MariaDB we need to enable system_versioning_alter_history only once
|
||||
// per DDL script. This info is stored in the virtual "__$HISTORY_FLAG__" table
|
||||
DdlAlterTable history = platformDdl.alterTable(writer, "__$HISTORY_FLAG__");
|
||||
if (table != null && table.isWithHistory() && !history.isHistoryHandled()) {
|
||||
writer.apply().appendStatement("SET @@system_versioning_alter_history = 1");
|
||||
history.setHistoryHandled();
|
||||
}
|
||||
}
|
||||
}
|
||||
-30
@@ -7,8 +7,6 @@ import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* MySql specific DDL.
|
||||
*/
|
||||
@@ -156,32 +154,4 @@ public class MySqlDdl extends PlatformDdl {
|
||||
// alter comment currently not supported as it requires to repeat whole column definition
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Locks all tables for triggers that have to be updated.
|
||||
*/
|
||||
@Override
|
||||
public void lockTables(DdlBuffer buffer, Collection<String> tables) {
|
||||
if (!tables.isEmpty()) {
|
||||
buffer.append("lock tables ");
|
||||
int i = 0;
|
||||
for (String table : tables) {
|
||||
if (i > 0) {
|
||||
buffer.append(", ");
|
||||
}
|
||||
buffer.append(table).append(" write");
|
||||
i++;
|
||||
}
|
||||
buffer.endOfStatement();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks all tables for triggers that have to be updated.
|
||||
*/
|
||||
@Override
|
||||
public void unlockTables(DdlBuffer buffer, Collection<String> tables) {
|
||||
buffer.append("unlock tables").endOfStatement();
|
||||
}
|
||||
|
||||
}
|
||||
+13
-26
@@ -1,8 +1,8 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
/**
|
||||
* MySql history support using DB triggers to maintain a history table.
|
||||
@@ -18,49 +18,36 @@ public class MySqlHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
buffer.append("drop trigger ").append(deleteTriggerName(baseTable)).endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createTriggers(DdlWrite writer, MTable table) {
|
||||
|
||||
DbTriggerUpdate update = createDbTriggerUpdate(writer, table);
|
||||
|
||||
addBeforeUpdate(updateTriggerName(update.getBaseTable()), update);
|
||||
addBeforeDelete(deleteTriggerName(update.getBaseTable()), update);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHistoryTriggers(DbTriggerUpdate update) {
|
||||
protected void createTriggers(DdlBuffer buffer, String baseTable, List<String> columnNames) {
|
||||
|
||||
recreateHistoryView(update);
|
||||
buffer.append("lock tables ").append(baseTable).append(" write").endOfStatement();
|
||||
addBeforeUpdate(buffer, updateTriggerName(baseTable), baseTable, columnNames);
|
||||
addBeforeDelete(buffer, deleteTriggerName(baseTable), baseTable, columnNames);
|
||||
buffer.appendStatement("unlock tables");
|
||||
|
||||
DdlBuffer buffer = update.historyTriggerBuffer();
|
||||
String baseTable = update.getBaseTable();
|
||||
|
||||
dropTriggers(buffer, baseTable);
|
||||
addBeforeUpdate(updateTriggerName(baseTable), update);
|
||||
addBeforeDelete(deleteTriggerName(baseTable), update);
|
||||
}
|
||||
|
||||
private void addBeforeUpdate(String triggerName, DbTriggerUpdate update) {
|
||||
private void addBeforeUpdate(DdlBuffer apply, String triggerName, String tableName, List<String> columnNames) {
|
||||
|
||||
DdlBuffer apply = update.historyTriggerBuffer();
|
||||
apply
|
||||
.append("delimiter $$").newLine()
|
||||
.append("create trigger ").append(triggerName).append(" before update on ").append(update.getBaseTable())
|
||||
.append("create trigger ").append(triggerName).append(" before update on ").append(tableName)
|
||||
.append(" for each row begin").newLine();
|
||||
appendInsertIntoHistory(apply, update.getHistoryTable(), update.getColumns());
|
||||
appendInsertIntoHistory(apply, tableName, columnNames);
|
||||
apply
|
||||
.append(" set NEW.").append(sysPeriod).append("_start = now(6)").endOfStatement()
|
||||
.append("end$$").newLine();
|
||||
}
|
||||
|
||||
private void addBeforeDelete(String triggerName, DbTriggerUpdate update) {
|
||||
private void addBeforeDelete(DdlBuffer apply, String triggerName, String tableName, List<String> columnNames) {
|
||||
|
||||
DdlBuffer apply = update.historyTriggerBuffer();
|
||||
apply
|
||||
.append("delimiter $$").newLine()
|
||||
.append("create trigger ").append(triggerName).append(" before delete on ").append(update.getBaseTable())
|
||||
.append("create trigger ").append(triggerName).append(" before delete on ").append(tableName)
|
||||
.append(" for each row begin").newLine();
|
||||
appendInsertIntoHistory(apply, update.getHistoryTable(), update.getColumns());
|
||||
appendInsertIntoHistory(apply, tableName, columnNames);
|
||||
apply.append("end$$").newLine();
|
||||
}
|
||||
|
||||
|
||||
-4
@@ -32,8 +32,4 @@ public class NoHistorySupportDdl implements PlatformHistoryDdl {
|
||||
// does nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, HistoryTableUpdate update) {
|
||||
// does nothing
|
||||
}
|
||||
}
|
||||
+14
-29
@@ -1,8 +1,9 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
/**
|
||||
* NuoDB history support using DB triggers to maintain a history table.
|
||||
@@ -22,48 +23,32 @@ public class NuoDbHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createTriggers(DdlWrite writer, MTable table) {
|
||||
protected void createTriggers(DdlBuffer buffer, String baseTable, List<String> columnNames) {
|
||||
|
||||
DbTriggerUpdate update = createDbTriggerUpdate(writer, table);
|
||||
|
||||
addBeforeUpdate(updateTriggerName(update.getBaseTable()), update);
|
||||
addBeforeDelete(deleteTriggerName(update.getBaseTable()), update);
|
||||
addBeforeUpdate(buffer, updateTriggerName(baseTable), baseTable, columnNames);
|
||||
addBeforeDelete(buffer, deleteTriggerName(baseTable), baseTable, columnNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHistoryTriggers(DbTriggerUpdate update) {
|
||||
private void addBeforeUpdate(DdlBuffer apply, String triggerName, String tableName, List<String> columnNames) {
|
||||
|
||||
recreateHistoryView(update);
|
||||
addTriggerStart(triggerName, tableName, apply, " before update for each row as ");
|
||||
|
||||
DdlBuffer buffer = update.historyTriggerBuffer();
|
||||
String baseTable = update.getBaseTable();
|
||||
|
||||
dropTriggers(buffer, baseTable);
|
||||
addBeforeUpdate(updateTriggerName(baseTable), update);
|
||||
addBeforeDelete(deleteTriggerName(baseTable), update);
|
||||
}
|
||||
|
||||
private void addBeforeUpdate(String triggerName, DbTriggerUpdate update) {
|
||||
|
||||
DdlBuffer apply = update.historyTriggerBuffer();
|
||||
addTriggerStart(triggerName, update, apply, " before update for each row as ");
|
||||
apply.append(" NEW.sys_period_start = greatest(current_timestamp, date_add(OLD.sys_period_start, interval 1 microsecond))").endOfStatement();
|
||||
appendInsertIntoHistory(apply, update.getHistoryTable(), update.getColumns());
|
||||
appendInsertIntoHistory(apply, tableName, columnNames);
|
||||
addEndTrigger(apply);
|
||||
}
|
||||
|
||||
private void addBeforeDelete(String triggerName, DbTriggerUpdate update) {
|
||||
private void addBeforeDelete(DdlBuffer apply, String triggerName, String tableName, List<String> columnNames) {
|
||||
|
||||
DdlBuffer apply = update.historyTriggerBuffer();
|
||||
addTriggerStart(triggerName, update, apply, " before delete for each row as");
|
||||
appendInsertIntoHistory(apply, update.getHistoryTable(), update.getColumns());
|
||||
addTriggerStart(triggerName, tableName, apply, " before delete for each row as");
|
||||
appendInsertIntoHistory(apply, tableName, columnNames);
|
||||
addEndTrigger(apply);
|
||||
}
|
||||
|
||||
private void addTriggerStart(String triggerName, DbTriggerUpdate update, DdlBuffer apply, String s) {
|
||||
private void addTriggerStart(String triggerName, String baseTable, DdlBuffer apply, String s) {
|
||||
apply
|
||||
.append("delimiter $$").newLine()
|
||||
.append("create or replace trigger ").append(triggerName).append(" for ").append(update.getBaseTable())
|
||||
.append("create or replace trigger ").append(triggerName).append(" for ").append(baseTable)
|
||||
.append(s).newLine();
|
||||
}
|
||||
|
||||
|
||||
+2
-17
@@ -18,7 +18,6 @@ import io.ebeaninternal.dbmigration.migration.Column;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -364,8 +363,8 @@ public class PlatformDdl {
|
||||
/**
|
||||
* Regenerate the history triggers (or function) due to a column being added/dropped/excluded or included.
|
||||
*/
|
||||
public void regenerateHistoryTriggers(DdlWrite writer, HistoryTableUpdate update) {
|
||||
historyDdl.updateTriggers(writer, update);
|
||||
public void regenerateHistoryTriggers(DdlWrite writer, String tableName) {
|
||||
historyDdl.updateTriggers(writer, tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -780,20 +779,6 @@ public class PlatformDdl {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mysql-specific: Locks all tables for triggers that have to be updated.
|
||||
*/
|
||||
public void lockTables(DdlBuffer buffer, Collection<String> tables) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Mysql-specific: Unlocks all tables for triggers that have to be updated.
|
||||
*/
|
||||
public void unlockTables(DdlBuffer buffer, Collection<String> tables) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the database-specific "create table" command prefix. For HANA this is
|
||||
* either "create column table" or "create row table", for all other databases
|
||||
|
||||
+14
-2
@@ -32,7 +32,19 @@ public interface PlatformHistoryDdl {
|
||||
void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable);
|
||||
|
||||
/**
|
||||
* Regenerate the history triggers/stored function due to column added/dropped/included or excluded.
|
||||
* Returns true, if alters on the live tables should be applied also to the history tables. This is required for DbTriggerBased
|
||||
* histories or on platforms like Hana, which are not SQL2011 history compatible (at least from DDL perspective)
|
||||
*/
|
||||
void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable);
|
||||
default boolean alterHistoryTables() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate the history triggers/stored function due to column added/dropped/included or excluded.
|
||||
*
|
||||
* Note: This function may be called multiple times for the same table.
|
||||
*/
|
||||
default void updateTriggers(DdlWrite writer, String tableName) {
|
||||
// nop
|
||||
}
|
||||
}
|
||||
+3
-22
@@ -49,14 +49,11 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createTriggers(DdlWrite writer, MTable table) {
|
||||
String baseTableName = table.getName();
|
||||
protected void createTriggers(DdlBuffer buffer, String baseTableName, List<String> columnNames) {
|
||||
String procedureName = procedureName(baseTableName);
|
||||
String triggerName = triggerName(baseTableName);
|
||||
List<String> columnNames = columnNamesForApply(table);
|
||||
DdlBuffer apply = writer.applyHistoryTrigger();
|
||||
createOrReplaceFunction(apply, procedureName, historyTableName(baseTableName), columnNames);
|
||||
apply
|
||||
createOrReplaceFunction(buffer, procedureName, historyTableName(baseTableName), columnNames);
|
||||
buffer
|
||||
.append("create trigger ").append(triggerName).newLine()
|
||||
.append(" before update or delete on ").append(baseTableName).newLine()
|
||||
.append(" for each row execute procedure ").append(procedureName).append("();").newLine().newLine();
|
||||
@@ -101,22 +98,6 @@ public class PostgresHistoryDdl extends DbTriggerBasedHistoryDdl {
|
||||
apply.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createStoredFunction(DdlWrite writer, MTable table) {
|
||||
String procedureName = procedureName(table.getName());
|
||||
String historyTable = historyTableName(table.getName());
|
||||
|
||||
List<String> columnNames = columnNamesForApply(table);
|
||||
createOrReplaceFunction(writer.applyHistoryTrigger(), procedureName, historyTable, columnNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHistoryTriggers(DbTriggerUpdate update) {
|
||||
String procedureName = procedureName(update.getBaseTable());
|
||||
recreateHistoryView(update);
|
||||
createOrReplaceFunction(update.historyTriggerBuffer(), procedureName, update.getHistoryTable(), update.getColumns());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void appendInsertIntoHistory(DdlBuffer buffer, String historyTable, List<String> columns) {
|
||||
buffer.append(" insert into ").append(historyTable).append(" (").append(sysPeriod).append(",");
|
||||
|
||||
+16
-11
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
|
||||
@@ -43,7 +44,7 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
private void enableSystemVersioning(DdlWrite writer, String baseTable) {
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
DdlBuffer apply = writer.applyPostAlter();
|
||||
apply.append("alter table ").append(baseTable).newLine()
|
||||
.append(" add ").append(systemPeriodStart).append(" datetime2 GENERATED ALWAYS AS ROW START NOT NULL DEFAULT SYSUTCDATETIME(),").newLine()
|
||||
.append(" ").append(systemPeriodEnd).append(" datetime2 GENERATED ALWAYS AS ROW END NOT NULL DEFAULT '9999-12-31T23:59:59.9999999',").newLine()
|
||||
@@ -88,15 +89,19 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTriggers(DdlWrite writer, HistoryTableUpdate baseTable) {
|
||||
// SQL Server 2016 does not need triggers
|
||||
DdlBuffer apply = writer.applyHistoryView();
|
||||
String baseTableName = baseTable.getBaseTable();
|
||||
apply.append("-- alter table ").append(baseTableName).append(" set (system_versioning = off (history_table=")
|
||||
.append(getHistoryTable(baseTableName)).append("))").endOfStatement();
|
||||
apply.append("-- history migration goes here").newLine();
|
||||
apply.append("-- alter table ").append(baseTableName).append(" set (system_versioning = on (history_table=")
|
||||
.append(getHistoryTable(baseTableName)).append("))").endOfStatement();
|
||||
|
||||
public void updateTriggers(DdlWrite writer, String tableName) {
|
||||
DdlAlterTable alter = platformDdl.alterTable(writer, tableName);
|
||||
writer.getTable(tableName);
|
||||
if (!alter.isHistoryHandled()) {
|
||||
// SQL Server 2016 does not need triggers
|
||||
DdlBuffer apply = writer.apply();
|
||||
apply.append("-- alter table ").append(tableName).append(" set (system_versioning = off (history_table=")
|
||||
.append(getHistoryTable(tableName)).append("))").endOfStatement();
|
||||
apply.append("-- history migration goes here").newLine();
|
||||
apply.append("-- alter table ").append(tableName).append(" set (system_versioning = on (history_table=")
|
||||
.append(getHistoryTable(tableName)).append("))").endOfStatement();
|
||||
}
|
||||
alter.setHistoryHandled();
|
||||
}
|
||||
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.config.dbplatform.h2.H2Platform;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.PlatformDdlBuilder;
|
||||
import io.ebeaninternal.dbmigration.model.CurrentModel;
|
||||
import io.ebeaninternal.dbmigration.model.MConfiguration;
|
||||
import io.ebeaninternal.dbmigration.model.ModelContainer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class H2HistoryDdlTest {
|
||||
|
||||
@Test
|
||||
public void testRegenerateHistoryTriggers() throws Exception {
|
||||
|
||||
SpiEbeanServer ebeanServer = (SpiEbeanServer) DB.getDefault();
|
||||
|
||||
HistoryTableUpdate update = new HistoryTableUpdate("c_user");
|
||||
update.add(HistoryTableUpdate.Change.ADD, "one");
|
||||
update.add(HistoryTableUpdate.Change.DROP, "two");
|
||||
|
||||
|
||||
CurrentModel currentModel = new CurrentModel(ebeanServer);
|
||||
ModelContainer modelContainer = currentModel.read();
|
||||
DdlWrite writer = new DdlWrite(new MConfiguration(), modelContainer, new DdlOptions());
|
||||
|
||||
H2Platform h2Platform = new H2Platform();
|
||||
PlatformDdl h2Ddl = PlatformDdlBuilder.create(h2Platform);
|
||||
h2Ddl.configure(ebeanServer.config());
|
||||
h2Ddl.regenerateHistoryTriggers(writer, update);
|
||||
|
||||
assertThat(writer.applyHistoryView().isEmpty()).isFalse();
|
||||
assertThat(writer.applyHistoryTrigger().isEmpty()).isFalse();
|
||||
assertThat(writer.applyHistoryView().getBuffer())
|
||||
.contains("create view")
|
||||
.doesNotContain("create trigger");
|
||||
assertThat(writer.applyHistoryTrigger().getBuffer())
|
||||
.contains("add one")
|
||||
.contains("create trigger")
|
||||
.doesNotContain("create view");
|
||||
assertThat(writer.dropAll().isEmpty()).isTrue();
|
||||
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class HistoryTableUpdateTest {
|
||||
|
||||
@Test
|
||||
public void testDescription() throws Exception {
|
||||
|
||||
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
|
||||
upd.add(HistoryTableUpdate.Change.ADD, "two");
|
||||
upd.add(HistoryTableUpdate.Change.DROP, "four");
|
||||
|
||||
assertThat(upd.description()).isEqualTo("[add two, drop four]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescription_withIncludeExclude() throws Exception {
|
||||
|
||||
HistoryTableUpdate upd = new HistoryTableUpdate("mytab");
|
||||
upd.add(HistoryTableUpdate.Change.ADD, "two");
|
||||
upd.add(HistoryTableUpdate.Change.INCLUDE, "five");
|
||||
upd.add(HistoryTableUpdate.Change.EXCLUDE, "six");
|
||||
upd.add(HistoryTableUpdate.Change.DROP, "four");
|
||||
|
||||
assertThat(upd.description()).isEqualTo("[add two, include five, exclude six, drop four]");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user