]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13613 DB migrations on upgrade fail due to orphan foreign keys
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 8 Jul 2020 15:40:03 +0000 (10:40 -0500)
committersonartech <sonartech@sonarsource.com>
Fri, 10 Jul 2020 20:05:50 +0000 (20:05 +0000)
14 files changed:
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/activerules/PopulateActiveRulesRuleUuidColumn.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/rulesmetadata/PopulateRulesMetadataRuleUuidColumn.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/groupsusers/PopulateGroupsUsersUserUuid.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/organizationmembers/PopulateOrganizationMembersUserUuid.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/permtemplatesusers/PopulatePermTemplatesUsersUserUuid.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/qprofileeditusers/PopulateQProfileEditUsersUserUuid.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/rules/activerules/PopulateActiveRulesRuleUuidColumnTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumnTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/rules/rulesmetadata/PopulateRulesMetadataRuleUuidColumnTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/users/fk/groupsusers/PopulateGroupsUsersUserUuidTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/users/fk/organizationmembers/PopulateOrganizationMembersUserUuidTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/users/fk/permtemplatesusers/PopulatePermTemplatesUsersUserUuidTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/users/fk/qprofileeditusers/PopulateQProfileEditUsersUserUuidTest.java

index 492c4b8861dc1e038aae230f555f448a5b2e7756..8f2b3a0656511c6a35cd64a5e0634c9e55dbdbf2 100644 (file)
@@ -45,5 +45,15 @@ public class PopulateActiveRulesRuleUuidColumn extends DataChange {
       update.setLong(2, row.getLong(1));
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select uuid from active_rules where rule_uuid is null");
+    massUpdate.update("delete from active_rules where uuid = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setString(1, row.getString(1));
+      return true;
+    });
   }
 }
index 8e35f83f76629703d58ea58a8f3caf3990c02001..506ec9735ed42e1a69ec86e7187bf8a5f17243a6 100644 (file)
@@ -45,5 +45,15 @@ public class PopulateDeprecatedRuleKeysRuleUuidColumn extends DataChange {
       update.setLong(2, row.getLong(1));
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select uuid from deprecated_rule_keys where rule_uuid is null");
+    massUpdate.update("delete from deprecated_rule_keys where uuid = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setString(1, row.getString(1));
+      return true;
+    });
   }
 }
index 6021a6975b42280ad7aae494bd2cebbf194a9ab8..4a9a26fa4a8d3b7856e5096f7cdf38808a73d942 100644 (file)
@@ -45,5 +45,16 @@ public class PopulateRulesMetadataRuleUuidColumn extends DataChange {
       update.setLong(2, row.getLong(1));
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select rule_id, organization_uuid from rules_metadata where rule_uuid is null");
+    massUpdate.update("delete from rules_metadata where rule_id = ? and organization_uuid = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setLong(1, row.getLong(1));
+      update.setString(2, row.getString(2));
+      return true;
+    });
   }
 }
index 48ef8e8f113eb425f1084c88bbec46fd26f3bc3f..02c1a80abfce82081962c912c55893e4bfc5e0ae 100644 (file)
@@ -49,5 +49,17 @@ public class PopulateGroupsUsersUserUuid extends DataChange {
       update.setLong(3, userId);
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select group_uuid, user_id from groups_users where user_uuid is null");
+    massUpdate.update("delete from groups_users where group_uuid = ? and user_id = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setString(1, row.getString(1));
+      update.setLong(2, row.getLong(2));
+
+      return true;
+    });
   }
 }
index 536693a24de7fe2b002b13e3b7403168bef95d3f..aec75a73ca3c77814b08f3d66636115d207ea8f6 100644 (file)
@@ -50,5 +50,17 @@ public class PopulateOrganizationMembersUserUuid extends DataChange {
       update.setLong(3, userId);
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select organization_uuid, user_id from organization_members where user_uuid is null");
+    massUpdate.update("delete from organization_members where organization_uuid = ? and user_id = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setString(1, row.getString(1));
+      update.setLong(2, row.getLong(2));
+
+      return true;
+    });
   }
 }
index 79a02a42d5673cd33592ba53925c49fd49416ba5..9c383611c81f85ae7bd0e03cbc5ec8e71d4bb8a0 100644 (file)
@@ -48,5 +48,15 @@ public class PopulatePermTemplatesUsersUserUuid extends DataChange {
       update.setString(2, permTemplatesUuid);
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select uuid from perm_templates_users where user_uuid is null");
+    massUpdate.update("delete from perm_templates_users where uuid = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setString(1, row.getString(1));
+      return true;
+    });
   }
 }
index c45e42db93a780d463ca25bcc677e4e45f11b33f..bb40326d8015e7440a65520b0837d3660373741d 100644 (file)
@@ -48,5 +48,15 @@ public class PopulateQProfileEditUsersUserUuid extends DataChange {
       update.setString(2, permTemplatesUuid);
       return true;
     });
+
+    massUpdate = context.prepareMassUpdate();
+
+    massUpdate.select("select uuid from qprofile_edit_users where user_uuid is null");
+    massUpdate.update("delete from qprofile_edit_users where uuid = ?");
+
+    massUpdate.execute((row, update) -> {
+      update.setString(1, row.getString(1));
+      return true;
+    });
   }
 }
