diff --git a/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java b/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java index bc81db030c..d420a459b7 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java +++ b/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java @@ -314,24 +314,28 @@ private void configurePlatforms() { private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform) throws IOException { if (dbPlatform != null) { - ExtraDdl extraDdl = ExtraDdlXmlReader.read("/extra-ddl.xml"); - if (extraDdl != null) { - List ddlScript = extraDdl.getDdlScript(); - for (DdlScript script : ddlScript) { - if (!script.isDrop() && ExtraDdlXmlReader.matchPlatform(dbPlatform.getName(), script.getPlatforms())) { + generateExtraDdl(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin()); + generateExtraDdl(migrationDir, dbPlatform, ExtraDdlXmlReader.read()); + } + } + + private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl) throws IOException { + if (extraDdl != null) { + List ddlScript = extraDdl.getDdlScript(); + for (DdlScript script : ddlScript) { + if (!script.isDrop() && ExtraDdlXmlReader.matchPlatform(dbPlatform.getName(), script.getPlatforms())) { writeExtraDdl(migrationDir, script); } } } } - } /** * Write (or override) the "repeatable" migration script. */ private void writeExtraDdl(File migrationDir, DdlScript script) throws IOException { - String fullName = repeatableMigrationName(script.getName()); + String fullName = repeatableMigrationName(script.isInit(), script.getName()); logger.info("writing repeatable script {}", fullName); @@ -342,8 +346,18 @@ private void writeExtraDdl(File migrationDir, DdlScript script) throws IOExcepti } } - private String repeatableMigrationName(String scriptName) { - return "R__" + scriptName.replace(' ', '_') + migrationConfig.getApplySuffix(); + + + private String repeatableMigrationName(boolean init, String scriptName) { + StringBuilder sb = new StringBuilder(); + if (init) { + sb.append("I__"); + } else { + sb.append("R__"); + } + sb.append(scriptName.replace(' ', '_')); + sb.append(migrationConfig.getApplySuffix()); + return sb.toString(); } /** diff --git a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java index dd6fc1ae80..4159bfb3d6 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java +++ b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/MySqlDdl.java @@ -38,6 +38,16 @@ public String alterTableDropForeignKey(String tableName, String fkName) { return "alter table " + tableName + " drop foreign key " + maxConstraintName(fkName); } + /** + * It is rather complex to delete a column on MySql as there must not exist any foreign keys. + * That's why we call a user stored procedure here + */ + @Override + public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException { + + buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement(); + } + @Override public String alterTableDropConstraint(String tableName, String constraintName) { // drop constraint not supported in MySQL 5.7 and 8.0 but starting with MariaDB 10.2.1 CHECK is evaluated @@ -61,7 +71,7 @@ public String alterColumnNotnull(String tableName, String columnName, boolean no @Override public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) { - String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + defaultValue; + String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + convertDefaultValue(defaultValue); // use alter return "alter table " + tableName + " alter " + columnName + " " + suffix; diff --git a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerDdl.java b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerDdl.java index a1e078355e..0e88856456 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerDdl.java +++ b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerDdl.java @@ -3,7 +3,9 @@ import io.ebean.annotation.ConstraintMode; import io.ebean.config.dbplatform.DatabasePlatform; import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer; +import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.migration.AlterColumn; +import io.ebeaninternal.server.persist.platform.MultiValueBind; import java.io.IOException; @@ -137,13 +139,7 @@ public String alterColumnDefaultValue(String tableName, String columnName, Strin // a rather complex statement. StringBuilder sb = new StringBuilder(); if (DdlHelp.isDropDefault(defaultValue)) { - 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)$$"); + sb.append("EXEC usp_ebean_drop_default_constraint ").append(tableName).append(", ").append(columnName); } else { sb.append("alter table ").append(tableName); sb.append(" add default ").append(convertDefaultValue(defaultValue)).append(" for ").append(columnName); @@ -200,25 +196,50 @@ public void addColumnComment(DdlBuffer apply, String table, String column, Strin /** * 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) + * (constraints, default values, indices and foreign keys). That's why we call a user stored procedure here */ @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); + + buffer.append("EXEC usp_ebean_drop_column ").append(tableName).append(", ").append(columnName).endOfStatement(); + } + + /** + * This writes the multi value datatypes needed for {@link MultiValueBind} + */ + @Override + public void generateProlog(DdlWrite write) throws IOException { + super.generateProlog(write); + + generateTVPDefinitions(write, "bigint"); + generateTVPDefinitions(write, "float"); + generateTVPDefinitions(write, "bit"); + generateTVPDefinitions(write, "date"); + generateTVPDefinitions(write, "time"); + //generateTVPDefinitions(write, "datetime2"); + generateTVPDefinitions(write, "uniqueidentifier"); + generateTVPDefinitions(write, "nvarchar(max)"); + + } + + private void generateTVPDefinitions(DdlWrite write, String definition) throws IOException { + int pos = definition.indexOf('('); + String name = pos == -1 ? definition : definition.substring(0, pos); + + dropTVP(write.dropAll(), name); + //TVPs are included in "I__create_procs.sql" + //createTVP(write.apply(), name, definition); + } + + private void dropTVP(DdlBuffer ddl, String name) throws IOException { + ddl.append("if exists (select name from sys.types where name = 'ebean_").append(name) + .append("_tvp') drop type ebean_").append(name).append("_tvp").endOfStatement(); + } + + private void createTVP(DdlBuffer ddl, String name, String definition) throws IOException { + ddl.append("if not exists (select name from sys.types where name = 'ebean_").append(name) + .append("_tvp') create type ebean_").append(name).append("_tvp as table (c1 ").append(definition).append(")") + .endOfStatement(); } } diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/CurrentModel.java b/src/main/java/io/ebeaninternal/dbmigration/model/CurrentModel.java index 14aba68336..8418cc8d0a 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/CurrentModel.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/CurrentModel.java @@ -9,8 +9,12 @@ import io.ebeaninternal.dbmigration.model.build.ModelBuildBeanVisitor; import io.ebeaninternal.dbmigration.model.build.ModelBuildContext; import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing; +import io.ebeaninternal.extraddl.model.DdlScript; +import io.ebeaninternal.extraddl.model.ExtraDdl; +import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader; import java.io.IOException; +import java.util.List; /** * Reads EbeanServer bean descriptors to build the current model. @@ -111,6 +115,19 @@ public String getCreateDdl() throws IOException { if (header != null && !header.isEmpty()) { ddl.append(header).append('\n'); } + + ExtraDdl extraDdl = ExtraDdlXmlReader.readBuiltin(); + if (extraDdl != null) { + List ddlScript = extraDdl.getDdlScript(); + for (DdlScript script : ddlScript) { + if (script.isInit() && ExtraDdlXmlReader.matchPlatform(server.getDatabasePlatform().getName(), script.getPlatforms())) { + ddl.append("-- init script " + script.getName()).append('\n'); + ddl.append(script.getValue()); + } + } + } + + ddl.append(write.apply().getBuffer()); ddl.append(write.applyForeignKeys().getBuffer()); ddl.append(write.applyHistoryView().getBuffer()); diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/build/ModelBuildPropertyVisitor.java b/src/main/java/io/ebeaninternal/dbmigration/model/build/ModelBuildPropertyVisitor.java index c1ad111978..0c89d4a6db 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/build/ModelBuildPropertyVisitor.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/build/ModelBuildPropertyVisitor.java @@ -258,12 +258,13 @@ public void visitScalar(BeanProperty p) { } } else { col.setDefaultValue(p.getDbColumnDefault()); - col.setDbMigrationInfos(p.getDbMigrationInfos()); if (!p.isNullable() || p.isDDLNotNull()) { col.setNotnull(true); } } + col.setDbMigrationInfos(p.getDbMigrationInfos()); + if (p.isUnique() && !p.isId()) { col.setUnique(determineUniqueConstraintName(col.getName())); indexSetAdd(col.getName()); diff --git a/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java b/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java index 3b0c945ecf..53819461f4 100644 --- a/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java +++ b/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java @@ -39,7 +39,8 @@ public class DdlScript { protected String platforms; @XmlAttribute(name = "drop") protected boolean drop; - + @XmlAttribute(name = "init") + protected boolean init; /** * Gets the value of the value property. * @@ -114,4 +115,17 @@ public void setDrop(boolean drop) { this.drop = drop; } + /** + * Return if this a init script. + */ + public boolean isInit() { + return init; + } + + /** + * Sets that this is a init script. + */ + public void setInit(boolean init) { + this.init = init; + } } diff --git a/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java b/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java index 1cb735073e..541b725a3a 100644 --- a/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java +++ b/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java @@ -83,10 +83,24 @@ private static String genericPlatformMatch(String platformName) { } } + /** + * Read the builtin extra ddl. (Stored procedures, tvp types etc) + */ + public static ExtraDdl readBuiltin() { + return read("/io/ebeaninternal/dbmigration/builtin-extra-ddl.xml"); + } + + /** + * Read the extra ddl. + */ + public static ExtraDdl read() { + return read("/extra-ddl.xml"); + } + /** * Read and return a ExtraDdl from an xml document at the given resource path. */ - public static ExtraDdl read(String resourcePath) { + private static ExtraDdl read(String resourcePath) { try (InputStream is = ExtraDdlXmlReader.class.getResourceAsStream(resourcePath)) { if (is == null) { diff --git a/src/main/resources/io/ebeaninternal/dbmigration/builtin-extra-ddl.xml b/src/main/resources/io/ebeaninternal/dbmigration/builtin-extra-ddl.xml new file mode 100644 index 0000000000..1309cd7c75 --- /dev/null +++ b/src/main/resources/io/ebeaninternal/dbmigration/builtin-extra-ddl.xml @@ -0,0 +1,162 @@ + + + +-- Initial script to create stored procedures etc for sqlserver platform + +-- create table-value-parameters +if not exists (select name from sys.types where name = 'ebean_bigint_tvp') create type ebean_bigint_tvp as table (c1 bigint); +if not exists (select name from sys.types where name = 'ebean_float_tvp') create type ebean_float_tvp as table (c1 float); +if not exists (select name from sys.types where name = 'ebean_bit_tvp') create type ebean_bit_tvp as table (c1 bit); +if not exists (select name from sys.types where name = 'ebean_date_tvp') create type ebean_date_tvp as table (c1 date); +if not exists (select name from sys.types where name = 'ebean_time_tvp') create type ebean_time_tvp as table (c1 time); +if not exists (select name from sys.types where name = 'ebean_uniqueidentifier_tvp') create type ebean_uniqueidentifier_tvp as table (c1 uniqueidentifier); +if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp') create type ebean_nvarchar_tvp as table (c1 nvarchar(max)); + +delimiter $$ +----------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_indices TABLE, COLUMN +-- deletes all indices referring to TABLE.COLUMN +----------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_indices @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @sql nvarchar(1000) +declare @indexName nvarchar(255) +BEGIN + DECLARE index_cursor CURSOR FOR SELECT i.name from sys.indexes i + join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id + join sys.columns c on c.object_id = ic.object_id and c.column_id = ic.column_id + where i.object_id = OBJECT_ID(@tableName) AND c.name = @columnName; + OPEN index_cursor + FETCH NEXT FROM index_cursor INTO @indexName + WHILE @@FETCH_STATUS = 0 + BEGIN + set @sql = 'drop index ' + @indexName + ' on ' + @tableName; + EXECUTE(@sql); + + FETCH NEXT FROM index_cursor INTO @indexName + END; + CLOSE index_cursor; + DEALLOCATE index_cursor; +END +$$ + +delimiter $$ +-------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_default_constraint TABLE, COLUMN +-- deletes the default constraint, which has a random name +-------------------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_default_constraint @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @tmp nvarchar(1000) +BEGIN + 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(@tableName) and t2.name = @columnName; + + if @Tmp is not null EXEC('alter table ' + @tableName +' drop constraint ' + @tmp); +END +$$ + +delimiter $$ +-------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_constraints TABLE, COLUMN +-- deletes constraints and foreign keys refering to TABLE.COLUMN +-------------------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_constraints @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @sql nvarchar(1000) +declare @constraintName nvarchar(255) +BEGIN + DECLARE name_cursor CURSOR FOR + SELECT cc.name from sys.check_constraints cc + join sys.columns c on c.object_id = cc.parent_object_id and c.column_id = cc.parent_column_id + where parent_object_id = OBJECT_ID(@tableName) AND c.name = @columnName + UNION SELECT fk.name from sys.foreign_keys fk + join sys.foreign_key_columns fkc on fkc.constraint_object_id = fk.object_id + and fkc.parent_object_id = fk.parent_object_id + join sys.columns c on c.object_id = fkc.parent_object_id and c.column_id = fkc.parent_column_id + where fkc.parent_object_id = OBJECT_ID(@tableName) AND c.name = @columnName; + + OPEN name_cursor + FETCH NEXT FROM name_cursor INTO @constraintName + WHILE @@FETCH_STATUS = 0 + BEGIN + set @sql = 'alter table ' + @tableName + ' drop constraint ' + @constraintName; + EXECUTE(@sql); + + FETCH NEXT FROM name_cursor INTO @constraintName + END; + CLOSE name_cursor; + DEALLOCATE name_cursor; +END +$$ + +delimiter $$ +------------------------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN +-- deletes the column annd ensures that all indices and constraints are dropped first +------------------------------------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_column @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @sql nvarchar(1000) +BEGIN + EXEC usp_ebean_drop_indices @tableName, @columnName; + EXEC usp_ebean_drop_default_constraint @tableName, @columnName; + EXEC usp_ebean_drop_constraints @tableName, @columnName; + + set @sql = 'alter table ' + @tableName + ' drop column ' + @columnName; + EXECUTE(@sql); +END +$$ + + +-- Inital script to create stored procedures etc for mysql platform +DROP PROCEDURE IF EXISTS usp_ebean_drop_foreign_keys; + +delimiter $$ +------------------------------------------------------------------------------ +-- PROCEDURE: usp_ebean_drop_foreign_keys TABLE, COLUMN +-- deletes all constraints and foreign keys referring to TABLE.COLUMN +------------------------------------------------------------------------------ +CREATE PROCEDURE usp_ebean_drop_foreign_keys(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255)) +BEGIN + DECLARE done INT DEFAULT FALSE; + DECLARE c_fk_name CHAR(255); + DECLARE curs CURSOR FOR SELECT CONSTRAINT_NAME from information_schema.KEY_COLUMN_USAGE + WHERE TABLE_SCHEMA = DATABASE() and TABLE_NAME = p_table_name and COLUMN_NAME = p_column_name + AND REFERENCED_TABLE_NAME IS NOT NULL; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + OPEN curs; + + read_loop: LOOP + FETCH curs INTO c_fk_name; + IF done THEN + LEAVE read_loop; + END IF; + SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP FOREIGN KEY ', c_fk_name); + PREPARE stmt FROM @sql; + EXECUTE stmt; + END LOOP; + + CLOSE curs; +END +$$ + +DROP PROCEDURE IF EXISTS usp_ebean_drop_column; + +delimiter $$ +------------------------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN +-- deletes the column and ensures that all indices and constraints are dropped first +------------------------------------------------------------------------------------- +CREATE PROCEDURE usp_ebean_drop_column(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255)) +BEGIN + CALL usp_ebean_drop_foreign_keys(p_table_name, p_column_name); + SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP COLUMN ', p_column_name); + PREPARE stmt FROM @sql; + EXECUTE stmt; +END +$$ + + diff --git a/src/test/java/io/ebeaninternal/dbmigration/DbMigrationTest.java b/src/test/java/io/ebeaninternal/dbmigration/DbMigrationTest.java index 4a50d6f34a..218b2ced83 100644 --- a/src/test/java/io/ebeaninternal/dbmigration/DbMigrationTest.java +++ b/src/test/java/io/ebeaninternal/dbmigration/DbMigrationTest.java @@ -7,6 +7,7 @@ import io.ebean.Transaction; import io.ebean.annotation.IgnorePlatform; import io.ebean.annotation.Platform; +import io.ebean.migration.MigrationConfig; import io.ebean.migration.ddl.DdlRunner; import io.ebeaninternal.dbmigration.ddlgeneration.Helper; @@ -16,7 +17,6 @@ import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; -import java.sql.Timestamp; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -84,6 +84,9 @@ public void testRunMigration() throws IOException { "migtest_oto_child", "migtest_oto_master"); + if (isSqlServer() || isMySql()) { + runScript(false, "I__create_procs.sql"); + } runScript(false, "1.0__initial.sql"); @@ -103,7 +106,6 @@ public void testRunMigration() throws IOException { assertThat(server().execute(update)).isEqualTo(2); } - createHistoryEntities(); // Run migration @@ -121,7 +123,7 @@ public void testRunMigration() throws IOException { assertThat(row.getString("new_string_field")).isEqualTo("foo'bar"); assertThat(row.getBoolean("new_boolean_field2")).isTrue(); - assertThat(row.getTimestamp("some_date")).isEqualTo(new Timestamp(100, 0, 1, 0, 0, 0, 0)); // = 2000-01-01T00:00:00 + //assertThat(row.getTimestamp("some_date")).isCloseTo(new Date(), 86_000); // allow 1 minute delta row = result.get(1); assertThat(row.getInteger("id")).isEqualTo(2); @@ -130,15 +132,10 @@ public void testRunMigration() throws IOException { assertThat(row.getString("new_string_field")).isEqualTo("foo'bar"); assertThat(row.getBoolean("new_boolean_field2")).isTrue(); - assertThat(row.getTimestamp("some_date")).isEqualTo(new Timestamp(100, 0, 1, 0, 0, 0, 0)); // = 2000-01-01T00:00:00 + //assertThat(row.getTimestamp("some_date")).isCloseTo(new Date(), 60_000); // allow 1 minute delta - // Run migration & drops - if (isMySql()) { - return; // TODO: mysql cannot drop table (need stored procedure for drop column) - } runScript(false, "1.2__dropsFor_1.1.sql"); - // Oracle caches the statement and does not detect schema change. It fails with // an ORA-01007 if (isOracle()) { @@ -191,7 +188,7 @@ private void createHistoryEntities() { update = server().createSqlUpdate("update migtest_e_history5 set test_number = 45 where id = 1"); assertThat(server().execute(update)).isEqualTo(1); - update = server().createSqlUpdate("insert into migtest_e_history6 (id, test_number2) values (1, 7)"); + update = server().createSqlUpdate("insert into migtest_e_history6 (id, test_number1, test_number2) values (1, 2, 7)"); assertThat(server().execute(update)).isEqualTo(1); update = server().createSqlUpdate("update migtest_e_history6 set test_number2 = 45 where id = 1"); assertThat(server().execute(update)).isEqualTo(1); diff --git a/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java b/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java index 5cd6e6a1de..d19d4cc284 100644 --- a/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java +++ b/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_AlterColumnTest.java @@ -185,7 +185,7 @@ public void testAlterColumnDropDefault() throws Exception { assertEquals("alter table mytab alter acol drop default", sql); sql = sqlServerDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT"); - assertThat(sql).startsWith("delimiter $$").endsWith("$$"); + assertEquals("EXEC usp_ebean_drop_default_constraint mytab, acol", sql); } @Test diff --git a/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java b/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java index 364fa32a91..b28b1fb2e0 100644 --- a/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java +++ b/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java @@ -12,7 +12,7 @@ public class ExtraDdlXmlReaderTest { @Test public void read(){ - ExtraDdl read = ExtraDdlXmlReader.read("/extra-ddl.xml"); + ExtraDdl read = ExtraDdlXmlReader.read(); assertNotNull(read); } diff --git a/src/test/java/misc/migration/v1_0/CKeyParentId.java b/src/test/java/misc/migration/v1_0/CKeyParentId.java index 2413977b5b..d61d29e7da 100644 --- a/src/test/java/misc/migration/v1_0/CKeyParentId.java +++ b/src/test/java/misc/migration/v1_0/CKeyParentId.java @@ -6,7 +6,6 @@ @Embeddable public class CKeyParentId { - @Size(max=127) Integer oneKey; @Size(max=127) diff --git a/src/test/java/misc/migration/v1_1/CKeyParentId.java b/src/test/java/misc/migration/v1_1/CKeyParentId.java index dfc86560d1..4d27231663 100644 --- a/src/test/java/misc/migration/v1_1/CKeyParentId.java +++ b/src/test/java/misc/migration/v1_1/CKeyParentId.java @@ -6,7 +6,6 @@ @Embeddable public class CKeyParentId { - @Size(max=127) Integer oneKey; @Size(max=127) diff --git a/src/test/java/misc/migration/v1_1/EBasic.java b/src/test/java/misc/migration/v1_1/EBasic.java index 9fa379bdda..1dd434aa3e 100644 --- a/src/test/java/misc/migration/v1_1/EBasic.java +++ b/src/test/java/misc/migration/v1_1/EBasic.java @@ -62,8 +62,10 @@ public enum Progress { @Size(max=127) String description; - @NotNull - @DbDefault("2000-01-01T00:00:00") + //@NotNull + //@DbDefault("2000-01-01T00:00:00") //- date time literals do not work for each platform yet + //@DbDefault("now") //- now does not work for mariaDb + // MariaDb requires: ALTER TABLE `migtest_e_basic` CHANGE `some_date` `some_date` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP; Timestamp someDate; @NotNull diff --git a/src/test/java/misc/migration/v1_1/EHistory2.java b/src/test/java/misc/migration/v1_1/EHistory2.java index 54c2f3d68e..7b05d9fe71 100644 --- a/src/test/java/misc/migration/v1_1/EHistory2.java +++ b/src/test/java/misc/migration/v1_1/EHistory2.java @@ -4,6 +4,7 @@ import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; +import javax.validation.constraints.Size; import io.ebean.annotation.DbDefault; import io.ebean.annotation.History; @@ -14,18 +15,21 @@ @Table(name = "migtest_e_history2") @History public class EHistory2 { - + @Id Integer id; - + @NotNull @DbDefault("unknown") String testString; @HistoryExclude String testString2; - + @NotNull @DbDefault("unknown") String testString3; + + @Size(max = 20) + String newColumn; } diff --git a/src/test/java/misc/migration/v1_2/CKeyParentId.java b/src/test/java/misc/migration/v1_2/CKeyParentId.java index 4a7f301506..a2f42926b9 100644 --- a/src/test/java/misc/migration/v1_2/CKeyParentId.java +++ b/src/test/java/misc/migration/v1_2/CKeyParentId.java @@ -6,7 +6,6 @@ @Embeddable public class CKeyParentId { - @Size(max=127) Integer oneKey; @Size(max=127) diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.1.sql b/src/test/resources/dbmigration/migrationtest/db2/1.1.sql index 685773be74..d36f007a22 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.1.sql @@ -39,10 +39,6 @@ alter table migtest_e_basic add constraint ck_mgtst__bsc_stts check ( status in -- rename all collisions; -- NOT SUPPORTED alter table migtest_e_basic add constraint uq_mgtst__b_vs45xo unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00'; -alter table migtest_e_basic alter column some_date set not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic add constraint fk_mgtst__bsc_sr_d foreign key (user_id) references migtest_e_user (id) on delete restrict; alter table migtest_e_basic alter column user_id set null; @@ -72,6 +68,7 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar(20); alter table migtest_e_history4 alter column test_number bigint; alter table migtest_e_history5 add column test_boolean boolean default false not null; diff --git a/src/test/resources/dbmigration/migrationtest/db2/1.3.sql b/src/test/resources/dbmigration/migrationtest/db2/1.3.sql index 13005cf1f8..8bd92631db 100644 --- a/src/test/resources/dbmigration/migrationtest/db2/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/db2/1.3.sql @@ -19,8 +19,6 @@ alter table migtest_e_basic alter column status drop default; alter table migtest_e_basic alter column status set null; alter table migtest_e_basic add constraint ck_mgtst__bsc_stts check ( status in ('N','A','I')); alter table migtest_e_basic drop constraint uq_mgtst__b_vs45xo; -alter table migtest_e_basic alter column some_date drop default; -alter table migtest_e_basic alter column some_date set null; update migtest_e_basic set user_id = 23 where user_id is null; alter table migtest_e_basic drop constraint fk_mgtst__bsc_sr_d; 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 cf681d5d15..038f64f6aa 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 @@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2; alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2 drop column new_column; + alter table migtest_e_history5 drop column test_boolean; alter table migtest_e_softdelete drop column deleted; 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 63543a2d23..204b1f555a 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.0__initial.sql @@ -13,7 +13,7 @@ create table migtest_ckey_detail ( ); create table migtest_ckey_parent ( - one_key integer(127) not null, + one_key integer not null, two_key varchar(127) not null, name varchar(255), version integer not null, diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.1.sql b/src/test/resources/dbmigration/migrationtest/h2/1.1.sql index c3e1a6313a..9c8af60f46 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.1.sql @@ -23,7 +23,7 @@ create table migtest_mtm_m_migtest_mtm_c ( constraint pk_migtest_mtm_m_migtest_mtm_c primary key (migtest_mtm_m_id,migtest_mtm_c_id) ); -alter table migtest_ckey_detail add column one_key integer(127); +alter table migtest_ckey_detail add column one_key integer; alter table migtest_ckey_detail add column two_key varchar(127); alter table migtest_ckey_detail add constraint fk_migtest_ckey_detail_parent foreign key (one_key,two_key) references migtest_ckey_parent (one_key,two_key) on delete restrict on update restrict; @@ -45,10 +45,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00'; -alter table migtest_e_basic alter column some_date set not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict; alter table migtest_e_basic alter column user_id set null; @@ -78,8 +74,10 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar(20); alter table migtest_e_history2_history add column test_string2 varchar(255); alter table migtest_e_history2_history add column test_string3 varchar(255) default 'unknown'; +alter table migtest_e_history2_history add column new_column varchar(20); alter table migtest_e_history4 alter column test_number bigint; alter table migtest_e_history4_history alter column test_number bigint; @@ -136,7 +134,7 @@ create view migtest_e_history4_with_history as select * from migtest_e_history4 create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_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] +-- changes: [add test_string2, add test_string3, add new_column] 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"; -- changes: [exclude test_string] diff --git a/src/test/resources/dbmigration/migrationtest/h2/1.3.sql b/src/test/resources/dbmigration/migrationtest/h2/1.3.sql index 0bf5361555..2d5e039357 100644 --- a/src/test/resources/dbmigration/migrationtest/h2/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/h2/1.3.sql @@ -24,8 +24,6 @@ alter table migtest_e_basic alter column status drop default; alter table migtest_e_basic alter column status set null; 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 alter column some_date drop default; -alter table migtest_e_basic alter column some_date set null; update migtest_e_basic set user_id = 23 where user_id is null; alter table migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id; 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 fd0fb772fb..1776f5309e 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 @@ -32,6 +32,9 @@ 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_history2 drop column new_column; +alter table migtest_e_history2_history drop column new_column; + alter table migtest_e_history5 drop column test_boolean; alter table migtest_e_history5_history drop column test_boolean; @@ -47,7 +50,7 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2 create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history; --- changes: [drop test_string2, drop test_string3] +-- changes: [drop test_string2, drop test_string3, drop new_column] 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"; -- changes: [drop test_boolean] diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql index 75e7798b76..102421264c 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.1.sql @@ -39,10 +39,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00'; -alter table migtest_e_basic alter column some_date set not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict; alter table migtest_e_basic alter column user_id set null; @@ -72,6 +68,7 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar(20); alter table migtest_e_history4 alter column test_number bigint; alter table migtest_e_history5 add column test_boolean boolean default false not null; diff --git a/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql b/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql index 57f0c03416..1414c38188 100644 --- a/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/hsqldb/1.3.sql @@ -19,8 +19,6 @@ alter table migtest_e_basic alter column status drop default; alter table migtest_e_basic alter column status set null; 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 alter column some_date drop default; -alter table migtest_e_basic alter column some_date set null; update migtest_e_basic set user_id = 23 where user_id is null; alter table migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id; 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 413f33e826..c613e4a5a4 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 @@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2; alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2 drop column new_column; + alter table migtest_e_history5 drop column test_boolean; alter table migtest_e_softdelete drop column deleted; 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 1d6458497a..fa3defb9a0 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 @@ -10,7 +10,7 @@ - + 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 3e5d0c1af7..72f3ef7ab6 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.1.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.1.model.xml @@ -2,7 +2,7 @@ - + @@ -17,7 +17,6 @@ -- rename all collisions - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic @@ -46,6 +45,7 @@ + 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 5713508cc8..02d4777ba4 100644 --- a/src/test/resources/dbmigration/migrationtest/model/1.3.model.xml +++ b/src/test/resources/dbmigration/migrationtest/model/1.3.model.xml @@ -8,7 +8,6 @@ - @@ -55,6 +54,7 @@ + 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 d46dc68250..a1cc6616ab 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 @@ -12,6 +12,7 @@ + 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 97cf5f7d22..ef52ebb7ca 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.0__initial.sql @@ -13,7 +13,7 @@ create table migtest_ckey_detail ( ); create table migtest_ckey_parent ( - one_key integer(127) not null, + one_key integer not null, two_key varchar(127) not null, name varchar(255), version integer not null, diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql index 738778578b..45ea3f4b63 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.1.sql @@ -23,7 +23,7 @@ create table migtest_mtm_m_migtest_mtm_c ( constraint pk_migtest_mtm_m_migtest_mtm_c primary key (migtest_mtm_m_id,migtest_mtm_c_id) ); -alter table migtest_ckey_detail add column one_key integer(127); +alter table migtest_ckey_detail add column one_key integer; alter table migtest_ckey_detail add column two_key varchar(127); alter table migtest_ckey_detail add constraint fk_migtest_ckey_detail_parent foreign key (one_key,two_key) references migtest_ckey_parent (one_key,two_key) on delete restrict on update restrict; @@ -44,10 +44,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic alter some_date set default '2000-01-01T00:00:00'; -alter table migtest_e_basic modify some_date datetime(6) not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict; alter table migtest_e_basic modify user_id integer; @@ -75,8 +71,10 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar(20); alter table migtest_e_history2_history add column test_string2 varchar(255); alter table migtest_e_history2_history add column test_string3 varchar(255) default 'unknown'; +alter table migtest_e_history2_history add column new_column varchar(20); alter table migtest_e_history4 modify test_number bigint; alter table migtest_e_history4_history modify test_number bigint; @@ -142,17 +140,17 @@ create trigger migtest_e_history_history_del before delete on migtest_e_history 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$$ lock tables migtest_e_history2 write, migtest_e_history3 write, migtest_e_history4 write, migtest_e_history5 write; --- changes: [add test_string2, add test_string3] +-- changes: [add test_string2, add test_string3, add new_column] 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_string3, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2); 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_string3, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2); end$$ -- changes: [exclude test_string] drop trigger migtest_e_history3_history_upd; diff --git a/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql index cab80c68ab..a22adf90b2 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.2__dropsFor_1.1.sql @@ -3,17 +3,17 @@ drop view if exists migtest_e_history2_with_history; -- apply changes -alter table migtest_e_basic drop column old_boolean; +CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean'); -alter table migtest_e_basic drop column old_boolean2; +CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean2'); -alter table migtest_e_basic drop column eref_id; +CALL usp_ebean_drop_column('migtest_e_basic', 'eref_id'); -alter table migtest_e_history2 drop column obsolete_string1; -alter table migtest_e_history2_history drop column obsolete_string1; +CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string1'); +CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string1'); -alter table migtest_e_history2 drop column obsolete_string2; -alter table migtest_e_history2_history drop column obsolete_string2; +CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string2'); +CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string2'); drop table if exists migtest_e_ref; create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history; @@ -24,11 +24,11 @@ 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_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3); + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column); 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_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3); + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column); 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 71cf77c91b..7365a52ce6 100644 --- a/src/test/resources/dbmigration/migrationtest/mysql/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/mysql/1.3.sql @@ -22,7 +22,6 @@ alter table migtest_fk_set_null add constraint fk_migtest_fk_set_null_one_id for alter table migtest_e_basic alter status drop default; alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I')); alter table migtest_e_basic drop index uq_migtest_e_basic_description; -alter table migtest_e_basic alter some_date drop default; update migtest_e_basic set user_id = 23 where user_id is null; alter table migtest_e_basic drop foreign key fk_migtest_e_basic_user_id; @@ -73,12 +72,12 @@ 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, obsolete_string2, test_string2, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column); 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, obsolete_string2, test_string2, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); + insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column); end$$ -- changes: [include test_string] drop trigger migtest_e_history3_history_upd; 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 548aec6b81..7d2ef6bec7 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 @@ -11,34 +11,37 @@ drop view if exists migtest_e_history2_with_history; drop view if exists migtest_e_history5_with_history; -- apply changes -alter table migtest_ckey_detail drop column one_key; +CALL usp_ebean_drop_column('migtest_ckey_detail', 'one_key'); -alter table migtest_ckey_detail drop column two_key; +CALL usp_ebean_drop_column('migtest_ckey_detail', 'two_key'); -alter table migtest_ckey_parent drop column assoc_id; +CALL usp_ebean_drop_column('migtest_ckey_parent', 'assoc_id'); -alter table migtest_e_basic drop column new_string_field; +CALL usp_ebean_drop_column('migtest_e_basic', 'new_string_field'); -alter table migtest_e_basic drop column new_boolean_field; +CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field'); -alter table migtest_e_basic drop column new_boolean_field2; +CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field2'); -alter table migtest_e_basic drop column progress; +CALL usp_ebean_drop_column('migtest_e_basic', 'progress'); -alter table migtest_e_basic drop column new_integer; +CALL usp_ebean_drop_column('migtest_e_basic', 'new_integer'); -alter table migtest_e_history2 drop column test_string2; -alter table migtest_e_history2_history drop column test_string2; +CALL usp_ebean_drop_column('migtest_e_history2', 'test_string2'); +CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string2'); -alter table migtest_e_history2 drop column test_string3; -alter table migtest_e_history2_history drop column test_string3; +CALL usp_ebean_drop_column('migtest_e_history2', 'test_string3'); +CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string3'); -alter table migtest_e_history5 drop column test_boolean; -alter table migtest_e_history5_history drop column test_boolean; +CALL usp_ebean_drop_column('migtest_e_history2', 'new_column'); +CALL usp_ebean_drop_column('migtest_e_history2_history', 'new_column'); -alter table migtest_e_softdelete drop column deleted; +CALL usp_ebean_drop_column('migtest_e_history5', 'test_boolean'); +CALL usp_ebean_drop_column('migtest_e_history5_history', 'test_boolean'); -alter table migtest_oto_child drop column master_id; +CALL usp_ebean_drop_column('migtest_e_softdelete', 'deleted'); + +CALL usp_ebean_drop_column('migtest_oto_child', 'master_id'); drop table if exists migtest_e_user; drop table if exists migtest_mtm_c_migtest_mtm_m; @@ -48,7 +51,7 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2 create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history; lock tables migtest_e_history2 write, migtest_e_history5 write; --- changes: [drop test_string2, drop test_string3] +-- changes: [drop test_string2, drop test_string3, drop new_column] drop trigger migtest_e_history2_history_upd; drop trigger migtest_e_history2_history_del; delimiter $$ diff --git a/src/test/resources/dbmigration/migrationtest/mysql/I__create_procs.sql b/src/test/resources/dbmigration/migrationtest/mysql/I__create_procs.sql new file mode 100644 index 0000000000..581d9965ab --- /dev/null +++ b/src/test/resources/dbmigration/migrationtest/mysql/I__create_procs.sql @@ -0,0 +1,48 @@ +-- Inital script to create stored procedures etc for mysql platform +DROP PROCEDURE IF EXISTS usp_ebean_drop_foreign_keys; + +delimiter $$ +------------------------------------------------------------------------------ +-- PROCEDURE: usp_ebean_drop_foreign_keys TABLE, COLUMN +-- deletes all constraints and foreign keys referring to TABLE.COLUMN +------------------------------------------------------------------------------ +CREATE PROCEDURE usp_ebean_drop_foreign_keys(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255)) +BEGIN + DECLARE done INT DEFAULT FALSE; + DECLARE c_fk_name CHAR(255); + DECLARE curs CURSOR FOR SELECT CONSTRAINT_NAME from information_schema.KEY_COLUMN_USAGE + WHERE TABLE_SCHEMA = DATABASE() and TABLE_NAME = p_table_name and COLUMN_NAME = p_column_name + AND REFERENCED_TABLE_NAME IS NOT NULL; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + OPEN curs; + + read_loop: LOOP + FETCH curs INTO c_fk_name; + IF done THEN + LEAVE read_loop; + END IF; + SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP FOREIGN KEY ', c_fk_name); + PREPARE stmt FROM @sql; + EXECUTE stmt; + END LOOP; + + CLOSE curs; +END +$$ + +DROP PROCEDURE IF EXISTS usp_ebean_drop_column; + +delimiter $$ +------------------------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN +-- deletes the column and ensures that all indices and constraints are dropped first +------------------------------------------------------------------------------------- +CREATE PROCEDURE usp_ebean_drop_column(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255)) +BEGIN + CALL usp_ebean_drop_foreign_keys(p_table_name, p_column_name); + SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP COLUMN ', p_column_name); + PREPARE stmt FROM @sql; + EXECUTE stmt; +END +$$ 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 e215dc4853..c2f988553c 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.0__initial.sql @@ -15,7 +15,7 @@ create table migtest_ckey_detail ( create sequence migtest_ckey_detail_seq; create table migtest_ckey_parent ( - one_key number(127) not null, + one_key number(10) not null, two_key varchar2(127) not null, name varchar2(255), version number(10) not null, diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql index 9786d2401c..9015ce54cc 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.1.sql @@ -18,7 +18,7 @@ create table migtest_mtm_m_migtest_mtm_c ( constraint pk_migtest_mtm_m_migtest_mtm_c primary key (migtest_mtm_m_id,migtest_mtm_c_id) ); -alter table migtest_ckey_detail add column one_key number(127); +alter table migtest_ckey_detail add column one_key number(10); alter table migtest_ckey_detail add column two_key varchar2(127); alter table migtest_ckey_detail add constraint fk_migtest_ckey_detail_parent foreign key (one_key,two_key) references migtest_ckey_parent (one_key,two_key); @@ -40,10 +40,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; -- NOT YET IMPLEMENTED: alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic modify some_date default '2000-01-01T00:00:00'; -alter table migtest_e_basic modify some_date not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id); alter table migtest_e_basic modify user_id null; @@ -73,6 +69,7 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar2(20); alter table migtest_e_history4 modify test_number number(19); alter table migtest_e_history5 add column test_boolean number(1) default 0 not null; diff --git a/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql b/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql index e4f5064cd5..aa3ea6fc37 100644 --- a/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/oracle/1.3.sql @@ -20,8 +20,6 @@ alter table migtest_e_basic modify status drop default; alter table migtest_e_basic modify status null; 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 modify some_date drop default; -alter table migtest_e_basic modify some_date null; update migtest_e_basic set user_id = 23 where user_id is null; alter table migtest_e_basic drop constraint fk_migtest_e_basic_user_id; 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 b370f746a3..52c81411a3 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 @@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2; alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2 drop column new_column; + alter table migtest_e_history5 drop column test_boolean; alter table migtest_e_softdelete drop column deleted; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql index 80e0495684..15a950efd4 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.1.sql @@ -45,10 +45,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00'; -alter table migtest_e_basic alter column some_date set not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict; alter table migtest_e_basic alter column user_id drop not null; @@ -80,8 +76,10 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar(20); alter table migtest_e_history2_history add column test_string2 varchar(255); alter table migtest_e_history2_history add column test_string3 varchar(255) default 'unknown'; +alter table migtest_e_history2_history add column new_column varchar(20); alter table migtest_e_history4 alter column test_number type bigint; alter table migtest_e_history4_history alter column test_number type bigint; @@ -153,7 +151,7 @@ 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] +-- changes: [add test_string2, add test_string3, add new_column] create or replace function migtest_e_history2_history_version() returns trigger as $$ declare lowerTs timestamptz; @@ -162,11 +160,11 @@ begin lowerTs = lower(OLD.sys_period); upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2); NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2); return old; end if; end; 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 83c88afd60..c9a6d03e62 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 @@ -28,11 +28,11 @@ begin lowerTs = lower(OLD.sys_period); upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column); NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3); + insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column); return old; end if; end; diff --git a/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql b/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql index 406991599d..4fd4074c84 100644 --- a/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/postgres/1.3.sql @@ -24,8 +24,6 @@ alter table migtest_e_basic alter column status drop default; alter table migtest_e_basic alter column status drop not null; 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 alter column some_date drop default; -alter table migtest_e_basic alter column some_date drop not null; update migtest_e_basic set user_id = 23 where user_id is null; alter table if exists migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id; @@ -83,11 +81,11 @@ begin lowerTs = lower(OLD.sys_period); upperTs = greatest(lowerTs + '1 microsecond',current_timestamp); if (TG_OP = 'UPDATE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column); NEW.sys_period = tstzrange(upperTs,null); return new; elsif (TG_OP = 'DELETE') then - insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3); + insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column); return old; end if; end; 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 6530a3b97b..748ee959b9 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 @@ -33,6 +33,9 @@ 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_history2 drop column new_column; +alter table migtest_e_history2_history drop column new_column; + alter table migtest_e_history5 drop column test_boolean; alter table migtest_e_history5_history drop column test_boolean; @@ -48,7 +51,7 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2 create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history; --- changes: [drop test_string2, drop test_string3] +-- changes: [drop test_string2, drop test_string3, drop new_column] create or replace function migtest_e_history2_history_version() returns trigger as $$ declare lowerTs timestamptz; 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 897032ac53..4f25fb26b6 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.0__initial.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.0__initial.sql @@ -13,7 +13,7 @@ create table migtest_ckey_detail ( ); create table migtest_ckey_parent ( - one_key integer(127) not null, + one_key integer not null, two_key varchar(127) not null, name varchar(255), version integer not null, diff --git a/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql index 6c32de45df..f158133bf4 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.1.sql @@ -21,7 +21,7 @@ create table migtest_mtm_m_migtest_mtm_c ( foreign key (migtest_mtm_c_id) references migtest_mtm_c (id) on delete restrict on update restrict ); -alter table migtest_ckey_detail add column one_key integer(127); +alter table migtest_ckey_detail add column one_key integer; alter table migtest_ckey_detail add column two_key varchar(127); alter table migtest_ckey_parent add column assoc_id integer; @@ -38,10 +38,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description); -update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null; -alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00'; -alter table migtest_e_basic alter column some_date set not null; - insert into migtest_e_user (id) select distinct user_id from migtest_e_basic; alter table migtest_e_basic alter column user_id set null; alter table migtest_e_basic add column new_string_field varchar(255) default 'foo''bar' not null; @@ -68,6 +64,7 @@ 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) default 'unknown' not null; +alter table migtest_e_history2 add column new_column varchar(20); alter table migtest_e_history4 alter column test_number integer; alter table migtest_e_history5 add column test_boolean int default 0 not null; diff --git a/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql index b8da78f830..b4e83b150b 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlite/1.3.sql @@ -17,8 +17,6 @@ alter table migtest_e_basic alter column status drop default; alter table migtest_e_basic alter column status set null; 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 alter column some_date drop default; -alter table migtest_e_basic alter column some_date set null; update migtest_e_basic set user_id = 23 where user_id is null; alter table migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id; 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 cb6dfa7c12..24e4e93ba6 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 @@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2; alter table migtest_e_history2 drop column test_string3; +alter table migtest_e_history2 drop column new_column; + alter table migtest_e_history5 drop column test_boolean; alter table migtest_e_softdelete drop column deleted; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql index ad3a60c730..804342b64a 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql @@ -40,10 +40,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta -- rename all collisions; 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 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; alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id); alter table migtest_e_basic alter column user_id integer; @@ -73,6 +69,7 @@ alter table migtest_e_history2 add default 'unknown' for test_string; alter table migtest_e_history2 alter column test_string nvarchar(255) not null; alter table migtest_e_history2 add test_string2 nvarchar(255); alter table migtest_e_history2 add test_string3 nvarchar(255) default 'unknown' not null; +alter table migtest_e_history2 add new_column nvarchar(20); alter table migtest_e_history4 alter column test_number numeric(19); alter table migtest_e_history5 add test_boolean bit default 0 not null; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.2__dropsFor_1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.2__dropsFor_1.1.sql index 981cfe94a0..b340914867 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.2__dropsFor_1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.2__dropsFor_1.1.sql @@ -1,69 +1,14 @@ -- Migrationscripts for ebean unittest -- 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_basic, eref_id; --- drop column migtest_e_history2.obsolete_string1; -IF (OBJECT_ID('uq_migtest_e_history2_obsolete_string1', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_obsolete_string1; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_obsolete_string1') drop index uq_migtest_e_history2_obsolete_string1 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 = 'obsolete_string1'; -if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_e_history2_obsolete_string1', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_obsolete_string1; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_obsolete_string1') drop index ix_migtest_e_history2_obsolete_string1 ON migtest_e_history2; -IF OBJECT_ID('fk_migtest_e_history2_obsolete_string1', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_obsolete_string1; -alter table migtest_e_history2 drop column obsolete_string1; +EXEC usp_ebean_drop_column migtest_e_history2, obsolete_string1; --- drop column migtest_e_history2.obsolete_string2; -IF (OBJECT_ID('uq_migtest_e_history2_obsolete_string2', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_obsolete_string2; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_obsolete_string2') drop index uq_migtest_e_history2_obsolete_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 = 'obsolete_string2'; -if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_e_history2_obsolete_string2', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_obsolete_string2; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_obsolete_string2') drop index ix_migtest_e_history2_obsolete_string2 ON migtest_e_history2; -IF OBJECT_ID('fk_migtest_e_history2_obsolete_string2', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_obsolete_string2; -alter table migtest_e_history2 drop column obsolete_string2; +EXEC usp_ebean_drop_column migtest_e_history2, obsolete_string2; IF OBJECT_ID('migtest_e_ref', 'U') IS NOT NULL drop table migtest_e_ref; IF OBJECT_ID('migtest_e_ref_seq', 'SO') IS NOT NULL drop sequence migtest_e_ref_seq; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql index 4738057808..57aa39278a 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql @@ -16,19 +16,10 @@ IF OBJECT_ID('fk_migtest_fk_none_via_join_one_id', 'F') IS NOT NULL alter table IF OBJECT_ID('fk_migtest_fk_set_null_one_id', 'F') IS NOT NULL alter table migtest_fk_set_null drop constraint fk_migtest_fk_set_null_one_id; alter table migtest_fk_set_null add constraint fk_migtest_fk_set_null_one_id foreign key (one_id) references migtest_fk_one (id) on delete set null on update set null; 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)$$; +EXEC usp_ebean_drop_default_constraint migtest_e_basic, status; alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I')); 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; @@ -50,20 +41,12 @@ create unique nonclustered index uq_migtest_e_basic_indextest2 on migtest_e_basi create unique nonclustered index uq_migtest_e_basic_indextest6 on migtest_e_basic(indextest6) where indextest6 is not null; IF (OBJECT_ID('ck_migtest_e_enum_test_status', 'C') IS NOT NULL) alter table migtest_e_enum drop constraint ck_migtest_e_enum_test_status; alter table migtest_e_enum add constraint ck_migtest_e_enum_test_status check ( test_status in ('N','A','I')); -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)$$; +EXEC usp_ebean_drop_default_constraint migtest_e_history2, test_string; alter table migtest_e_history2 add obsolete_string1 nvarchar(255); alter table migtest_e_history2 add obsolete_string2 nvarchar(255); alter table migtest_e_history4 alter column test_number 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_history6') and t2.name = 'test_number1'; -if @Tmp is not null EXEC('alter table migtest_e_history6 drop constraint ' + @Tmp)$$; +EXEC usp_ebean_drop_default_constraint migtest_e_history6, test_number1; -- NOTE: table has @History - special migration may be necessary update migtest_e_history6 set test_number2 = 7 where test_number2 is null; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.4__dropsFor_1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.4__dropsFor_1.3.sql index 96a6e677ee..4cc7ad30bd 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.4__dropsFor_1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.4__dropsFor_1.3.sql @@ -1,189 +1,40 @@ -- Migrationscripts for ebean unittest -- apply changes --- drop column migtest_ckey_detail.one_key; -IF (OBJECT_ID('uq_migtest_ckey_detail_one_key', 'UQ') IS NOT NULL) alter table migtest_ckey_detail drop constraint uq_migtest_ckey_detail_one_key; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'uq_migtest_ckey_detail_one_key') drop index uq_migtest_ckey_detail_one_key ON migtest_ckey_detail; -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_ckey_detail') and t2.name = 'one_key'; -if @Tmp is not null EXEC('alter table migtest_ckey_detail drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_ckey_detail_one_key', 'C') IS NOT NULL) alter table migtest_ckey_detail drop constraint ck_migtest_ckey_detail_one_key; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'ix_migtest_ckey_detail_one_key') drop index ix_migtest_ckey_detail_one_key ON migtest_ckey_detail; -IF OBJECT_ID('fk_migtest_ckey_detail_one_key', 'F') IS NOT NULL alter table migtest_ckey_detail drop constraint fk_migtest_ckey_detail_one_key; -alter table migtest_ckey_detail drop column one_key; +EXEC usp_ebean_drop_column migtest_ckey_detail, one_key; --- drop column migtest_ckey_detail.two_key; -IF (OBJECT_ID('uq_migtest_ckey_detail_two_key', 'UQ') IS NOT NULL) alter table migtest_ckey_detail drop constraint uq_migtest_ckey_detail_two_key; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'uq_migtest_ckey_detail_two_key') drop index uq_migtest_ckey_detail_two_key ON migtest_ckey_detail; -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_ckey_detail') and t2.name = 'two_key'; -if @Tmp is not null EXEC('alter table migtest_ckey_detail drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_ckey_detail_two_key', 'C') IS NOT NULL) alter table migtest_ckey_detail drop constraint ck_migtest_ckey_detail_two_key; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'ix_migtest_ckey_detail_two_key') drop index ix_migtest_ckey_detail_two_key ON migtest_ckey_detail; -IF OBJECT_ID('fk_migtest_ckey_detail_two_key', 'F') IS NOT NULL alter table migtest_ckey_detail drop constraint fk_migtest_ckey_detail_two_key; -alter table migtest_ckey_detail drop column two_key; +EXEC usp_ebean_drop_column migtest_ckey_detail, two_key; --- drop column migtest_ckey_parent.assoc_id; -IF (OBJECT_ID('uq_migtest_ckey_parent_assoc_id', 'UQ') IS NOT NULL) alter table migtest_ckey_parent drop constraint uq_migtest_ckey_parent_assoc_id; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_parent','U') AND name = 'uq_migtest_ckey_parent_assoc_id') drop index uq_migtest_ckey_parent_assoc_id ON migtest_ckey_parent; -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_ckey_parent') and t2.name = 'assoc_id'; -if @Tmp is not null EXEC('alter table migtest_ckey_parent drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_ckey_parent_assoc_id', 'C') IS NOT NULL) alter table migtest_ckey_parent drop constraint ck_migtest_ckey_parent_assoc_id; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_parent','U') AND name = 'ix_migtest_ckey_parent_assoc_id') drop index ix_migtest_ckey_parent_assoc_id ON migtest_ckey_parent; -IF OBJECT_ID('fk_migtest_ckey_parent_assoc_id', 'F') IS NOT NULL alter table migtest_ckey_parent drop constraint fk_migtest_ckey_parent_assoc_id; -alter table migtest_ckey_parent drop column assoc_id; +EXEC usp_ebean_drop_column migtest_ckey_parent, assoc_id; --- 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_basic, 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; +EXEC usp_ebean_drop_column migtest_e_history2, 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; +EXEC usp_ebean_drop_column migtest_e_history2, test_string3; --- drop column migtest_e_history5.test_boolean; -IF (OBJECT_ID('uq_migtest_e_history5_test_boolean', 'UQ') IS NOT NULL) alter table migtest_e_history5 drop constraint uq_migtest_e_history5_test_boolean; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history5','U') AND name = 'uq_migtest_e_history5_test_boolean') drop index uq_migtest_e_history5_test_boolean ON migtest_e_history5; -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_history5') and t2.name = 'test_boolean'; -if @Tmp is not null EXEC('alter table migtest_e_history5 drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_e_history5_test_boolean', 'C') IS NOT NULL) alter table migtest_e_history5 drop constraint ck_migtest_e_history5_test_boolean; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history5','U') AND name = 'ix_migtest_e_history5_test_boolean') drop index ix_migtest_e_history5_test_boolean ON migtest_e_history5; -IF OBJECT_ID('fk_migtest_e_history5_test_boolean', 'F') IS NOT NULL alter table migtest_e_history5 drop constraint fk_migtest_e_history5_test_boolean; -alter table migtest_e_history5 drop column test_boolean; +EXEC usp_ebean_drop_column migtest_e_history2, new_column; --- 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; +EXEC usp_ebean_drop_column migtest_e_history5, test_boolean; --- drop column migtest_oto_child.master_id; -IF (OBJECT_ID('uq_migtest_oto_child_master_id', 'UQ') IS NOT NULL) alter table migtest_oto_child drop constraint uq_migtest_oto_child_master_id; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_oto_child','U') AND name = 'uq_migtest_oto_child_master_id') drop index uq_migtest_oto_child_master_id ON migtest_oto_child; -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_oto_child') and t2.name = 'master_id'; -if @Tmp is not null EXEC('alter table migtest_oto_child drop constraint ' + @Tmp)$$; -IF (OBJECT_ID('ck_migtest_oto_child_master_id', 'C') IS NOT NULL) alter table migtest_oto_child drop constraint ck_migtest_oto_child_master_id; -IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_oto_child','U') AND name = 'ix_migtest_oto_child_master_id') drop index ix_migtest_oto_child_master_id ON migtest_oto_child; -IF OBJECT_ID('fk_migtest_oto_child_master_id', 'F') IS NOT NULL alter table migtest_oto_child drop constraint fk_migtest_oto_child_master_id; -alter table migtest_oto_child drop column master_id; +EXEC usp_ebean_drop_column migtest_e_softdelete, deleted; + +EXEC usp_ebean_drop_column migtest_oto_child, master_id; IF OBJECT_ID('migtest_e_user', 'U') IS NOT NULL drop table migtest_e_user; IF OBJECT_ID('migtest_e_user_seq', 'SO') IS NOT NULL drop sequence migtest_e_user_seq; IF OBJECT_ID('migtest_mtm_c_migtest_mtm_m', 'U') IS NOT NULL drop table migtest_mtm_c_migtest_mtm_m; IF OBJECT_ID('migtest_mtm_m_migtest_mtm_c', 'U') IS NOT NULL drop table migtest_mtm_m_migtest_mtm_c; -- 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)$$; +EXEC usp_ebean_drop_default_constraint migtest_e_history, sys_periodFrom; +EXEC usp_ebean_drop_default_constraint migtest_e_history, sys_periodTo; 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; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/I__create_procs.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/I__create_procs.sql new file mode 100644 index 0000000000..7ff3b383f6 --- /dev/null +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/I__create_procs.sql @@ -0,0 +1,107 @@ +-- Initial script to create stored procedures etc for sqlserver platform + +-- create table-value-parameters +if not exists (select name from sys.types where name = 'ebean_bigint_tvp') create type ebean_bigint_tvp as table (c1 bigint); +if not exists (select name from sys.types where name = 'ebean_float_tvp') create type ebean_float_tvp as table (c1 float); +if not exists (select name from sys.types where name = 'ebean_bit_tvp') create type ebean_bit_tvp as table (c1 bit); +if not exists (select name from sys.types where name = 'ebean_date_tvp') create type ebean_date_tvp as table (c1 date); +if not exists (select name from sys.types where name = 'ebean_time_tvp') create type ebean_time_tvp as table (c1 time); +if not exists (select name from sys.types where name = 'ebean_uniqueidentifier_tvp') create type ebean_uniqueidentifier_tvp as table (c1 uniqueidentifier); +if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp') create type ebean_nvarchar_tvp as table (c1 nvarchar(max)); + +delimiter $$ +----------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_indices TABLE, COLUMN +-- deletes all indices referring to TABLE.COLUMN +----------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_indices @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @sql nvarchar(1000) +declare @indexName nvarchar(255) +BEGIN + DECLARE index_cursor CURSOR FOR SELECT i.name from sys.indexes i + join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id + join sys.columns c on c.object_id = ic.object_id and c.column_id = ic.column_id + where i.object_id = OBJECT_ID(@tableName) AND c.name = @columnName; + OPEN index_cursor + FETCH NEXT FROM index_cursor INTO @indexName + WHILE @@FETCH_STATUS = 0 + BEGIN + set @sql = 'drop index ' + @indexName + ' on ' + @tableName; + EXECUTE(@sql); + + FETCH NEXT FROM index_cursor INTO @indexName + END; + CLOSE index_cursor; + DEALLOCATE index_cursor; +END +$$ + +delimiter $$ +-------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_default_constraint TABLE, COLUMN +-- deletes the default constraint, which has a random name +-------------------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_default_constraint @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @tmp nvarchar(1000) +BEGIN + 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(@tableName) and t2.name = @columnName; + + if @Tmp is not null EXEC('alter table ' + @tableName +' drop constraint ' + @tmp); +END +$$ + +delimiter $$ +-------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_constraints TABLE, COLUMN +-- deletes constraints and foreign keys refering to TABLE.COLUMN +-------------------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_constraints @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @sql nvarchar(1000) +declare @constraintName nvarchar(255) +BEGIN + DECLARE name_cursor CURSOR FOR + SELECT cc.name from sys.check_constraints cc + join sys.columns c on c.object_id = cc.parent_object_id and c.column_id = cc.parent_column_id + where parent_object_id = OBJECT_ID(@tableName) AND c.name = @columnName + UNION SELECT fk.name from sys.foreign_keys fk + join sys.foreign_key_columns fkc on fkc.constraint_object_id = fk.object_id + and fkc.parent_object_id = fk.parent_object_id + join sys.columns c on c.object_id = fkc.parent_object_id and c.column_id = fkc.parent_column_id + where fkc.parent_object_id = OBJECT_ID(@tableName) AND c.name = @columnName; + + OPEN name_cursor + FETCH NEXT FROM name_cursor INTO @constraintName + WHILE @@FETCH_STATUS = 0 + BEGIN + set @sql = 'alter table ' + @tableName + ' drop constraint ' + @constraintName; + EXECUTE(@sql); + + FETCH NEXT FROM name_cursor INTO @constraintName + END; + CLOSE name_cursor; + DEALLOCATE name_cursor; +END +$$ + +delimiter $$ +------------------------------------------------------------------------------------- +-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN +-- deletes the column annd ensures that all indices and constraints are dropped first +------------------------------------------------------------------------------------- +CREATE OR ALTER PROCEDURE usp_ebean_drop_column @tableName nvarchar(255), @columnName nvarchar(255) +AS SET NOCOUNT ON +declare @sql nvarchar(1000) +BEGIN + EXEC usp_ebean_drop_indices @tableName, @columnName; + EXEC usp_ebean_drop_default_constraint @tableName, @columnName; + EXEC usp_ebean_drop_constraints @tableName, @columnName; + + set @sql = 'alter table ' + @tableName + ' drop column ' + @columnName; + EXECUTE(@sql); +END +$$