]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14078 fix duplication of notification keys after upgrading to SonarQube 8.4+
authorZipeng WU <zipeng.wu@sonarsource.com>
Wed, 21 Apr 2021 14:44:43 +0000 (16:44 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 22 Apr 2021 20:03:34 +0000 (20:03 +0000)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/properties/PopulatePropertiesUserUuid.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v84/users/fk/properties/PopulatePropertiesUserUuidTest.java

index 6bc811f3b6ead23e8ffbd5252d84d1d7bc5d5fd7..87d148cb8a3994603828281d032a8e9603754fa3 100644 (file)
@@ -32,6 +32,11 @@ public class PopulatePropertiesUserUuid extends DataChange {
 
   @Override
   protected void execute(Context context) throws SQLException {
+    populateUserUuid(context);
+    removeRowWithNonExistentUser(context);
+  }
+
+  private static void populateUserUuid(Context context) throws SQLException {
     MassUpdate massUpdate = context.prepareMassUpdate();
 
     massUpdate.select("select p.uuid, u.uuid " +
@@ -49,4 +54,10 @@ public class PopulatePropertiesUserUuid extends DataChange {
       return true;
     });
   }
+
+  private static void removeRowWithNonExistentUser(Context context) throws SQLException {
+    context.prepareUpsert("delete from properties where user_uuid is null and user_id is not null")
+      .execute()
+      .commit();
+  }
 }
index 1f62b88a2e83a4a0f864e57d9542b49072378ec8..2550fec05cec2029b13c96700b25c0012e896ccf 100644 (file)
@@ -74,6 +74,30 @@ public class PopulatePropertiesUserUuidTest {
     assertThatPropertyUserUuidIsEqualTo(propertyUuid_5, null);
   }
 
+  @Test
+  public void should_remove_property_for_non_existent_user() throws SQLException {
+    long userId_1 = 1L;
+    String userUuid_1 = "uuid-1";
+    insertUser(userId_1, userUuid_1);
+
+    long userId_2 = 2L;
+
+    long userId_3 = 3L;
+
+    String propertyUuid_1 = Uuids.createFast();
+    insertProperty(propertyUuid_1, userId_1);
+    String propertyUuid_2 = Uuids.createFast();
+    insertProperty(propertyUuid_2, userId_2);
+    String propertyUuid_3 = Uuids.createFast();
+    insertProperty(propertyUuid_3, userId_3);
+
+    underTest.execute();
+
+    assertThatPropertyUserUuidIsEqualTo(propertyUuid_1, userUuid_1);
+    assertPropertyIsRemoved(propertyUuid_2);
+    assertPropertyIsRemoved(propertyUuid_3);
+  }
+
   @Test
   public void migration_is_reentrant() throws SQLException {
     long userId_1 = 1L;
@@ -131,6 +155,10 @@ public class PopulatePropertiesUserUuidTest {
 
   }
 
+  private void assertPropertyIsRemoved(String propertyUuid){
+    assertThat(db.select(String.format("select 1 from properties where uuid = '%s'", propertyUuid))).isEmpty();
+  }
+
   private void insertProperty(String uuid, Long userId) {
     db.executeInsert("properties",
       "uuid", uuid,