From 9a7646195c38fd4e59d4d3ec3e1b4a2a64ef1957 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Sun, 10 Sep 2017 05:26:01 +0200 Subject: [PATCH] Featur/dbmigration 3 (#1118) * ADD: more test classes for Softdelete and History * update reference models after adding test classes * DbMigration:Improved History generation for sqlserver * FIX: When DB supports Sql2011 platform, we must not alter the history-table (as this is handled by DB itself) * DbMigration: When dropping a table, drop the sequence also. * DbMigration: dropColumn is handled by platformDdl now, as SqlServerDdl needs to delete dependent objects (default constraints...) first * FIX: DbMigration is not executed on history table * DbMigration: can drop comment now. * No effective code change: completed ObjectFactory / fixed imports * FIX: drop table comment * Committed the reference models --- .../ddlgeneration/platform/BaseTableDdl.java | 44 +++++-- .../ddlgeneration/platform/PlatformDdl.java | 15 ++- .../ddlgeneration/platform/SqlServerDdl.java | 69 ++++++++++- .../platform/SqlServerHistoryDdl.java | 13 +++ .../ebean/dbmigration/migration/Column.java | 1 - .../dbmigration/migration/DropTable.java | 44 +++++++ .../dbmigration/migration/ObjectFactory.java | 8 ++ .../io/ebean/dbmigration/model/MColumn.java | 2 - .../io/ebean/dbmigration/model/MTable.java | 25 +++- .../platform/PlatformDdl_AlterColumnTest.java | 11 +- .../PlatformDdl_dropUniqueConstraintTest.java | 3 +- .../java/misc/migration/v1_0/EHistory2.java | 20 ++++ .../java/misc/migration/v1_0/ESoftdelete.java | 17 +++ .../java/misc/migration/v1_1/EHistory2.java | 29 +++++ .../java/misc/migration/v1_1/ESoftdelete.java | 22 ++++ .../java/misc/migration/v1_2/EHistory2.java | 20 ++++ .../java/misc/migration/v1_2/ESoftdelete.java | 17 +++ .../migrationtest/db2/1.0__initial.sql | 12 ++ .../dbmigration/migrationtest/db2/1.1.sql | 11 ++ .../migrationtest/db2/1.2__dropsFor_1.1.sql | 1 + .../dbmigration/migrationtest/db2/1.3.sql | 5 +- .../migrationtest/db2/1.4__dropsFor_1.3.sql | 9 ++ .../migrationtest/h2/1.0__initial.sql | 23 ++++ .../dbmigration/migrationtest/h2/1.1.sql | 14 +++ .../migrationtest/h2/1.2__dropsFor_1.1.sql | 1 + .../dbmigration/migrationtest/h2/1.3.sql | 5 +- .../migrationtest/h2/1.4__dropsFor_1.3.sql | 12 ++ .../migrationtest/hsqldb/1.0__initial.sql | 12 ++ .../dbmigration/migrationtest/hsqldb/1.1.sql | 11 ++ .../hsqldb/1.2__dropsFor_1.1.sql | 1 + .../dbmigration/migrationtest/hsqldb/1.3.sql | 5 +- .../hsqldb/1.4__dropsFor_1.3.sql | 9 ++ .../model/1.0__initial.model.xml | 8 ++ .../migrationtest/model/1.1.model.xml | 10 +- .../model/1.2__dropsFor_1.1.model.xml | 2 +- .../migrationtest/model/1.3.model.xml | 8 +- .../model/1.4__dropsFor_1.3.model.xml | 5 +- .../migrationtest/mysql/1.0__initial.sql | 31 +++++ .../dbmigration/migrationtest/mysql/1.1.sql | 25 ++++ .../dbmigration/migrationtest/mysql/1.3.sql | 4 +- .../migrationtest/mysql/1.4__dropsFor_1.3.sql | 22 ++++ .../migrationtest/oracle/1.0__initial.sql | 14 +++ .../dbmigration/migrationtest/oracle/1.1.sql | 9 ++ .../oracle/1.2__dropsFor_1.1.sql | 1 + .../dbmigration/migrationtest/oracle/1.3.sql | 5 +- .../oracle/1.4__dropsFor_1.3.sql | 7 ++ .../migrationtest/postgres/1.0__initial.sql | 33 ++++++ .../migrationtest/postgres/1.1.sql | 30 +++++ .../postgres/1.2__dropsFor_1.1.sql | 1 + .../migrationtest/postgres/1.3.sql | 5 +- .../postgres/1.4__dropsFor_1.3.sql | 26 +++++ .../migrationtest/sqlite/1.0__initial.sql | 12 ++ .../dbmigration/migrationtest/sqlite/1.1.sql | 11 ++ .../dbmigration/migrationtest/sqlite/1.3.sql | 2 + .../sqlite/1.4__dropsFor_1.3.sql | 8 ++ .../migrationtest/sqlserver/1.0__initial.sql | 17 +++ .../migrationtest/sqlserver/1.1.sql | 15 ++- .../sqlserver/1.2__dropsFor_1.1.sql | 33 ++++++ .../migrationtest/sqlserver/1.3.sql | 24 +++- .../sqlserver/1.4__dropsFor_1.3.sql | 108 ++++++++++++++++++ 60 files changed, 922 insertions(+), 45 deletions(-) create mode 100644 src/test/java/misc/migration/v1_0/EHistory2.java create mode 100644 src/test/java/misc/migration/v1_0/ESoftdelete.java create mode 100644 src/test/java/misc/migration/v1_1/EHistory2.java create mode 100644 src/test/java/misc/migration/v1_1/ESoftdelete.java create mode 100644 src/test/java/misc/migration/v1_2/EHistory2.java create mode 100644 src/test/java/misc/migration/v1_2/ESoftdelete.java diff --git a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java index 1ca30a410..ff3087311 100644 --- a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java +++ b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java @@ -3,6 +3,8 @@ package io.ebean.dbmigration.ddlgeneration.platform; import io.ebean.config.DbConstraintNaming; import io.ebean.config.NamingConvention; import io.ebean.config.ServerConfig; +import io.ebean.config.dbplatform.DbHistorySupport; +import io.ebean.config.dbplatform.DbIdentity; import io.ebean.config.dbplatform.IdType; import io.ebean.dbmigration.ddlgeneration.DdlBuffer; import io.ebean.dbmigration.ddlgeneration.DdlWrite; @@ -74,6 +76,8 @@ public class BaseTableDdl implements TableDdl { protected Map regenerateHistoryTriggers = new LinkedHashMap<>(); private boolean strict; + + private final boolean sql2011History; /** * Helper class that is used to execute the migration ddl before and after the migration action. @@ -196,6 +200,8 @@ public class BaseTableDdl implements TableDdl { this.platformDdl = platformDdl; this.platformDdl.configure(serverConfig); this.strict = true; // TODO RPr serverConfig.getMigrationConfig().isStrict(); + DbHistorySupport hist = platformDdl.getPlatform().getHistorySupport(); + this.sql2011History = hist != null && hist.isStandardsBased(); } /** @@ -512,6 +518,14 @@ public class BaseTableDdl implements TableDdl { buffer.append(platformDdl.dropTable(tableName)).endOfStatement(); } + /** + * Add 'drop sequence' statement to the buffer. + */ + protected void dropSequence(DdlBuffer buffer, String sequenceName) throws IOException { + + buffer.append(platformDdl.dropSequence(sequenceName)).endOfStatement(); + } + /** * Write all the check constraints. */ @@ -715,7 +729,7 @@ public class BaseTableDdl implements TableDdl { alterTableAddColumn(writer.apply(), tableName, column, false); } - if (isTrue(addColumn.isWithHistory())) { + if (isTrue(addColumn.isWithHistory()) && !sql2011History) { // make same changes to the history table String historyTable = historyTable(tableName); for (Column column : columns) { @@ -741,6 +755,15 @@ public class BaseTableDdl implements TableDdl { public void generate(DdlWrite writer, DropTable dropTable) throws IOException { dropTable(writer.apply(), dropTable.getName()); + + if (hasValue(dropTable.getSequenceCol()) + && platformDdl.getPlatform().getDbIdentity().isSupportsSequence()) { + String sequenceName = dropTable.getSequenceName(); + if (!hasValue(sequenceName)) { + sequenceName = namingConvention.getSequenceName(dropTable.getName(), dropTable.getSequenceCol()); + } + dropSequence(writer.apply(), sequenceName); + } } /** @@ -752,7 +775,7 @@ public class BaseTableDdl implements TableDdl { String tableName = dropColumn.getTableName(); alterTableDropColumn(writer.apply(), tableName, dropColumn.getColumnName()); - if (isTrue(dropColumn.isWithHistory())) { + if (isTrue(dropColumn.isWithHistory()) && !sql2011History) { // also drop from the history table regenerateHistoryTriggers(tableName, HistoryTableUpdate.Change.DROP, dropColumn.getColumnName()); alterTableDropColumn(writer.apply(), historyTable(tableName), dropColumn.getColumnName()); @@ -853,7 +876,7 @@ public class BaseTableDdl implements TableDdl { if (hasValue(ddl)) { writer.apply().append(ddl).endOfStatement(); - if (isTrue(alter.isWithHistory()) && alter.getType() != null) { + if (isTrue(alter.isWithHistory()) && alter.getType() != null && !sql2011History) { // mysql and sql server column type change allowing nulls in the history table column AlterColumn alterHistoryColumn = new AlterColumn(); alterHistoryColumn.setTableName(historyTable(alter.getTableName())); @@ -904,7 +927,7 @@ public class BaseTableDdl implements TableDdl { String ddl = platformDdl.alterColumnType(alter.getTableName(), alter.getColumnName(), alter.getType()); if (hasValue(ddl)) { writer.apply().append(ddl).endOfStatement(); - if (isTrue(alter.isWithHistory())) { + if (isTrue(alter.isWithHistory()) && !sql2011History) { // apply same type change to matching column in the history table ddl = platformDdl.alterColumnType(historyTable(alter.getTableName()), alter.getColumnName(), alter.getType()); writer.apply().append(ddl).endOfStatement(); @@ -970,17 +993,20 @@ public class BaseTableDdl implements TableDdl { protected void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { - - buffer.append("alter table ").append(tableName).append(" drop column ").append(columnName) - .endOfStatement(); + platformDdl.alterTableDropColumn(buffer, tableName, columnName); } protected void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column, boolean onHistoryTable) throws IOException { DdlMigrationHelp help = new DdlMigrationHelp(tableName, column); - help.writeBefore(buffer); + if (!onHistoryTable) { + help.writeBefore(buffer); + } + platformDdl.alterTableAddColumn(buffer, tableName, column, onHistoryTable, help.getDefaultValue()); - help.writeAfter(buffer); + if (!onHistoryTable) { + help.writeAfter(buffer); + } } protected boolean isFalse(Boolean value) { diff --git a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java index 08d7fc7f8..ca8a263b7 100644 --- a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java +++ b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java @@ -418,6 +418,11 @@ public class PlatformDdl { } } + + public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { + buffer.append("alter table ").append(tableName).append(" drop column ").append(columnName) + .endOfStatement(); + } /** * Return true if unique constraints for nullable columns can be inlined as normal. @@ -542,15 +547,19 @@ public class PlatformDdl { * Add table comment as a separate statement (from the create table statement). */ public void addTableComment(DdlBuffer apply, String tableName, String tableComment) throws IOException { - + if (DdlHelp.isDropComment(tableComment)) { + tableComment = ""; + } apply.append(String.format("comment on table %s is '%s'", tableName, tableComment)).endOfStatement(); } - + /** * Add column comment as a separate statement. */ public void addColumnComment(DdlBuffer apply, String table, String column, String comment) throws IOException { - + if (DdlHelp.isDropComment(comment)) { + comment = ""; + } apply.append(String.format("comment on column %s.%s is '%s'", table, column, comment)).endOfStatement(); } } diff --git a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerDdl.java b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerDdl.java index 5ca2a5b16..29815929f 100644 --- a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerDdl.java +++ b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerDdl.java @@ -25,7 +25,12 @@ public class SqlServerDdl extends PlatformDdl { @Override public String dropTable(String tableName) { - return "IF OBJECT_ID('" + tableName + "', 'U') IS NOT NULL drop table " + tableName; + StringBuilder buffer = new StringBuilder(); + buffer.append("IF OBJECT_ID('"); + buffer.append(tableName); + buffer.append("', 'U') IS NOT NULL drop table "); + buffer.append(tableName); + return buffer.toString(); } @Override @@ -76,7 +81,24 @@ public class SqlServerDdl extends PlatformDdl { } return sb.toString(); } - + + public String alterTableDropConstraint(String tableName, String constraintName) { + StringBuilder sb = new StringBuilder(); + sb.append("IF (OBJECT_ID('").append(constraintName).append("', 'C') IS NOT NULL) "); + sb.append(super.alterTableDropConstraint(tableName, constraintName)); + return sb.toString(); + } + /** + * Drop a unique constraint from the table (Sometimes this is an index). + */ + @Override + public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) { + StringBuilder sb = new StringBuilder(); + sb.append("IF (OBJECT_ID('").append(uniqueConstraintName).append("', 'UQ') IS NOT NULL) "); + sb.append(super.alterTableDropUniqueConstraint(tableName, uniqueConstraintName)).append(";\n"); + sb.append(dropIndex(uniqueConstraintName, tableName)); + return sb.toString(); + } /** * Generate and return the create sequence DDL. */ @@ -102,13 +124,24 @@ public class SqlServerDdl extends PlatformDdl { @Override public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) { - + // Unfortunately, the SqlServer creates default values with a random name. + // You can specify a name in DDL, but this does not work in conjunction with + // temporal tables in certain cases. So we have to delete the constraint with + // a rather complex statement. + StringBuilder sb = new StringBuilder(); if (DdlHelp.isDropDefault(defaultValue)) { - return "alter table " + tableName + " drop constraint df_" + tableName + "_" + columnName; + sb.append("delimiter $$\n"); + sb.append("DECLARE @Tmp nvarchar(200);"); + sb.append("select @Tmp = t1.name from sys.default_constraints t1\n"); + sb.append(" join sys.columns t2 on t1.object_id = t2.default_object_id\n"); + sb.append(" where t1.parent_object_id = OBJECT_ID('").append(tableName) + .append("') and t2.name = '").append(columnName).append("';\n"); + sb.append("if @Tmp is not null EXEC('alter table ").append(tableName).append(" drop constraint ' + @Tmp)$$"); } else { - return "alter table " + tableName + " add constraint df_" + tableName + "_" + columnName - + " default " + defaultValue + " for " + columnName; + sb.append("alter table ").append(tableName); + sb.append(" add default ").append(defaultValue).append(" for ").append(columnName); } + return sb.toString(); } @Override @@ -157,4 +190,28 @@ public class SqlServerDdl extends PlatformDdl { // do nothing for MS SQL Server (cause it requires stored procedures etc) } + + /** + * It is rather complex to delete a column on SqlServer as there must not exist any references + * (constraints, default values, indices and foreign keys). The list is not yet complete, as + * indices over multiple columns will not yet deleted. + * (This may be changed to delete all refering objects by using the sys.* tables later) + */ + @Override + public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { + buffer.append("-- drop column ").append(tableName).append(".").append(columnName).endOfStatement(); + + buffer.append(alterTableDropUniqueConstraint(tableName, naming.uniqueConstraintName(tableName, columnName))); + buffer.endOfStatement(); + buffer.append(alterColumnDefaultValue(tableName, columnName, DdlHelp.DROP_DEFAULT)); + buffer.endOfStatement(); + buffer.append(alterTableDropConstraint(tableName, naming.checkConstraintName(tableName, columnName))); + buffer.endOfStatement(); + buffer.append(dropIndex(naming.indexName(tableName, columnName), tableName)); + buffer.endOfStatement(); + buffer.append(alterTableDropForeignKey(tableName, naming.foreignKeyConstraintName(tableName, columnName))); + buffer.endOfStatement(); + super.alterTableDropColumn(buffer, tableName, columnName); + } + } diff --git a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java index fd4232678..b7c374117 100644 --- a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java +++ b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java @@ -1,5 +1,6 @@ package io.ebean.dbmigration.ddlgeneration.platform; +import io.ebean.config.DbConstraintNaming; import io.ebean.config.ServerConfig; import io.ebean.dbmigration.ddlgeneration.DdlBuffer; import io.ebean.dbmigration.ddlgeneration.DdlWrite; @@ -16,11 +17,13 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl { private String systemPeriodStart; private String systemPeriodEnd; + private PlatformDdl platformDdl; @Override public void configure(ServerConfig serverConfig, PlatformDdl platformDdl) { this.systemPeriodStart = serverConfig.getAsOfSysPeriod() + "From"; this.systemPeriodEnd = serverConfig.getAsOfSysPeriod() + "To"; + this.platformDdl = platformDdl; } @Override @@ -52,9 +55,19 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl { public void dropHistoryTable(DdlWrite writer, DropHistoryTable dropHistoryTable) throws IOException { String baseTable = dropHistoryTable.getBaseTable(); DdlBuffer apply = writer.applyHistory(); + apply.append("-- dropping history support for ").append(baseTable).endOfStatement(); + // drop default constraints + + apply.append(platformDdl.alterColumnDefaultValue(baseTable, systemPeriodStart, DdlHelp.DROP_DEFAULT)).endOfStatement(); + apply.append(platformDdl.alterColumnDefaultValue(baseTable, systemPeriodEnd, DdlHelp.DROP_DEFAULT)).endOfStatement(); + // switch of versioning & period apply.append("alter table ").append(baseTable).append(" set (system_versioning = off)").endOfStatement(); + apply.append("alter table ").append(baseTable).append(" drop period for system_time").endOfStatement(); + // now drop tables & columns apply.append("alter table ").append(baseTable).append(" drop column ").append(systemPeriodStart).endOfStatement(); apply.append("alter table ").append(baseTable).append(" drop column ").append(systemPeriodEnd).endOfStatement(); + apply.append("IF OBJECT_ID('").append(baseTable).append("_history', 'U') IS NOT NULL drop table ").append(baseTable).append("_history").endOfStatement(); + apply.end(); } @Override diff --git a/src/main/java/io/ebean/dbmigration/migration/Column.java b/src/main/java/io/ebean/dbmigration/migration/Column.java index 4a16eaa0f..26b995d31 100644 --- a/src/main/java/io/ebean/dbmigration/migration/Column.java +++ b/src/main/java/io/ebean/dbmigration/migration/Column.java @@ -9,7 +9,6 @@ import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.XmlValue; /** diff --git a/src/main/java/io/ebean/dbmigration/migration/DropTable.java b/src/main/java/io/ebean/dbmigration/migration/DropTable.java index 69cdbe1d2..77d54d892 100644 --- a/src/main/java/io/ebean/dbmigration/migration/DropTable.java +++ b/src/main/java/io/ebean/dbmigration/migration/DropTable.java @@ -29,6 +29,10 @@ public class DropTable { @XmlAttribute(name = "name", required = true) protected String name; + @XmlAttribute(name = "sequenceCol") + protected String sequenceCol; + @XmlAttribute(name = "sequenceName") + protected String sequenceName; /** * Gets the value of the name property. @@ -50,4 +54,44 @@ public class DropTable { this.name = value; } + /** + * Gets the value of the sequenceCol property. + * + * @return possible object is + * {@link String } + */ + public String getSequenceCol() { + return sequenceCol; + } + + /** + * Gets the value of the sequenceName property. + * + * @return possible object is + * {@link String } + */ + public String getSequenceName() { + return sequenceName; + } + + /** + * Sets the value of the sequenceCol property. + * + * @param value allowed object is + * {@link String } + */ + public void setSequenceCol(String value) { + this.sequenceCol = value; + } + + /** + * Sets the value of the sequenceName property. + * + * @param value allowed object is + * {@link String } + */ + public void setSequenceName(String value) { + this.sequenceName = value; + } + } diff --git a/src/main/java/io/ebean/dbmigration/migration/ObjectFactory.java b/src/main/java/io/ebean/dbmigration/migration/ObjectFactory.java index 7421a09ca..dd672488a 100644 --- a/src/main/java/io/ebean/dbmigration/migration/ObjectFactory.java +++ b/src/main/java/io/ebean/dbmigration/migration/ObjectFactory.java @@ -186,5 +186,13 @@ public class ObjectFactory { public Migration createMigration() { return new Migration(); } + + /** + * Create an instance of {@link DdlScript } + */ + public DdlScript createDdlScript() { + return new DdlScript(); + } + } diff --git a/src/main/java/io/ebean/dbmigration/model/MColumn.java b/src/main/java/io/ebean/dbmigration/model/MColumn.java index 7915c09f1..fbea21234 100644 --- a/src/main/java/io/ebean/dbmigration/model/MColumn.java +++ b/src/main/java/io/ebean/dbmigration/model/MColumn.java @@ -2,8 +2,6 @@ package io.ebean.dbmigration.model; import java.util.List; -import javax.sound.midi.MidiDevice.Info; - import io.ebean.dbmigration.ddlgeneration.platform.DdlHelp; import io.ebean.dbmigration.migration.AlterColumn; import io.ebean.dbmigration.migration.Column; diff --git a/src/main/java/io/ebean/dbmigration/model/MTable.java b/src/main/java/io/ebean/dbmigration/model/MTable.java index 16f0a6206..515a8d264 100644 --- a/src/main/java/io/ebean/dbmigration/model/MTable.java +++ b/src/main/java/io/ebean/dbmigration/model/MTable.java @@ -1,5 +1,6 @@ package io.ebean.dbmigration.model; +import io.ebean.dbmigration.ddlgeneration.platform.DdlHelp; import io.ebean.dbmigration.migration.AddColumn; import io.ebean.dbmigration.migration.AddHistoryTable; import io.ebean.dbmigration.migration.AddTableComment; @@ -180,6 +181,24 @@ public class MTable { public DropTable dropTable() { DropTable dropTable = new DropTable(); dropTable.setName(name); + // we must add pk col name & sequence name, as we have to delete the sequence also. + if (identityType != IdentityType.GENERATOR && identityType != IdentityType.EXTERNAL) { + String pkCol = null; + for (MColumn column : columns.values()) { + if (column.isPrimaryKey()) { + if (pkCol == null) { + pkCol = column.getName(); + } else { // multiple pk cols -> no sequence + pkCol = null; + break; + } + } + } + if (pkCol != null) { + dropTable.setSequenceCol(pkCol); + dropTable.setSequenceName(sequenceName); + } + } return dropTable; } @@ -288,7 +307,11 @@ public class MTable { if (MColumn.different(comment, newTable.comment)) { AddTableComment addTableComment = new AddTableComment(); addTableComment.setName(name); - addTableComment.setComment(newTable.comment); + if (newTable.comment == null) { + addTableComment.setComment(DdlHelp.DROP_COMMENT); + } else { + addTableComment.setComment(newTable.comment); + } modelDiff.addTableComment(addTableComment); } } diff --git a/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java b/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java index 7b9439974..d54e2a17d 100644 --- a/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java +++ b/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java @@ -1,5 +1,7 @@ package io.ebean.dbmigration.ddlgeneration.platform; +import io.ebean.Ebean; +import io.ebean.config.ServerConfig; import io.ebean.config.dbplatform.h2.H2Platform; import io.ebean.config.dbplatform.sqlserver.SqlServerPlatform; import io.ebean.config.dbplatform.mysql.MySqlPlatform; @@ -11,7 +13,6 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; public class PlatformDdl_AlterColumnTest { @@ -21,6 +22,10 @@ public class PlatformDdl_AlterColumnTest { PlatformDdl oraDdl = new OraclePlatform().getPlatformDdl(); PlatformDdl sqlServerDdl = new SqlServerPlatform().getPlatformDdl(); + { + ServerConfig serverConfig = Ebean.getDefaultServer().getPluginApi().getServerConfig(); + sqlServerDdl.configure(serverConfig); + } AlterColumn alterNotNull() { AlterColumn alterColumn = new AlterColumn(); alterColumn.setTableName("mytab"); @@ -157,7 +162,7 @@ public class PlatformDdl_AlterColumnTest { assertEquals("alter table mytab alter acol set default 'hi'", sql); sql = sqlServerDdl.alterColumnDefaultValue("mytab", "acol", "'hi'"); - assertEquals("alter table mytab add constraint df_mytab_acol default 'hi' for acol", sql); + assertEquals("alter table mytab add default 'hi' for acol", sql); } @Test @@ -176,7 +181,7 @@ public class PlatformDdl_AlterColumnTest { assertEquals("alter table mytab alter acol drop default", sql); sql = sqlServerDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT"); - assertEquals("alter table mytab drop constraint df_mytab_acol", sql); + assertThat(sql).startsWith("delimiter $$").endsWith("$$"); } } diff --git a/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java b/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java index 5462b62e5..235533071 100644 --- a/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java +++ b/src/test/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java @@ -28,7 +28,8 @@ public class PlatformDdl_dropUniqueConstraintTest { sql = oraDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); assertEquals("alter table mytab drop constraint uq_name", sql); sql = sqlServerDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); - assertEquals("alter table mytab drop constraint uq_name", sql); + assertEquals("IF (OBJECT_ID('uq_name', 'UQ') IS NOT NULL) alter table mytab drop constraint uq_name;\n" + + "IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('mytab','U') AND name = 'uq_name') drop index uq_name ON mytab", sql); sql = mysqlDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); diff --git a/src/test/java/misc/migration/v1_0/EHistory2.java b/src/test/java/misc/migration/v1_0/EHistory2.java new file mode 100644 index 000000000..1cdf859c3 --- /dev/null +++ b/src/test/java/misc/migration/v1_0/EHistory2.java @@ -0,0 +1,20 @@ +package misc.migration.v1_0; + + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import io.ebean.annotation.History; + +@Entity +@Table(name = "migtest_e_history2") +@History +public class EHistory2 { + + @Id + Integer id; + + + String testString; +} diff --git a/src/test/java/misc/migration/v1_0/ESoftdelete.java b/src/test/java/misc/migration/v1_0/ESoftdelete.java new file mode 100644 index 000000000..58d9a0af0 --- /dev/null +++ b/src/test/java/misc/migration/v1_0/ESoftdelete.java @@ -0,0 +1,17 @@ +package misc.migration.v1_0; + + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "migtest_e_softdelete") +public class ESoftdelete { + + @Id + Integer id; + + + String testString; +} diff --git a/src/test/java/misc/migration/v1_1/EHistory2.java b/src/test/java/misc/migration/v1_1/EHistory2.java new file mode 100644 index 000000000..1e1fe4ebe --- /dev/null +++ b/src/test/java/misc/migration/v1_1/EHistory2.java @@ -0,0 +1,29 @@ +package misc.migration.v1_1; + + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import io.ebean.annotation.DbDefault; +import io.ebean.annotation.History; +import io.ebean.annotation.NotNull; + +@Entity +@Table(name = "migtest_e_history2") +@History +public class EHistory2 { + + @Id + Integer id; + + @NotNull + @DbDefault("unknown") + String testString; + + String testString2; + + @NotNull + @DbDefault("unknown") + String testString3; +} diff --git a/src/test/java/misc/migration/v1_1/ESoftdelete.java b/src/test/java/misc/migration/v1_1/ESoftdelete.java new file mode 100644 index 000000000..3213ddcd3 --- /dev/null +++ b/src/test/java/misc/migration/v1_1/ESoftdelete.java @@ -0,0 +1,22 @@ +package misc.migration.v1_1; + + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import io.ebean.annotation.SoftDelete; + +@Entity +@Table(name = "migtest_e_softdelete") +public class ESoftdelete { + + @Id + Integer id; + + + String testString; + + @SoftDelete + boolean deleted; +} diff --git a/src/test/java/misc/migration/v1_2/EHistory2.java b/src/test/java/misc/migration/v1_2/EHistory2.java new file mode 100644 index 000000000..d5b64ae70 --- /dev/null +++ b/src/test/java/misc/migration/v1_2/EHistory2.java @@ -0,0 +1,20 @@ +package misc.migration.v1_2; + + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import io.ebean.annotation.History; + +@Entity +@Table(name = "migtest_e_history2") +@History +public class EHistory2 { + + @Id + Integer id; + + + String testString; +} diff --git a/src/test/java/misc/migration/v1_2/ESoftdelete.java b/src/test/java/misc/migration/v1_2/ESoftdelete.java new file mode 100644 index 000000000..5987135e3 --- /dev/null +++ b/src/test/java/misc/migration/v1_2/ESoftdelete.java @@ -0,0 +1,17 @@ +package misc.migration.v1_2; + + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "migtest_e_softdelete") +public class ESoftdelete { + + @Id + Integer id; + + + String testString; +} diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/db2/1.0__initial.sql index eb452c747..8cb33ca98 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.0__initial.sql @@ -27,11 +27,23 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id integer generated by default as identity not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id integer generated by default as identity not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id integer generated by default as identity not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id) on delete restrict on update restrict; diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.1.sql b/src/test/resources/dbmigration/migrationtest/db2/1.1.sql index 2a11c8390..6e54e10e2 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.1.sql @@ -33,6 +33,17 @@ alter table migtest_e_basic add column new_integer integer not null default 42; comment on column migtest_e_history.test_string is 'Column altered to long now'; alter table migtest_e_history alter column test_string bigint; comment on table migtest_e_history is 'We have history now'; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 alter column test_string set default 'unknown'; +alter table migtest_e_history2 alter column test_string set not null; +alter table migtest_e_history2 add column test_string2 varchar(255); +alter table migtest_e_history2 add column test_string3 varchar(255) not null default 'unknown'; +alter table migtest_e_history2_history add column test_string2 varchar(255); +alter table migtest_e_history2_history add column test_string3 varchar(255); + +alter table migtest_e_softdelete add column deleted boolean not null default false; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index ix_migtest_e_basic_indextest1; diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/db2/1.2__dropsFor_1.1.sql index d0243323d..81b4fe749 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.2__dropsFor_1.1.sql @@ -6,3 +6,4 @@ alter table migtest_e_basic drop column old_boolean2; alter table migtest_e_basic drop column eref_id; drop table migtest_e_ref; +drop sequence migtest_e_ref_seq; diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.3.sql b/src/test/resources/dbmigration/migrationtest/db2/1.3.sql index d3cc83024..e1ff6206a 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.3.sql @@ -20,7 +20,10 @@ alter table migtest_e_basic add column old_boolean boolean not null default fals alter table migtest_e_basic add column old_boolean2 boolean; alter table migtest_e_basic add column eref_id integer; -comment on column migtest_e_history.test_string is 'DROP COMMENT'; +comment on column migtest_e_history.test_string is ''; +comment on table migtest_e_history is ''; +alter table migtest_e_history2 alter column test_string drop default; +alter table migtest_e_history2 alter column test_string set null; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index ix_migtest_e_basic_indextest3; diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/db2/1.4__dropsFor_1.3.sql index 6d20a02da..f4cb4ebd8 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.4__dropsFor_1.3.sql @@ -9,4 +9,13 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table migtest_e_user; +drop sequence migtest_e_user_seq; diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/h2/1.0__initial.sql index a56745884..92ce9e535 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.0__initial.sql @@ -27,13 +27,36 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id integer auto_increment not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id integer auto_increment not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id integer auto_increment not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id) on delete restrict on update restrict; create index ix_migtest_e_basic_eref_id on migtest_e_basic (eref_id); +alter table migtest_e_history2 add column sys_period_start datetime(6) default now(6); +alter table migtest_e_history2 add column sys_period_end datetime(6); +create table migtest_e_history2_history( + id integer, + test_string varchar(255), + sys_period_start datetime(6), + sys_period_end datetime(6) +); +create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history; + +create trigger migtest_e_history2_history_upd before update,delete on migtest_e_history2 for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger"; diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.1.sql b/src/test/resources/dbmigration/migrationtest/h2/1.1.sql index 88e60af97..88dc14a3d 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.1.sql @@ -33,6 +33,17 @@ alter table migtest_e_basic add column new_integer integer not null default 42; comment on column migtest_e_history.test_string is 'Column altered to long now'; alter table migtest_e_history alter column test_string bigint; comment on table migtest_e_history is 'We have history now'; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 alter column test_string set default 'unknown'; +alter table migtest_e_history2 alter column test_string set not null; +alter table migtest_e_history2 add column test_string2 varchar(255); +alter table migtest_e_history2 add column test_string3 varchar(255) not null default 'unknown'; +alter table migtest_e_history2_history add column test_string2 varchar(255); +alter table migtest_e_history2_history add column test_string3 varchar(255); + +alter table migtest_e_softdelete add column deleted boolean not null default false; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index if exists ix_migtest_e_basic_indextest1; @@ -48,3 +59,6 @@ create table migtest_e_history_history( create view migtest_e_history_with_history as select * from migtest_e_history union all select * from migtest_e_history_history; create trigger migtest_e_history_history_upd before update,delete on migtest_e_history for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger"; +-- changes: [add test_string2, add test_string3] +drop trigger migtest_e_history2_history_upd; +create trigger migtest_e_history2_history_upd before update,delete on migtest_e_history2 for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger"; diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/h2/1.2__dropsFor_1.1.sql index 05a3f449e..652aa4dcb 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.2__dropsFor_1.1.sql @@ -6,3 +6,4 @@ alter table migtest_e_basic drop column old_boolean2; alter table migtest_e_basic drop column eref_id; drop table if exists migtest_e_ref; +drop sequence if exists migtest_e_ref_seq; diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.3.sql b/src/test/resources/dbmigration/migrationtest/h2/1.3.sql index 574b9a362..3c1834cc4 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.3.sql @@ -20,7 +20,10 @@ alter table migtest_e_basic add column old_boolean boolean not null default fals alter table migtest_e_basic add column old_boolean2 boolean; alter table migtest_e_basic add column eref_id integer; -comment on column migtest_e_history.test_string is 'DROP COMMENT'; +comment on column migtest_e_history.test_string is ''; +comment on table migtest_e_history is ''; +alter table migtest_e_history2 alter column test_string drop default; +alter table migtest_e_history2 alter column test_string set null; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index if exists ix_migtest_e_basic_indextest3; diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/h2/1.4__dropsFor_1.3.sql index 1179896ec..ca2eda59b 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.4__dropsFor_1.3.sql @@ -17,4 +17,16 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table if exists migtest_e_user; +drop sequence if exists migtest_e_user_seq; +-- changes: [drop test_string2, drop test_string3] +drop trigger migtest_e_history2_history_upd; +create trigger migtest_e_history2_history_upd before update,delete on migtest_e_history2 for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger"; diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.0__initial.sql index 6eb8ba786..98a8aefd4 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.0__initial.sql @@ -27,11 +27,23 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id integer generated by default as identity (start with 1) not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id integer generated by default as identity (start with 1) not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id integer generated by default as identity (start with 1) not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id) on delete restrict on update restrict; diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql index 1d17c55a6..b83f9267e 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql @@ -33,6 +33,17 @@ alter table migtest_e_basic add column new_integer integer not null default 42; comment on column migtest_e_history.test_string is 'Column altered to long now'; alter table migtest_e_history alter column test_string bigint; comment on table migtest_e_history is 'We have history now'; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 alter column test_string set default 'unknown'; +alter table migtest_e_history2 alter column test_string set not null; +alter table migtest_e_history2 add column test_string2 varchar(255); +alter table migtest_e_history2 add column test_string3 varchar(255) not null default 'unknown'; +alter table migtest_e_history2_history add column test_string2 varchar(255); +alter table migtest_e_history2_history add column test_string3 varchar(255); + +alter table migtest_e_softdelete add column deleted boolean not null default false; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index if exists ix_migtest_e_basic_indextest1; diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.2__dropsFor_1.1.sql index 05a3f449e..652aa4dcb 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.2__dropsFor_1.1.sql @@ -6,3 +6,4 @@ alter table migtest_e_basic drop column old_boolean2; alter table migtest_e_basic drop column eref_id; drop table if exists migtest_e_ref; +drop sequence if exists migtest_e_ref_seq; diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql index cca1c04e6..29dae3bf0 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql @@ -20,7 +20,10 @@ alter table migtest_e_basic add column old_boolean boolean not null default fals alter table migtest_e_basic add column old_boolean2 boolean; alter table migtest_e_basic add column eref_id integer; -comment on column migtest_e_history.test_string is 'DROP COMMENT'; +comment on column migtest_e_history.test_string is ''; +comment on table migtest_e_history is ''; +alter table migtest_e_history2 alter column test_string drop default; +alter table migtest_e_history2 alter column test_string set null; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index if exists ix_migtest_e_basic_indextest3; diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.4__dropsFor_1.3.sql index 203511771..9e22e4a76 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.4__dropsFor_1.3.sql @@ -9,4 +9,13 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table if exists migtest_e_user; +drop sequence if exists migtest_e_user_seq; diff --git a/src/test/resources/dbmigration/migrationtest/model/1.0__initial.model.xml b/src/test/resources/dbmigration/migrationtest/model/1.0__initial.model.xml index d57e29028..a01d7db52 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.0__initial.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.0__initial.model.xml @@ -25,9 +25,17 @@ + + + + + + + + diff --git a/src/test/resources/dbmigration/migrationtest/model/1.1.model.xml b/src/test/resources/dbmigration/migrationtest/model/1.1.model.xml index f8773a5bb..bf86d2450 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.1.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.1.model.xml @@ -24,6 +24,14 @@ alter table ${table} alter column ${column} TYPE bigint USING (${column}::integer) + + + + + + + + @@ -36,6 +44,6 @@ - + \ No newline at end of file diff --git a/src/test/resources/dbmigration/migrationtest/model/1.2__dropsFor_1.1.model.xml b/src/test/resources/dbmigration/migrationtest/model/1.2__dropsFor_1.1.model.xml index cf5d7dda9..aff7eb878 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.2__dropsFor_1.1.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.2__dropsFor_1.1.model.xml @@ -5,6 +5,6 @@ - + \ No newline at end of file diff --git a/src/test/resources/dbmigration/migrationtest/model/1.3.model.xml b/src/test/resources/dbmigration/migrationtest/model/1.3.model.xml index 7e66df907..2d08341e2 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.3.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.3.model.xml @@ -12,7 +12,8 @@ - + + @@ -28,6 +29,9 @@ - + + + + \ No newline at end of file diff --git a/src/test/resources/dbmigration/migrationtest/model/1.4__dropsFor_1.3.model.xml b/src/test/resources/dbmigration/migrationtest/model/1.4__dropsFor_1.3.model.xml index 398853857..214b60a2a 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.4__dropsFor_1.3.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.4__dropsFor_1.3.model.xml @@ -8,6 +8,9 @@ - + + + + \ No newline at end of file diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.0__initial.sql index 0b3ef8584..8fe02c94c 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.0__initial.sql @@ -27,13 +27,44 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id integer auto_increment not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id integer auto_increment not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id integer auto_increment not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id) on delete restrict on update restrict; create index ix_migtest_e_basic_eref_id on migtest_e_basic (eref_id); +alter table migtest_e_history2 add column sys_period_start datetime(6) default now(6); +alter table migtest_e_history2 add column sys_period_end datetime(6); +create table migtest_e_history2_history( + id integer, + test_string varchar(255), + sys_period_start datetime(6), + sys_period_end datetime(6) +); +create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history; + +delimiter $$ +create trigger migtest_e_history2_history_upd before update on migtest_e_history2 for each row begin + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string); + set NEW.sys_period_start = now(6); +end$$ +delimiter $$ +create trigger migtest_e_history2_history_del before delete on migtest_e_history2 for each row begin + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string); +end$$ diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql index ede5c47ff..3299a9517 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql @@ -32,6 +32,17 @@ alter table migtest_e_basic add column new_integer integer not null default 42; comment on column migtest_e_history.test_string is 'Column altered to long now'; alter table migtest_e_history modify test_string bigint; alter table migtest_e_history comment = 'We have history now'; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 alter test_string set default 'unknown'; +alter table migtest_e_history2 modify test_string varchar(255) not null; +alter table migtest_e_history2 add column test_string2 varchar(255); +alter table migtest_e_history2 add column test_string3 varchar(255) not null default 'unknown'; +alter table migtest_e_history2_history add column test_string2 varchar(255); +alter table migtest_e_history2_history add column test_string3 varchar(255); + +alter table migtest_e_softdelete add column deleted tinyint(1) default 0 not null; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index ix_migtest_e_basic_indextest1 on migtest_e_basic; @@ -55,3 +66,17 @@ delimiter $$ create trigger migtest_e_history_history_del before delete on migtest_e_history for each row begin insert into migtest_e_history_history (sys_period_start,sys_period_end,id, test_string) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string); end$$ +-- changes: [add test_string2, add test_string3] +lock tables migtest_e_history2 write; +drop trigger migtest_e_history2_history_upd; +drop trigger migtest_e_history2_history_del; +delimiter $$ +create trigger migtest_e_history2_history_upd before update on migtest_e_history2 for each row begin + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string2, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string2, OLD.test_string3); + set NEW.sys_period_start = now(6); +end$$ +delimiter $$ +create trigger migtest_e_history2_history_del before delete on migtest_e_history2 for each row begin + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string2, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string2, OLD.test_string3); +end$$ +unlock tables; diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.3.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.3.sql index 58f7fef6e..90f5f63d6 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.3.sql @@ -17,7 +17,9 @@ alter table migtest_e_basic add column old_boolean tinyint(1) default 0 not null alter table migtest_e_basic add column old_boolean2 tinyint(1) default 0; alter table migtest_e_basic add column eref_id integer; -comment on column migtest_e_history.test_string is 'DROP COMMENT'; +comment on column migtest_e_history.test_string is ''; +alter table migtest_e_history comment = 'DROP COMMENT'; +alter table migtest_e_history2 alter test_string drop default; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index ix_migtest_e_basic_indextest3 on migtest_e_basic; diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql index 50d0d6bb0..f7207d883 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.4__dropsFor_1.3.sql @@ -18,4 +18,26 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table if exists migtest_e_user; +-- changes: [drop test_string2, drop test_string3] +lock tables migtest_e_history2 write; +drop trigger migtest_e_history2_history_upd; +drop trigger migtest_e_history2_history_del; +delimiter $$ +create trigger migtest_e_history2_history_upd before update on migtest_e_history2 for each row begin + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string); + set NEW.sys_period_start = now(6); +end$$ +delimiter $$ +create trigger migtest_e_history2_history_del before delete on migtest_e_history2 for each row begin + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string); +end$$ +unlock tables; diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.0__initial.sql index 42144bdeb..439cc5a00 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.0__initial.sql @@ -29,12 +29,26 @@ create table migtest_e_history ( ); create sequence migtest_e_history_seq; +create table migtest_e_history2 ( + id number(10) not null, + test_string varchar2(255), + constraint pk_migtest_e_history2 primary key (id) +); +create sequence migtest_e_history2_seq; + create table migtest_e_ref ( id number(10) not null, constraint pk_migtest_e_ref primary key (id) ); create sequence migtest_e_ref_seq; +create table migtest_e_softdelete ( + id number(10) not null, + test_string varchar2(255), + constraint pk_migtest_e_softdelete primary key (id) +); +create sequence migtest_e_softdelete_seq; + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id); diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql index 1dd8204ce..23faa95f0 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql @@ -34,6 +34,15 @@ alter table migtest_e_basic add column new_integer number(10) not null default 4 comment on column migtest_e_history.test_string is 'Column altered to long now'; alter table migtest_e_history modify test_string number(19); comment on table migtest_e_history is 'We have history now'; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 modify test_string default 'unknown'; +alter table migtest_e_history2 modify test_string not null; +alter table migtest_e_history2 add column test_string2 varchar2(255); +alter table migtest_e_history2 add column test_string3 varchar2(255) not null default 'unknown'; + +alter table migtest_e_softdelete add column deleted number(1) default 0 not null; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index ix_migtest_e_basic_indextest1; diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.2__dropsFor_1.1.sql index 212351c21..0e8f695a2 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.2__dropsFor_1.1.sql @@ -6,3 +6,4 @@ alter table migtest_e_basic drop column old_boolean2; alter table migtest_e_basic drop column eref_id; drop table migtest_e_ref cascade constraints purge; +drop sequence migtest_e_ref_seq; diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql index db710a973..ab7ac0adc 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql @@ -21,7 +21,10 @@ alter table migtest_e_basic add column old_boolean number(1) default 0 not null; alter table migtest_e_basic add column old_boolean2 number(1) default 0; alter table migtest_e_basic add column eref_id number(10); -comment on column migtest_e_history.test_string is 'DROP COMMENT'; +comment on column migtest_e_history.test_string is ''; +comment on table migtest_e_history is ''; +alter table migtest_e_history2 modify test_string drop default; +alter table migtest_e_history2 modify test_string null; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index ix_migtest_e_basic_indextest3; diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.4__dropsFor_1.3.sql index 119695b0e..5337f9fe6 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.4__dropsFor_1.3.sql @@ -9,4 +9,11 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table migtest_e_user cascade constraints purge; +drop sequence migtest_e_user_seq; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql index 1e2cc3b18..372bcf4ba 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.0__initial.sql @@ -27,13 +27,46 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id serial not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id serial not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id serial not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id) on delete restrict on update restrict; create index ix_migtest_e_basic_eref_id on migtest_e_basic (eref_id); +alter table migtest_e_history2 add column sys_period tstzrange not null default tstzrange(current_timestamp, null); +create table migtest_e_history2_history(like migtest_e_history2); +create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history; + +create or replace function migtest_e_history2_history_version() returns trigger as $$ +begin + if (TG_OP = 'UPDATE') then + insert into migtest_e_history2_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + NEW.sys_period = tstzrange(current_timestamp,null); + return new; + elsif (TG_OP = 'DELETE') then + insert into migtest_e_history2_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + return old; + end if; +end; +$$ LANGUAGE plpgsql; + +create trigger migtest_e_history2_history_upd + before update or delete on migtest_e_history2 + for each row execute procedure migtest_e_history2_history_version(); + diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql index 549e99d53..6838be60f 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql @@ -1,3 +1,6 @@ +-- drop dependencies +drop view if exists migtest_e_history2_with_history; + -- apply changes create table migtest_e_user ( id serial not null, @@ -35,6 +38,17 @@ alter table migtest_e_history alter column test_string TYPE bigint USING (test_s comment on column migtest_e_history.test_string is 'Column altered to long now'; alter table migtest_e_history alter column test_string type bigint; comment on table migtest_e_history is 'We have history now'; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 alter column test_string set default 'unknown'; +alter table migtest_e_history2 alter column test_string set not null; +alter table migtest_e_history2 add column test_string2 varchar(255); +alter table migtest_e_history2 add column test_string3 varchar(255) not null default 'unknown'; +alter table migtest_e_history2_history add column test_string2 varchar(255); +alter table migtest_e_history2_history add column test_string3 varchar(255); + +alter table migtest_e_softdelete add column deleted boolean not null default false; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index if exists ix_migtest_e_basic_indextest1; @@ -60,3 +74,19 @@ create trigger migtest_e_history_history_upd before update or delete on migtest_e_history for each row execute procedure migtest_e_history_history_version(); +-- changes: [add test_string2, add test_string3] +create or replace view migtest_e_history2_with_history as select id, test_string, test_string2, test_string3, sys_period from migtest_e_history2 union all select id, test_string, test_string2, test_string3, sys_period from migtest_e_history2_history; + +create or replace function migtest_e_history2_history_version() returns trigger as $$ +begin + if (TG_OP = 'UPDATE') then + insert into migtest_e_history2_history (sys_period,id, test_string, test_string2, test_string3) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.test_string2, OLD.test_string3); + NEW.sys_period = tstzrange(current_timestamp,null); + return new; + elsif (TG_OP = 'DELETE') then + insert into migtest_e_history2_history (sys_period,id, test_string, test_string2, test_string3) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string, OLD.test_string2, OLD.test_string3); + return old; + end if; +end; +$$ LANGUAGE plpgsql; + diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql index 3f4a147db..b6fdce9f1 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.2__dropsFor_1.1.sql @@ -6,3 +6,4 @@ alter table migtest_e_basic drop column old_boolean2; alter table migtest_e_basic drop column eref_id; drop table if exists migtest_e_ref cascade; +drop sequence if exists migtest_e_ref_seq; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql index 46835021a..7dc779bc8 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql @@ -20,7 +20,10 @@ alter table migtest_e_basic add column old_boolean boolean not null default fals alter table migtest_e_basic add column old_boolean2 boolean; alter table migtest_e_basic add column eref_id integer; -comment on column migtest_e_history.test_string is 'DROP COMMENT'; +comment on column migtest_e_history.test_string is ''; +comment on table migtest_e_history is ''; +alter table migtest_e_history2 alter column test_string drop default; +alter table migtest_e_history2 alter column test_string drop not null; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index if exists ix_migtest_e_basic_indextest3; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql index 245eee0f5..105207717 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.4__dropsFor_1.3.sql @@ -6,6 +6,7 @@ drop view migtest_e_history_with_history; alter table migtest_e_history drop column sys_period; drop table migtest_e_history_history; +drop view if exists migtest_e_history2_with_history; -- apply changes alter table migtest_e_basic drop column new_string_field; @@ -18,4 +19,29 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table if exists migtest_e_user cascade; +drop sequence if exists migtest_e_user_seq; +-- changes: [drop test_string2, drop test_string3] +create or replace view migtest_e_history2_with_history as select id, test_string, sys_period from migtest_e_history2 union all select id, test_string, sys_period from migtest_e_history2_history; + +create or replace function migtest_e_history2_history_version() returns trigger as $$ +begin + if (TG_OP = 'UPDATE') then + insert into migtest_e_history2_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + NEW.sys_period = tstzrange(current_timestamp,null); + return new; + elsif (TG_OP = 'DELETE') then + insert into migtest_e_history2_history (sys_period,id, test_string) values (tstzrange(lower(OLD.sys_period), current_timestamp), OLD.id, OLD.test_string); + return old; + end if; +end; +$$ LANGUAGE plpgsql; + diff --git a/src/test/resources/dbmigration/migrationtest/sqlite/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/sqlite/1.0__initial.sql index 02c62cdff..88d329a2b 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.0__initial.sql @@ -28,10 +28,22 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id integer not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id integer not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id integer not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); diff --git a/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql index c7ee1643b..c649f78b8 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql @@ -30,6 +30,17 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_progress check ( p alter table migtest_e_basic add column new_integer integer not null default 42; alter table migtest_e_history alter column test_string integer; + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 alter column test_string set default 'unknown'; +alter table migtest_e_history2 alter column test_string set not null; +alter table migtest_e_history2 add column test_string2 varchar(255); +alter table migtest_e_history2 add column test_string3 varchar(255) not null default 'unknown'; +alter table migtest_e_history2_history add column test_string2 varchar(255); +alter table migtest_e_history2_history add column test_string3 varchar(255); + +alter table migtest_e_softdelete add column deleted int default 0 not null; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); drop index if exists ix_migtest_e_basic_indextest1; diff --git a/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql index 5509970df..7593d6696 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql @@ -20,6 +20,8 @@ alter table migtest_e_basic add column old_boolean int default 0 not null; alter table migtest_e_basic add column old_boolean2 int default 0; alter table migtest_e_basic add column eref_id integer; +alter table migtest_e_history2 alter column test_string drop default; +alter table migtest_e_history2 alter column test_string set null; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); drop index if exists ix_migtest_e_basic_indextest3; diff --git a/src/test/resources/dbmigration/migrationtest/sqlite/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlite/1.4__dropsFor_1.3.sql index 203511771..957772584 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.4__dropsFor_1.3.sql @@ -9,4 +9,12 @@ alter table migtest_e_basic drop column progress; alter table migtest_e_basic drop column new_integer; +alter table migtest_e_history2 drop column test_string2; +alter table migtest_e_history2_history drop column test_string2; + +alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2_history drop column test_string3; + +alter table migtest_e_softdelete drop column deleted; + drop table if exists migtest_e_user; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver/1.0__initial.sql b/src/test/resources/dbmigration/migrationtest/sqlserver/1.0__initial.sql index ce13b6a8f..39fff72cf 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver/1.0__initial.sql @@ -27,13 +27,30 @@ create table migtest_e_history ( constraint pk_migtest_e_history primary key (id) ); +create table migtest_e_history2 ( + id integer identity(1,1) not null, + test_string varchar(255), + constraint pk_migtest_e_history2 primary key (id) +); + create table migtest_e_ref ( id integer identity(1,1) not null, constraint pk_migtest_e_ref primary key (id) ); +create table migtest_e_softdelete ( + id integer identity(1,1) not null, + test_string varchar(255), + constraint pk_migtest_e_softdelete primary key (id) +); + create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); alter table migtest_e_basic add constraint fk_migtest_e_basic_eref_id foreign key (eref_id) references migtest_e_ref (id); create index ix_migtest_e_basic_eref_id on migtest_e_basic (eref_id); +alter table migtest_e_history2 + add sys_periodFrom datetime2 GENERATED ALWAYS AS ROW START NOT NULL DEFAULT SYSUTCDATETIME(), + sys_periodTo datetime2 GENERATED ALWAYS AS ROW END NOT NULL DEFAULT '9999-12-31T23:59:59.9999999', +period for system_time (sys_periodFrom, sys_periodTo); +alter table migtest_e_history2 set (system_versioning = on (history_table=dbo.migtest_e_history2_history)); diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver/1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlserver/1.1.sql index 8b8465ea1..07aaf23d4 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver/1.1.sql @@ -6,8 +6,8 @@ create table migtest_e_user ( update migtest_e_basic set status = 'A' where status is null; -alter table migtest_e_basic drop constraint ck_migtest_e_basic_status; -alter table migtest_e_basic add constraint df_migtest_e_basic_status default 'A' for status; +IF (OBJECT_ID('ck_migtest_e_basic_status', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_status; +alter table migtest_e_basic add default 'A' for status; alter table migtest_e_basic alter column status varchar(1) not null; alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I','?')); @@ -15,7 +15,7 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta create unique nonclustered index uq_migtest_e_basic_description on migtest_e_basic(description) where description is not null; update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic add constraint df_migtest_e_basic_some_date default '2000-01-01T00:00:00' for some_date; +alter table migtest_e_basic add default '2000-01-01T00:00:00' for some_date; alter table migtest_e_basic alter column some_date datetime2 not null; insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; @@ -31,6 +31,15 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_progress check ( p alter table migtest_e_basic add new_integer integer not null default 42; alter table migtest_e_history alter column test_string numeric(19); + +update migtest_e_history2 set test_string = 'unknown' where test_string is null; +alter table migtest_e_history2 add default 'unknown' for test_string; +alter table migtest_e_history2 alter column test_string varchar(255) not null; +alter table migtest_e_history2 add test_string2 varchar(255); +alter table migtest_e_history2 add test_string3 varchar(255) not null default 'unknown'; + +alter table migtest_e_softdelete add deleted bit default 0 not null; + create index ix_migtest_e_basic_indextest3 on migtest_e_basic (indextest3); create index ix_migtest_e_basic_indextest6 on migtest_e_basic (indextest6); IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_indextest1') drop index ix_migtest_e_basic_indextest1 ON migtest_e_basic; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlserver/1.2__dropsFor_1.1.sql index 08a80a5e5..b0b4bdf40 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver/1.2__dropsFor_1.1.sql @@ -1,8 +1,41 @@ -- apply changes +-- drop column migtest_e_basic.old_boolean; +IF (OBJECT_ID('uq_migtest_e_basic_old_boolean', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_old_boolean; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_old_boolean') drop index uq_migtest_e_basic_old_boolean ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'old_boolean'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_old_boolean', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_old_boolean; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_old_boolean') drop index ix_migtest_e_basic_old_boolean ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_old_boolean', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_old_boolean; alter table migtest_e_basic drop column old_boolean; +-- drop column migtest_e_basic.old_boolean2; +IF (OBJECT_ID('uq_migtest_e_basic_old_boolean2', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_old_boolean2; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_old_boolean2') drop index uq_migtest_e_basic_old_boolean2 ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'old_boolean2'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_old_boolean2', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_old_boolean2; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_old_boolean2') drop index ix_migtest_e_basic_old_boolean2 ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_old_boolean2', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_old_boolean2; alter table migtest_e_basic drop column old_boolean2; +-- drop column migtest_e_basic.eref_id; +IF (OBJECT_ID('uq_migtest_e_basic_eref_id', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_eref_id; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_eref_id') drop index uq_migtest_e_basic_eref_id ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'eref_id'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_eref_id', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_eref_id; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_eref_id') drop index ix_migtest_e_basic_eref_id ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_eref_id', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_eref_id; alter table migtest_e_basic drop column eref_id; IF OBJECT_ID('migtest_e_ref', 'U') IS NOT NULL drop table migtest_e_ref; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver/1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlserver/1.3.sql index ea4a0b4f4..3700d2298 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver/1.3.sql @@ -4,20 +4,34 @@ create table migtest_e_ref ( constraint pk_migtest_e_ref primary key (id) ); -alter table migtest_e_basic drop constraint ck_migtest_e_basic_status; -alter table migtest_e_basic drop constraint df_migtest_e_basic_status; +IF (OBJECT_ID('ck_migtest_e_basic_status', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_status; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'status'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I')); -alter table migtest_e_basic drop constraint uq_migtest_e_basic_description; -alter table migtest_e_basic drop constraint df_migtest_e_basic_some_date; +IF (OBJECT_ID('uq_migtest_e_basic_description', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_description; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_description') drop index uq_migtest_e_basic_description ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'some_date'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; update migtest_e_basic set user_id = 23 where user_id is null; IF OBJECT_ID('fk_migtest_e_basic_user_id', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_user_id; -alter table migtest_e_basic add constraint df_migtest_e_basic_user_id default 23 for user_id; +alter table migtest_e_basic add default 23 for user_id; alter table migtest_e_basic alter column user_id integer not null; alter table migtest_e_basic add old_boolean bit default 0 not null; alter table migtest_e_basic add old_boolean2 bit default 0; alter table migtest_e_basic add eref_id integer; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_history2') and t2.name = 'test_string'; +if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$; create index ix_migtest_e_basic_indextest1 on migtest_e_basic (indextest1); create index ix_migtest_e_basic_indextest5 on migtest_e_basic (indextest5); IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_indextest3') drop index ix_migtest_e_basic_indextest3 ON migtest_e_basic; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlserver/1.4__dropsFor_1.3.sql index b3088fdd1..8d4df2e09 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver/1.4__dropsFor_1.3.sql @@ -1,15 +1,123 @@ -- apply changes +-- drop column migtest_e_basic.new_string_field; +IF (OBJECT_ID('uq_migtest_e_basic_new_string_field', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_string_field; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_string_field') drop index uq_migtest_e_basic_new_string_field ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'new_string_field'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_new_string_field', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_string_field; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_string_field') drop index ix_migtest_e_basic_new_string_field ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_new_string_field', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_string_field; alter table migtest_e_basic drop column new_string_field; +-- drop column migtest_e_basic.new_boolean_field; +IF (OBJECT_ID('uq_migtest_e_basic_new_boolean_field', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_boolean_field; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_boolean_field') drop index uq_migtest_e_basic_new_boolean_field ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'new_boolean_field'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_new_boolean_field', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_boolean_field; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_boolean_field') drop index ix_migtest_e_basic_new_boolean_field ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_new_boolean_field', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_boolean_field; alter table migtest_e_basic drop column new_boolean_field; +-- drop column migtest_e_basic.new_boolean_field2; +IF (OBJECT_ID('uq_migtest_e_basic_new_boolean_field2', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_boolean_field2; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_boolean_field2') drop index uq_migtest_e_basic_new_boolean_field2 ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'new_boolean_field2'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_new_boolean_field2', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_boolean_field2; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_boolean_field2') drop index ix_migtest_e_basic_new_boolean_field2 ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_new_boolean_field2', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_boolean_field2; alter table migtest_e_basic drop column new_boolean_field2; +-- drop column migtest_e_basic.progress; +IF (OBJECT_ID('uq_migtest_e_basic_progress', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_progress; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_progress') drop index uq_migtest_e_basic_progress ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'progress'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_progress', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_progress; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_progress') drop index ix_migtest_e_basic_progress ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_progress', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_progress; alter table migtest_e_basic drop column progress; +-- drop column migtest_e_basic.new_integer; +IF (OBJECT_ID('uq_migtest_e_basic_new_integer', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_integer; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_integer') drop index uq_migtest_e_basic_new_integer ON migtest_e_basic; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_basic') and t2.name = 'new_integer'; +if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_basic_new_integer', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_integer; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_integer') drop index ix_migtest_e_basic_new_integer ON migtest_e_basic; +IF OBJECT_ID('fk_migtest_e_basic_new_integer', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_integer; alter table migtest_e_basic drop column new_integer; +-- drop column migtest_e_history2.test_string2; +IF (OBJECT_ID('uq_migtest_e_history2_test_string2', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_test_string2; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_test_string2') drop index uq_migtest_e_history2_test_string2 ON migtest_e_history2; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_history2') and t2.name = 'test_string2'; +if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_history2_test_string2', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_test_string2; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_test_string2') drop index ix_migtest_e_history2_test_string2 ON migtest_e_history2; +IF OBJECT_ID('fk_migtest_e_history2_test_string2', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_test_string2; +alter table migtest_e_history2 drop column test_string2; + +-- drop column migtest_e_history2.test_string3; +IF (OBJECT_ID('uq_migtest_e_history2_test_string3', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_test_string3; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_test_string3') drop index uq_migtest_e_history2_test_string3 ON migtest_e_history2; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_history2') and t2.name = 'test_string3'; +if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_history2_test_string3', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_test_string3; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_test_string3') drop index ix_migtest_e_history2_test_string3 ON migtest_e_history2; +IF OBJECT_ID('fk_migtest_e_history2_test_string3', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_test_string3; +alter table migtest_e_history2 drop column test_string3; + +-- drop column migtest_e_softdelete.deleted; +IF (OBJECT_ID('uq_migtest_e_softdelete_deleted', 'UQ') IS NOT NULL) alter table migtest_e_softdelete drop constraint uq_migtest_e_softdelete_deleted; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_softdelete','U') AND name = 'uq_migtest_e_softdelete_deleted') drop index uq_migtest_e_softdelete_deleted ON migtest_e_softdelete; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_softdelete') and t2.name = 'deleted'; +if @Tmp is not null EXEC('alter table migtest_e_softdelete drop constraint ' + @Tmp)$$; +IF (OBJECT_ID('ck_migtest_e_softdelete_deleted', 'C') IS NOT NULL) alter table migtest_e_softdelete drop constraint ck_migtest_e_softdelete_deleted; +IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_softdelete','U') AND name = 'ix_migtest_e_softdelete_deleted') drop index ix_migtest_e_softdelete_deleted ON migtest_e_softdelete; +IF OBJECT_ID('fk_migtest_e_softdelete_deleted', 'F') IS NOT NULL alter table migtest_e_softdelete drop constraint fk_migtest_e_softdelete_deleted; +alter table migtest_e_softdelete drop column deleted; + IF OBJECT_ID('migtest_e_user', 'U') IS NOT NULL drop table migtest_e_user; +-- dropping history support for migtest_e_history; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_history') and t2.name = 'sys_periodFrom'; +if @Tmp is not null EXEC('alter table migtest_e_history drop constraint ' + @Tmp)$$; +delimiter $$ +DECLARE @Tmp nvarchar(200);select @Tmp = t1.name from sys.default_constraints t1 + join sys.columns t2 on t1.object_id = t2.default_object_id + where t1.parent_object_id = OBJECT_ID('migtest_e_history') and t2.name = 'sys_periodTo'; +if @Tmp is not null EXEC('alter table migtest_e_history drop constraint ' + @Tmp)$$; alter table migtest_e_history set (system_versioning = off); +alter table migtest_e_history drop period for system_time; alter table migtest_e_history drop column sys_periodFrom; alter table migtest_e_history drop column sys_periodTo; +IF OBJECT_ID('migtest_e_history_history', 'U') IS NOT NULL drop table migtest_e_history_history; +