From 7925daf9d5b9978f5dcfbde74fe950091bd209fa Mon Sep 17 00:00:00 2001 From: Michal Duda Date: Mon, 30 Nov 2020 18:06:25 +0100 Subject: [PATCH] SONAR-14033 fix migration for applications with no projects --- ...rateApplicationDefinitionsFromXmlToDb.java | 9 +++++--- ...ApplicationDefinitionsFromXmlToDbTest.java | 22 ++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDb.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDb.java index 72675effcc2..62257488f0f 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDb.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDb.java @@ -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 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 projectUuidsByKeys = context.prepareSelect(format(SELECT_PROJECTS_BY_KEYS, queryParam)) .list(r -> new AbstractMap.SimpleEntry<>(r.getString(1), r.getString(2))) .stream() diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest.java index ad79409fd34..856943d0644 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/MigrateApplicationDefinitionsFromXmlToDbTest.java @@ -61,7 +61,7 @@ public class MigrateApplicationDefinitionsFromXmlToDbTest { private static final String EMPTY_XML = ""; private static final String EMPTY_APP_XML = "\n" + - " \n" + + " \n" + " \n" + " \n" + " \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(); } -- 2.39.5