index 7591b9e6cc86d1ecce67c6c33f3c1aaecad51605..4ba7bb81754183d027a3334ff0cee49d7349a165 100644 (file)
@@ -46,6 +46,8 @@ public class PopulateActiveRulesRuleUuidColumnTest {
     insertActiveRule("uuid-ar-1", 1L, "uuid-profile-1");
     insertActiveRule("uuid-ar-2", 1L, "uuid-profile-2");
     insertActiveRule("uuid-ar-3", 2L, "uuid-profile-1");
+    // orphan FK
+    insertActiveRule("uuid-ar-4", 10L, "uuid-profile-1");
   }
 
   @Test
index 7f9c48b61c03d54064f8792636ef923818977d87..edd75678eb773c23ab5711167cc0155a1d2a8560 100644 (file)
@@ -47,6 +47,8 @@ public class PopulateDeprecatedRuleKeysRuleUuidColumnTest {
     insertDeprecatedRuleKeyRow("uuid-drk-1", 1L);
     insertDeprecatedRuleKeyRow("uuid-drk-2", 1L);
     insertDeprecatedRuleKeyRow("uuid-drk-3", 2L);
+    // orphan FK
+    insertDeprecatedRuleKeyRow("uuid-drk-4", 10L);
   }
 
   @Test
index aa1217b9387e5adaa846b78a4d21d5fc2f79c234..7e7787bdc28ed477f572cb0376c6c92cc784ee61 100644 (file)
@@ -62,9 +62,13 @@ public class PopulateGroupsUsersUserUuidTest {
     insertGroupUser(userId_3, groupUuid_3);
     String groupUuid_4 = Uuids.createFast();
     insertGroupUser(userId_1, groupUuid_4);
+    // orphan FK
+    String groupUuid_5 = Uuids.createFast();
+    insertGroupUser(100L, groupUuid_5);
 
     underTest.execute();
 
+    assertThat(db.countRowsOfTable("groups_users")).isEqualTo(4);
     assertThatGroupsUserUserUuidIsEqualTo(userId_1, groupUuid_1, userUuid_1);
     assertThatGroupsUserUserUuidIsEqualTo(userId_2, groupUuid_2, userUuid_2);
     assertThatGroupsUserUserUuidIsEqualTo(userId_3, groupUuid_3, userUuid_3);
index 554a985b4556fc1c17d257067fca8d19c888aed4..913ae086fc2fe13d86abaf5e5060989509cb7b00 100644 (file)
@@ -64,8 +64,13 @@ public class PopulateOrganizationMembersUserUuidTest {
     String organizationUuid_5 = Uuids.createFast();
     insertOrganizationMember(userId_1, organizationUuid_5);
 
+    // orphan FK
+    String organizationUuid_6 = Uuids.createFast();
+    insertOrganizationMember(100L, organizationUuid_6);
+
     underTest.execute();
 
+    assertThat(db.countRowsOfTable("organization_members")).isEqualTo(5);
     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_1, organizationUuid_1, userUuid_1);
     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_2, organizationUuid_2, userUuid_2);
     assertThatQProfileChangeRulesProfileUuidIsEqualTo(userId_3, organizationUuid_3, userUuid_3);
index 9e167a77981cf478db43970d889b216b19c57243..16cf78b061cf4e60f77645b8b3b6e056202118f9 100644 (file)
@@ -64,8 +64,13 @@ public class PopulatePermTemplatesUsersUserUuidTest {
     String permTemplatesUserUuid_5 = Uuids.createFast();
     insertPermTemplatesUser(permTemplatesUserUuid_5, userId_1);
 
+    // orphan FK
+    String permTemplatesUserUuid_6 = Uuids.createFast();
+    insertPermTemplatesUser(permTemplatesUserUuid_6, 100L);
+
     underTest.execute();
 
+    assertThat(db.countRowsOfTable("perm_templates_users")).isEqualTo(5);
     assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_1, userUuid_1);
     assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_2, userUuid_2);
     assertThatPermTemplatesUsersUserUuidIsEqualTo(permTemplatesUserUuid_3, userUuid_3);
index 05a194a1b64d5ef5ecdb7099b5bd67b2d151c059..1a1863829ddd01db4a0eca1c1ffcd9f5ba61bd74 100644 (file)
@@ -64,8 +64,13 @@ public class PopulateQProfileEditUsersUserUuidTest {
     String qprofileEditUserUuid_5 = Uuids.createFast();
     insertQProfileEditUser(qprofileEditUserUuid_5, userId_1);
 
+    // orphan FK
+    String qprofileEditUserUuid_6 = Uuids.createFast();
+    insertQProfileEditUser(qprofileEditUserUuid_6, 100L);
+
     underTest.execute();
 
+    assertThat(db.countRowsOfTable("qprofile_edit_users")).isEqualTo(5);
     assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_1, userUuid_1);
     assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_2, userUuid_2);
     assertThatQProfileEditUserUserUuidIsEqualTo(qprofileEditUserUuid_3, userUuid_3);