mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
DB2: Migration is working now
This commit is contained in:
1 parent
4d62958645
commit
05a1b0d64e
25 files changed
+1244
-219
No files matched your search
+119
-6
@@ -1,7 +1,11 @@
|
||||
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.ebeaninternal.dbmigration.ddlgeneration.DdlAlterTable;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
|
||||
/**
|
||||
* DB2 platform specific DDL.
|
||||
@@ -12,8 +16,8 @@ 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 ";
|
||||
@@ -30,7 +34,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 +47,117 @@ public class DB2Ddl extends PlatformDdl {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void appendForeignKeyOnUpdate(StringBuilder buffer, ConstraintMode mode) {
|
||||
// do nothing, no on update clause for db2
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user