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 9b43ff5e7..0db74278f 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerDdl.java +++ b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerDdl.java @@ -105,9 +105,9 @@ public class SqlServerDdl extends PlatformDdl { @Override public String alterTableDropUniqueConstraint(String tableName, String uniqueConstraintName) { StringBuilder sb = new StringBuilder(); + sb.append(dropIndex(uniqueConstraintName, tableName)).append(";\n"); sb.append("IF (OBJECT_ID('").append(maxConstraintName(uniqueConstraintName)).append("', 'UQ') IS NOT NULL) "); - sb.append(super.alterTableDropUniqueConstraint(tableName, uniqueConstraintName)).append(";\n"); - sb.append(dropIndex(uniqueConstraintName, tableName)); + sb.append(super.alterTableDropUniqueConstraint(tableName, uniqueConstraintName)); return sb.toString(); } /** diff --git a/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java b/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java index 50bd331b6..2b157b42c 100644 --- a/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java +++ b/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/PlatformDdl_dropUniqueConstraintTest.java @@ -31,8 +31,9 @@ public class PlatformDdl_dropUniqueConstraintTest { sql = oraDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); assertEquals("alter table mytab drop constraint uq_name", sql); sql = sqlServerDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); - assertEquals("IF (OBJECT_ID('uq_name', 'UQ') IS NOT NULL) alter table mytab drop constraint uq_name;\n" - + "IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('mytab','U') AND name = 'uq_name') drop index uq_name ON mytab", + assertEquals( + "IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('mytab','U') AND name = 'uq_name') drop index uq_name ON mytab;\n" + + "IF (OBJECT_ID('uq_name', 'UQ') IS NOT NULL) alter table mytab drop constraint uq_name", sql); sql = mysqlDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); @@ -41,12 +42,12 @@ public class PlatformDdl_dropUniqueConstraintTest { ServerConfig serverConfig = new ServerConfig(); hanaDdl.configure(serverConfig); sql = hanaDdl.alterTableDropUniqueConstraint("mytab", "uq_name"); - assertEquals("delimiter $$\n" + - "do\n" + - "begin\n" + - "declare exit handler for sql_error_code 397 begin end;\n" + - "exec 'alter table mytab drop constraint uq_name';\n" + - "end;\n" + + assertEquals("delimiter $$\n" + + "do\n" + + "begin\n" + + "declare exit handler for sql_error_code 397 begin end;\n" + + "exec 'alter table mytab drop constraint uq_name';\n" + + "end;\n" + "$$", sql); } diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql index 90dbc13af..d4dcc9c00 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.1.sql @@ -55,10 +55,10 @@ alter table migtest_e_basic add progress integer default 0 not null; alter table migtest_e_basic add constraint ck_migtest_e_basic_progress check ( progress in (0,1,2)); alter table migtest_e_basic add new_integer integer default 42 not null; -IF (OBJECT_ID('uq_migtest_e_basic_indextest2', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest2; IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_indextest2') drop index uq_migtest_e_basic_indextest2 ON migtest_e_basic; -IF (OBJECT_ID('uq_migtest_e_basic_indextest6', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest6; +IF (OBJECT_ID('uq_migtest_e_basic_indextest2', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest2; IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_indextest6') drop index uq_migtest_e_basic_indextest6 ON migtest_e_basic; +IF (OBJECT_ID('uq_migtest_e_basic_indextest6', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest6; create unique nonclustered index uq_migtest_e_basic_status_indextest1 on migtest_e_basic(status,indextest1) where indextest1 is not null; create unique nonclustered index uq_migtest_e_basic_name on migtest_e_basic(name) where name is not null; create unique nonclustered index uq_migtest_e_basic_indextest4 on migtest_e_basic(indextest4) where indextest4 is not null; diff --git a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql index 5eefaa4e0..ae274869c 100644 --- a/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql +++ b/src/test/resources/dbmigration/migrationtest/sqlserver17/1.3.sql @@ -25,8 +25,8 @@ IF (OBJECT_ID('ck_migtest_e_basic_status2', 'C') IS NOT NULL) alter table migtes alter table migtest_e_basic add default 'N' for status2; alter table migtest_e_basic alter column status2 nvarchar(1) not null; alter table migtest_e_basic add constraint ck_migtest_e_basic_status2 check ( status2 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; +IF (OBJECT_ID('uq_migtest_e_basic_description', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_description; 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; @@ -36,14 +36,14 @@ alter table migtest_e_basic add old_boolean bit default 0 not null; alter table migtest_e_basic add old_boolean2 bit; alter table migtest_e_basic add eref_id integer; -IF (OBJECT_ID('uq_migtest_e_basic_status_indextest1', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_status_indextest1; IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_status_indextest1') drop index uq_migtest_e_basic_status_indextest1 ON migtest_e_basic; -IF (OBJECT_ID('uq_migtest_e_basic_name', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_name; +IF (OBJECT_ID('uq_migtest_e_basic_status_indextest1', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_status_indextest1; IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_name') drop index uq_migtest_e_basic_name ON migtest_e_basic; -IF (OBJECT_ID('uq_migtest_e_basic_indextest4', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest4; +IF (OBJECT_ID('uq_migtest_e_basic_name', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_name; IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_indextest4') drop index uq_migtest_e_basic_indextest4 ON migtest_e_basic; -IF (OBJECT_ID('uq_migtest_e_basic_indextest5', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest5; +IF (OBJECT_ID('uq_migtest_e_basic_indextest4', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest4; IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_indextest5') drop index uq_migtest_e_basic_indextest5 ON migtest_e_basic; +IF (OBJECT_ID('uq_migtest_e_basic_indextest5', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_indextest5; create unique nonclustered index uq_migtest_e_basic_indextest2 on migtest_e_basic(indextest2) where indextest2 is not null; 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;