mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
DB2: HistorySupport added
This commit is contained in:
1 parent
05a1b0d64e
commit
c9204eed3f
6 files changed
+211
-1
No files matched your search
+9
-1
@@ -759,6 +759,11 @@ public class BaseTableDdl implements TableDdl {
|
||||
// we will apply only type changes or notNull -> null transition
|
||||
boolean isNull = Boolean.FALSE.equals(alter.isNotnull());
|
||||
boolean applyToHistory = alter.getType() != null || isNull;
|
||||
boolean syncExact = platformDdl.getPlatform().isPlatform(Platform.DB2);
|
||||
// DB2 needs exact sync for notnull/null
|
||||
if (syncExact && alter.isNotnull() != null) {
|
||||
applyToHistory = true;
|
||||
}
|
||||
if (applyToHistory) {
|
||||
platformDdl.regenerateHistoryTriggers(writer, alter.getTableName());
|
||||
if (alterHistoryTables) {
|
||||
@@ -768,7 +773,10 @@ public class BaseTableDdl implements TableDdl {
|
||||
// ignore default value (not needed on history tables)
|
||||
alterHistoryColumn.setCurrentType(alter.getCurrentType());
|
||||
alterHistoryColumn.setType(alter.getType());
|
||||
if (isNull) {
|
||||
if (syncExact) {
|
||||
alterHistoryColumn.setCurrentNotnull(alter.isCurrentNotnull());
|
||||
alterHistoryColumn.setNotnull(alter.isNotnull());
|
||||
} else if (isNull) {
|
||||
// do transition from notNull to null
|
||||
alterHistoryColumn.setCurrentNotnull(Boolean.TRUE);
|
||||
alterHistoryColumn.setNotnull(Boolean.FALSE);
|
||||
|
||||
+30
@@ -4,8 +4,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebean.util.StringHelper;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.Column;
|
||||
|
||||
/**
|
||||
* DB2 platform specific DDL.
|
||||
@@ -22,6 +25,7 @@ public class DB2Ddl extends PlatformDdl {
|
||||
this.columnSetNull = "drop not null";
|
||||
this.columnSetType = "set data type ";
|
||||
this.inlineUniqueWhenNullable = false;
|
||||
this.historyDdl = new Db2HistoryDdl();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,6 +50,32 @@ public class DB2Ddl extends PlatformDdl {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void alterTableAddColumn(DdlWrite writer, String tableName, Column column, boolean onHistoryTable, String defaultValue) {
|
||||
|
||||
String convertedType = convert(column.getType());
|
||||
DdlBuffer buffer = alterTable(writer, tableName).append(addColumn, column.getName());
|
||||
buffer.append(convertedType);
|
||||
|
||||
// Add default value also to history table if it is not excluded
|
||||
if (defaultValue != null) {
|
||||
buffer.append(" default ");
|
||||
buffer.append(defaultValue);
|
||||
}
|
||||
|
||||
if (isTrue(column.isNotnull())) {
|
||||
buffer.appendWithSpace(columnNotNull);
|
||||
}
|
||||
// DB2 History table must match exact!
|
||||
if (!onHistoryTable) {
|
||||
// check constraints cannot be added in one statement for h2
|
||||
if (!StringHelper.isNull(column.getCheckConstraint())) {
|
||||
String ddl = alterTableAddCheckConstraint(tableName, column.getCheckConstraintName(), column.getCheckConstraint());
|
||||
writer.applyPostAlter().appendStatement(ddl);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public String alterTableDropForeignKey(String tableName, String fkName) {
|
||||
return alterTableDropConstraint(tableName, fkName);
|
||||
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
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;
|
||||
import io.ebeaninternal.dbmigration.migration.DropHistoryTable;
|
||||
import io.ebeaninternal.dbmigration.model.MColumn;
|
||||
import io.ebeaninternal.dbmigration.model.MTable;
|
||||
|
||||
/**
|
||||
* DB2 History support.
|
||||
*
|
||||
* @author Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public class Db2HistoryDdl implements PlatformHistoryDdl {
|
||||
|
||||
private String systemPeriodStart;
|
||||
private String systemPeriodEnd;
|
||||
private String transactionId;
|
||||
private PlatformDdl platformDdl;
|
||||
private String historySuffix;
|
||||
|
||||
@Override
|
||||
public void configure(DatabaseConfig config, PlatformDdl platformDdl) {
|
||||
this.systemPeriodStart = config.getAsOfSysPeriod() + "_start";
|
||||
this.systemPeriodEnd = config.getAsOfSysPeriod() + "_end";
|
||||
this.transactionId = config.getAsOfSysPeriod() + "_txn"; // required for DB2
|
||||
this.platformDdl = platformDdl;
|
||||
this.historySuffix = config.getHistoryTableSuffix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createWithHistory(DdlWrite writer, MTable table) {
|
||||
String tableName = table.getName();
|
||||
String historyTableName = tableName + historySuffix;
|
||||
|
||||
DdlBuffer apply = writer.applyPostAlter();
|
||||
apply.append(platformDdl.getCreateTableCommandPrefix()).append(" ").append(historyTableName).append(" (").newLine();
|
||||
|
||||
// create history table
|
||||
Collection<MColumn> cols = table.allColumns();
|
||||
for (MColumn column : cols) {
|
||||
if (!column.isDraftOnly()) {
|
||||
writeColumnDefinition(apply, column.getName(), column.getType(), column.isNotnull() || column.isPrimaryKey());
|
||||
apply.append(",").newLine();
|
||||
}
|
||||
}
|
||||
writeColumnDefinition(apply, systemPeriodStart, "timestamp(12)", true);
|
||||
apply.append(",").newLine();
|
||||
writeColumnDefinition(apply, systemPeriodEnd, "timestamp(12)", true);
|
||||
apply.append(",").newLine();
|
||||
writeColumnDefinition(apply, transactionId, "timestamp(12)", false);
|
||||
apply.newLine().append(")").endOfStatement();
|
||||
|
||||
// enable system versioning
|
||||
addSysPeriodColumns(writer, tableName);
|
||||
enableSystemVersioning(apply, tableName);
|
||||
platformDdl.alterTable(writer, tableName).setHistoryHandled();
|
||||
|
||||
// drop all: We do not drop columns here, as the whole table will be dropped
|
||||
disableSystemVersioning(writer.dropAll(), tableName);
|
||||
writer.dropAll().append("drop table ").append(historyTableName).endOfStatement();
|
||||
}
|
||||
|
||||
void addSysPeriodColumns(DdlWrite writer, String baseTable) {
|
||||
platformDdl.alterTableAddColumn(writer, baseTable, systemPeriodStart, "timestamp(12) not null generated always as row begin", null);
|
||||
platformDdl.alterTableAddColumn(writer, baseTable, systemPeriodEnd, "timestamp(12) not null generated always as row end", null);
|
||||
platformDdl.alterTableAddColumn(writer, baseTable, transactionId, "timestamp(12) generated always as transaction start id", null);
|
||||
platformDdl.alterTable(writer, baseTable).append("add period system_time", null)
|
||||
.append("(").append(systemPeriodStart).append(",").append(systemPeriodEnd).append(")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) {
|
||||
dropHistoryTable(writer, dropHistoryTable.getBaseTable(), dropHistoryTable.getBaseTable() + historySuffix);
|
||||
}
|
||||
|
||||
protected void dropHistoryTable(DdlWrite writer, String baseTable, String historyTable) {
|
||||
disableSystemVersioning(writer.apply(), baseTable);
|
||||
writer.apply().append("alter table ").append(baseTable).append(" drop period system_time").endOfStatement();
|
||||
|
||||
// drop the period & period columns
|
||||
platformDdl.alterTableDropColumn(writer, baseTable, systemPeriodStart);
|
||||
platformDdl.alterTableDropColumn(writer, baseTable, systemPeriodEnd);
|
||||
platformDdl.alterTableDropColumn(writer, baseTable, transactionId);
|
||||
|
||||
// drop the history table
|
||||
writer.applyPostAlter().append("drop table ").append(historyTable).endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addHistoryTable(DdlWrite writer, AddHistoryTable addHistoryTable) {
|
||||
MTable table = writer.getTable(addHistoryTable.getBaseTable());
|
||||
if (table == null) {
|
||||
throw new IllegalStateException("MTable " + addHistoryTable.getBaseTable() + " not found in writer? (required for history DDL)");
|
||||
}
|
||||
createWithHistory(writer, table);
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
alter.setHistoryHandled();
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeColumnDefinition(DdlBuffer buffer, String columnName, String type, boolean isNotNull) {
|
||||
|
||||
String platformType = platformDdl.convert(type);
|
||||
buffer.append(" ").append(platformDdl.lowerColumnName(columnName));
|
||||
buffer.append(" ").append(platformType);
|
||||
if (isNotNull) {
|
||||
buffer.append(" not null");
|
||||
}
|
||||
}
|
||||
|
||||
public void disableSystemVersioning(DdlBuffer apply, String tableName) {
|
||||
apply.append("alter table ").append(tableName).append(" drop versioning").endOfStatement();
|
||||
}
|
||||
|
||||
public void enableSystemVersioning(DdlBuffer apply, String tableName) {
|
||||
apply.append("alter table ").append(tableName).append(" add versioning use history table ").append(tableName).append(historySuffix).endOfStatement();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user