From 89dcde3506b9491f5b2270b1851759794d7d4b39 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 2 Mar 2021 15:29:55 -0600 Subject: [PATCH] SONAR-14541 Migration 4207 fails if the PK constraint name is unexpected --- .../db/migration/version/v87/DropOrgMembersTable.java | 9 ++++++--- .../version/v87/DropOrgMembersTableTest.java | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTable.java index 5865f1b8109..64e704b60f3 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTable.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTable.java @@ -20,22 +20,25 @@ package org.sonar.server.platform.db.migration.version.v87; import java.sql.SQLException; +import java.util.Arrays; import org.sonar.db.Database; -import org.sonar.server.platform.db.migration.sql.DropConstraintBuilder; import org.sonar.server.platform.db.migration.sql.DropIndexBuilder; +import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator; import org.sonar.server.platform.db.migration.sql.DropTableBuilder; import org.sonar.server.platform.db.migration.step.DdlChange; public class DropOrgMembersTable extends DdlChange { private static final String TABLE_NAME = "organization_members"; + private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator; - public DropOrgMembersTable(Database db) { + public DropOrgMembersTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) { super(db); + this.dropPrimaryKeySqlGenerator = dropPrimaryKeySqlGenerator; } @Override public void execute(Context context) throws SQLException { - context.execute(new DropConstraintBuilder(getDialect()).setTable(TABLE_NAME).setName("pk_organization_members").build()); + context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, Arrays.asList("user_uuid", "organization_uuid"), false)); context.execute(new DropIndexBuilder(getDialect()).setTable(TABLE_NAME).setName("org_members_user_uuid").build()); context.execute(new DropTableBuilder(getDialect(), TABLE_NAME).build()); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTableTest.java index 282d628e489..2da0bcf4621 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTableTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DropOrgMembersTableTest.java @@ -23,18 +23,21 @@ import java.sql.SQLException; import org.junit.Rule; import org.junit.Test; import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder; +import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator; import org.sonar.server.platform.db.migration.step.MigrationStep; public class DropOrgMembersTableTest { @Rule - public CoreDbTester dbTester = CoreDbTester.createForSchema(DropOrgMembersTableTest.class, "schema.sql"); + public CoreDbTester db = CoreDbTester.createForSchema(DropOrgMembersTableTest.class, "schema.sql"); + private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new DbPrimaryKeyConstraintFinder(db.database())); - private final MigrationStep underTest = new DropOrgMembersTable(dbTester.database()); + private final MigrationStep underTest = new DropOrgMembersTable(db.database(), dropPrimaryKeySqlGenerator); @Test public void table_is_dropped() throws SQLException { - dbTester.assertTableExists("organization_members"); + db.assertTableExists("organization_members"); underTest.execute(); - dbTester.assertTableDoesNotExist("organization_members"); + db.assertTableDoesNotExist("organization_members"); } } -- 2.39.5