Merge pull request #2596 from FOCONIS/ddl-refactor8

DDl-Refactor: Add DB2 Reorg support and history support
This commit is contained in:
Rob Bygrave authored and GitHub committed 2022-03-16 18:47:58 +13:00
commit 00a1324c52
35 files changed
+2021 -230

No files matched your search

@@ -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);
@@ -1,7 +1,14 @@
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebean.annotation.ConstraintMode;
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.
@@ -12,12 +19,13 @@ public class DB2Ddl extends PlatformDdl {
super(platform);
this.dropTableIfExists = "drop table ";
this.dropSequenceIfExists = "drop sequence ";
this.dropConstraintIfExists = "drop constraint";
this.dropIndexIfExists = "drop index ";
this.dropConstraintIfExists = "NOT USED";
this.dropIndexIfExists = "NOT USED";
this.identitySuffix = " generated by default as identity";
this.columnSetNull = "drop not null";
this.columnSetType = "set data type ";
this.inlineUniqueWhenNullable = false;
this.historyDdl = new Db2HistoryDdl();
}
@Override
@@ -30,7 +38,7 @@ public class DB2Ddl extends PlatformDdl {
throw new NullPointerException();
}
StringBuilder sb = new StringBuilder("create unique index ");
sb.append(uqName).append(" on ").append(tableName).append('(');
sb.append(maxConstraintName(uqName)).append(" on ").append(tableName).append('(');
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
@@ -43,8 +51,143 @@ public class DB2Ddl extends PlatformDdl {
}
@Override
protected void appendForeignKeyOnUpdate(StringBuilder buffer, ConstraintMode mode) {
// do nothing, no on update clause for db2
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);
};
@Override
public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) {
return alterTableDropConstraint(tableName, uniqueConstraintName)
+ "\n" + dropIndex(uniqueConstraintName, tableName);
}
@Override
public String alterTableDropConstraint(String tableName, String constraintName) {
StringBuilder sb = new StringBuilder(300);
sb.append("delimiter $$\n")
.append("begin\n")
.append("if exists (select constname from syscat.tabconst where tabschema = current_schema and constname = '")
.append(maxConstraintName(constraintName).toUpperCase())
.append("' and tabname = '").append(lowerTableName(tableName).toUpperCase()).append("') then\n")
.append(" prepare stmt from 'alter table ").append(lowerTableName(tableName))
.append(" drop constraint ").append(maxConstraintName(constraintName)).append("';\n")
.append(" execute stmt;\n")
.append("end if;\n")
.append("end$$");
return sb.toString();
}
@Override
public String dropIndex(String indexName, String tableName, boolean concurrent) {
StringBuilder sb = new StringBuilder(300);
sb.append("delimiter $$\n")
.append("begin\n")
.append("if exists (select indname from syscat.indexes where indschema = current_schema and indname = '")
.append(maxConstraintName(indexName).toUpperCase()).append("') then\n")
.append(" prepare stmt from 'drop index ").append(maxConstraintName(indexName)).append("';\n")
.append(" execute stmt;\n")
.append("end if;\n")
.append("end$$");
return sb.toString();
}
@Override
public String dropSequence(String sequenceName) {
StringBuilder sb = new StringBuilder(300);
sb.append("delimiter $$\n");
sb.append("begin\n");
sb.append("if exists (select seqschema from syscat.sequences where seqschema = current_schema and seqname = '")
.append(maxConstraintName(sequenceName).toUpperCase()).append("') then\n");
sb.append(" prepare stmt from 'drop sequence ").append(maxConstraintName(sequenceName)).append("';\n");
sb.append(" execute stmt;\n");
sb.append("end if;\n");
sb.append("end$$");
return sb.toString();
}
@Override
protected DdlAlterTable alterTable(DdlWrite writer, String tableName) {
return writer.applyAlterTable(tableName, Db2AlterTableWrite::new);
};
static class Db2AlterTableWrite extends BaseAlterTableWrite {
public Db2AlterTableWrite(String tableName) {
super(tableName);
}
@Override
protected List<AlterCmd> postProcessCommands(List<AlterCmd> cmds) {
List<AlterCmd> ret = new ArrayList<>(cmds.size() + 1);
boolean requiresReorg = false;
for (AlterCmd cmd : cmds) {
ret.add(cmd);
if (!requiresReorg && checkReorg(cmd)) {
requiresReorg = true;
}
}
if (requiresReorg) {
ret.add(newRawCommand("call sysproc.admin_cmd('reorg table " + tableName() + "')"));
}
return ret;
}
/**
* determine, if we need a reorg.
*
* See: https://www.ibm.com/docs/en/db2/11.5?topic=statements-alter-table The following is the full list of REORG-recommended
* ALTER statements that cause a version change and place the table into a REORG-pending state:
* <ul>
* <li>DROP COLUMN
* <li>ALTER COLUMN SET NOT NULL
* <li>ALTER COLUMN DROP NOT NULL
* <li>ALTER COLUMN SET DATA TYPE, except in the following situations:<br>
* Increasing the length of a VARCHAR or VARGRAPHIC column<br>
* Decreasing the length of a VARCHAR or VARGRAPHIC column without truncating trailing blanks from existing data, when no indexes
* exist on the column
* </ul>
*
*/
private boolean checkReorg(AlterCmd cmd) {
switch (cmd.getOperation()) {
case "drop column":
return true;
case "alter column":
String alter = cmd.getAlternation();
return alter.equals("set not null")
|| alter.equals("drop not default")
|| alter.startsWith("set data type"); // note: altering varchar length only is not detected here
default:
return false;
}
}
}
}
@@ -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();
}
}
@@ -135,7 +135,8 @@ public class PlatformDdl_AlterColumnTest {
softly.assertThat(sql).isEqualTo("-- apply alter tables\n"
+ "alter table mytab alter column acol set data type varchar(50);\n"
+ "alter table mytab alter column acol set default 'hi';\n"
+ "alter table mytab alter column acol set not null;\n");
+ "alter table mytab alter column acol set not null;\n"
+ "call sysproc.admin_cmd('reorg table mytab');\n");
//
alter.setCurrentNotnull(Boolean.TRUE);
@@ -213,7 +214,10 @@ public class PlatformDdl_AlterColumnTest {
sql = alterColumn(db2Ddl, alter);
softly.assertThat(sql).isEqualTo("-- apply alter tables\n"
+ "alter table mytab alter column acol set data type varchar(20);\n");
+ "alter table mytab alter column acol set data type varchar(20);\n"
// Note, this reorg may be not necessary when only length attribute is alterd
// but this is currently not implemented.
+ "call sysproc.admin_cmd('reorg table mytab');\n");
alter.setType("bigint");
sql = alterColumn(pgDdl, alter);
@@ -271,7 +275,8 @@ public class PlatformDdl_AlterColumnTest {
sql = alterColumn(db2Ddl, alter);
softly.assertThat(sql).isEqualTo("-- apply alter tables\n"
+ "alter table mytab alter column acol set not null;\n");
+ "alter table mytab alter column acol set not null;\n"
+ "call sysproc.admin_cmd('reorg table mytab');\n");
}
@@ -512,7 +517,7 @@ public class PlatformDdl_AlterColumnTest {
softly.assertThat(alterFkey(oraDdl, null, null)).isEqualTo("");
softly.assertThat(alterFkey(sqlServerDdl, null, null)).isEqualTo("");
softly.assertThat(alterFkey(hanaDdl, null, null)).isEqualTo(" on delete restrict on update restrict");
softly.assertThat(alterFkey(db2Ddl, null, null)).isEqualTo(" on delete restrict");
softly.assertThat(alterFkey(db2Ddl, null, null)).isEqualTo(" on delete restrict on update restrict");
}
@Test
@@ -536,7 +541,7 @@ public class PlatformDdl_AlterColumnTest {
.isEqualTo(" on delete restrict on update set null");
softly.assertThat(alterFkey(db2Ddl, "RESTRICT", "SET_NULL"))
.isEqualTo(" on delete restrict");
.isEqualTo(" on delete restrict on update set null");
}
@Test
@@ -560,7 +565,7 @@ public class PlatformDdl_AlterColumnTest {
.isEqualTo(" on delete set null on update restrict");
softly.assertThat(alterFkey(db2Ddl, "SET_NULL", "RESTRICT"))
.isEqualTo(" on delete set null");
.isEqualTo(" on delete set null on update restrict");
}
@Test
@@ -584,7 +589,7 @@ public class PlatformDdl_AlterColumnTest {
.isEqualTo(" on delete set default on update cascade");
softly.assertThat(alterFkey(db2Ddl, "SET_DEFAULT", "CASCADE"))
.isEqualTo(" on delete set default");
.isEqualTo(" on delete set default on update cascade");
}
private String alterColumn(PlatformDdl ddl, AlterColumn alterColumn) {