From 10729609eb21a9a07c77111e1f4e15e266379078 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Thu, 4 May 2023 17:02:14 +0200 Subject: [PATCH] SONAR-18917 Fix migration failure and support user_token without project key --- .../migration/version/v101/RemoveOrphanUserTokens.java | 6 +++--- .../version/v101/RemoveOrphanUserTokensTest.java | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokens.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokens.java index 9345262d503..18eeabb94c9 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokens.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokens.java @@ -31,7 +31,7 @@ public class RemoveOrphanUserTokens extends DataChange { SELECT ut.uuid as tokenUuid FROM user_tokens ut LEFT OUTER JOIN projects p ON ut.project_key = p.kee - WHERE p.uuid is null + WHERE p.uuid is null and ut.project_key IS NOT NULL """; public RemoveOrphanUserTokens(Database db) { @@ -44,7 +44,7 @@ public class RemoveOrphanUserTokens extends DataChange { if (DatabaseUtils.tableColumnExists(connection, "user_tokens", "project_key")) { MassUpdate massUpdate = context.prepareMassUpdate(); massUpdate.select(SELECT_QUERY); - massUpdate.update("delete user_tokens where uuid = ?"); + massUpdate.update("delete from user_tokens where uuid = ?"); massUpdate.execute((row, update) -> { String uuid = row.getString(1); update.setString(1, uuid); @@ -53,4 +53,4 @@ public class RemoveOrphanUserTokens extends DataChange { } } } -} \ No newline at end of file +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokensTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokensTest.java index b659b6b722d..be11ab3ff84 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokensTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/RemoveOrphanUserTokensTest.java @@ -22,12 +22,12 @@ package org.sonar.server.platform.db.migration.version.v101; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; +import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.sonar.core.util.UuidFactory; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.CoreDbTester; -import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder; import org.sonar.server.platform.db.migration.step.DataChange; import static org.assertj.core.api.Assertions.assertThat; @@ -46,11 +46,12 @@ public class RemoveOrphanUserTokensTest { String token1Uuid = insertUserToken("project1"); String token2Uuid = insertUserToken("orphan"); + String token3Uuid = insertUserToken(null); underTest.execute(); assertThat(db.select("select * from user_tokens")) .extracting(r -> r.get("UUID")) - .containsOnly(token1Uuid); + .containsOnly(token1Uuid, token3Uuid); } @Test @@ -68,7 +69,7 @@ public class RemoveOrphanUserTokensTest { .containsOnly(token1Uuid); } - private String insertUserToken(String projectKey) { + private String insertUserToken( @Nullable String projectKey) { Map map = new HashMap<>(); String uuid = uuidFactory.create(); map.put("UUID", uuid); @@ -94,4 +95,4 @@ public class RemoveOrphanUserTokensTest { db.executeInsert("projects", map); return uuid; } -} \ No newline at end of file +} -- 2.39.5