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) {
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);
}
}
}
-}
\ No newline at end of file
+}
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;
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
.containsOnly(token1Uuid);
}
- private String insertUserToken(String projectKey) {
+ private String insertUserToken( @Nullable String projectKey) {
Map<String, Object> map = new HashMap<>();
String uuid = uuidFactory.create();
map.put("UUID", uuid);
db.executeInsert("projects", map);
return uuid;
}
-}
\ No newline at end of file
+}