]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14033 fix migration for applications with no projects
authorMichal Duda <michal.duda@sonarsource.com>
Mon, 30 Nov 2020 17:06:25 +0000 (18:06 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 2 Dec 2020 20:06:57 +0000 (20:06 +0000)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDb.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest.java

index 72675effcc2b1e49cae8aa78cc5de87aff2a3eaf..62257488f0f994dfb491ab5512e90f037db30b16 100644 (file)
@@ -118,12 +118,15 @@ public class MigrateApplicationDefinitionsFromXmlToDb extends DataChange {
       .setString(1, app.getKey())
       .get(r -> r.getString(1));
 
-    // ignore if application only exists in xml and not in the db. It will be removed from the xml at later stage of the migration.
-    if (applicationUuid == null) {
+    // skip migration if:
+    // - application only exists in xml and not in the db. It will be removed from the xml at later stage of the migration.
+    // - application contains no projects- it's already in a valid db state
+    List<String> projects = app.getProjects();
+    if (applicationUuid == null || projects.isEmpty()) {
       return;
     }
 
-    String queryParam = app.getProjects().stream().map(uuid -> "'" + uuid + "'").collect(Collectors.joining(","));
+    String queryParam = projects.stream().map(uuid -> "'" + uuid + "'").collect(Collectors.joining(","));
     Map<String, String> projectUuidsByKeys = context.prepareSelect(format(SELECT_PROJECTS_BY_KEYS, queryParam))
       .list(r -> new AbstractMap.SimpleEntry<>(r.getString(1), r.getString(2)))
       .stream()
index ad79409fd34d89a7975d520d96d98248efa4450f..856943d06443aeaed22f1f2c97f35a2b9aeb5622 100644 (file)
@@ -61,7 +61,7 @@ public class MigrateApplicationDefinitionsFromXmlToDbTest {
   private static final String EMPTY_XML = "<views></views>";
 
   private static final String EMPTY_APP_XML = "<views>\n" +
-    "    <vw key=\"app1\" def=\"false\">\n" +
+    "    <vw key=\"app1-key\" def=\"false\">\n" +
     "        <name><![CDATA[app1]]></name>\n" +
     "        <desc><![CDATA[]]></desc>\n" +
     "        <qualifier><![CDATA[APP]]></qualifier>\n" +
@@ -453,16 +453,32 @@ public class MigrateApplicationDefinitionsFromXmlToDbTest {
     assertThat(db.countSql("select count(*) from app_branch_project_branch")).isZero();
   }
 
+  @Test
+  public void skips_apps_that_exist_in_the_definition_but_does_not_exist_in_db() throws SQLException {
+    setupFullProject1();
+    insertViewsDefInternalProperty(EMPTY_APP_XML);
+
+    underTest.execute();
+
+    assertThat(db.select("select uuid from projects"))
+      .extracting(r -> r.get("UUID"))
+      .containsExactlyInAnyOrder(PROJECT_1_UUID);
+    assertThat(db.countSql("select count(*) from app_projects")).isZero();
+    assertThat(db.countSql("select count(*) from app_branch_project_branch")).isZero();
+  }
+
   @Test
   public void migrates_app_with_0_projects_in_views_definition() throws SQLException {
-    setupProjectsAndApps();
+    setupFullProject1();
+    setupProject2();
+    setupApp1WithNoBranches();
     insertViewsDefInternalProperty(EMPTY_APP_XML);
 
     underTest.execute();
 
     assertThat(db.select("select uuid from projects"))
       .extracting(r -> r.get("UUID"))
-      .containsExactlyInAnyOrder(PROJECT_1_UUID, PROJECT_2_UUID, APP_1_UUID, APP_2_UUID);
+      .containsExactlyInAnyOrder(PROJECT_1_UUID, PROJECT_2_UUID, APP_1_UUID);
     assertThat(db.countSql("select count(*) from app_projects")).isZero();
     assertThat(db.countSql("select count(*) from app_branch_project_branch")).isZero();
   }