aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Hartmann <hartmann.eric@gmail.com>2017-12-04 15:38:19 +0100
committerEric Hartmann <hartmann.eric@gmail.com>2017-12-04 15:38:19 +0100
commit2ba36cab8ba668c3dfa4c6e33399bd076b356adf (patch)
tree37408a7e634b800d4ec7b32407af5d8566c32a3d
parent1795cff89143b881f8d832a22d0dcec8a6abbc19 (diff)
downloadsonarqube-2ba36cab8ba668c3dfa4c6e33399bd076b356adf.tar.gz
sonarqube-2ba36cab8ba668c3dfa4c6e33399bd076b356adf.zip
SONAR-10089 Fix migration on MySQL
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGate.java53
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGateTest.java59
2 files changed, 34 insertions, 78 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGate.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGate.java
index 0a0a20b63aa..3ba88942c0b 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGate.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGate.java
@@ -22,14 +22,15 @@ package org.sonar.server.platform.db.migration.version.v70;
import java.sql.SQLException;
import java.util.Date;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;
-import static java.lang.String.format;
-
public class RenameOldSonarQubeWayQualityGate extends DataChange {
+ private static final Logger LOG = Loggers.get(RenameOldSonarQubeWayQualityGate.class);
private static final String SONARQUBE_WAY_QUALITY_GATE = "SonarQube way";
private static final String SONARQUBE_WAY_QUALITY_GATE_OUTDATED = "Sonar way (outdated copy)";
private final System2 system2;
@@ -41,38 +42,32 @@ public class RenameOldSonarQubeWayQualityGate extends DataChange {
@Override
protected void execute(Context context) throws SQLException {
- MassUpdate massUpdate = context.prepareMassUpdate();
- massUpdate.select("SELECT id FROM quality_gates WHERE name = ?")
- .setString(1, SONARQUBE_WAY_QUALITY_GATE);
- massUpdate.rowPluralName("quality gates");
- massUpdate.update("UPDATE quality_gates SET name=?, is_built_in=?, updated_at=? WHERE id=?");
- massUpdate.execute((row, update) -> {
- update.setString(1, findNewQualityGateName(context));
- update.setBoolean(2, false);
- update.setDate(3, new Date(system2.now()));
- update.setLong(4, row.getLong(1));
- return true;
- });
- }
+ //ensureThatOutdatedCopyQDoesNotExist(context);
- private String findNewQualityGateName(Context context) throws SQLException {
- if (isQualityGateNameAvailable(context, SONARQUBE_WAY_QUALITY_GATE_OUTDATED)) {
- return SONARQUBE_WAY_QUALITY_GATE_OUTDATED;
+ try {
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("SELECT id FROM quality_gates WHERE name = ?")
+ .setString(1, SONARQUBE_WAY_QUALITY_GATE);
+ massUpdate.rowPluralName("quality gates");
+ massUpdate.update("UPDATE quality_gates SET name=?, is_built_in=?, updated_at=? WHERE id=?");
+ massUpdate.execute((row, update) -> {
+ update.setString(1, SONARQUBE_WAY_QUALITY_GATE_OUTDATED);
+ update.setBoolean(2, false);
+ update.setDate(3, new Date(system2.now()));
+ update.setLong(4, row.getLong(1));
+ return true;
+ });
+ } catch(Exception ex) {
+ LOG.error("There is already a quality profile with name [{}]", SONARQUBE_WAY_QUALITY_GATE_OUTDATED);
+ throw ex;
}
-
- String newName = SONARQUBE_WAY_QUALITY_GATE_OUTDATED + " " + system2.now();
- if (isQualityGateNameAvailable(context, newName)) {
- return newName;
- }
-
- // Given up if no name available
- throw new IllegalStateException(format("There are already two quality profiles with name [%s,%s]", SONARQUBE_WAY_QUALITY_GATE_OUTDATED, newName));
}
- private static boolean isQualityGateNameAvailable(Context context, String qualityGateName) throws SQLException {
+ private boolean isOutdatedCopyQGExists(Context context) throws SQLException {
return context.prepareSelect(
- "SELECT COUNT(id) FROM quality_gates WHERE name = '" + qualityGateName + "'")
+ "SELECT COUNT(id) FROM quality_gates WHERE name = ?")
+ .setString(1, SONARQUBE_WAY_QUALITY_GATE_OUTDATED)
.get(
- row -> row.getInt(1) == 0);
+ row -> row.getInt(1) > 0);
}
}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGateTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGateTest.java
index 55aba3d230f..798ea6e7b8e 100644
--- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGateTest.java
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/RenameOldSonarQubeWayQualityGateTest.java
@@ -23,13 +23,12 @@ import java.sql.SQLException;
import java.util.Date;
import java.util.stream.Collectors;
import org.assertj.core.groups.Tuple;
-import org.hamcrest.Description;
-import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.api.utils.log.LogTester;
import org.sonar.db.CoreDbTester;
import static org.assertj.core.api.Assertions.assertThat;
@@ -48,6 +47,8 @@ public class RenameOldSonarQubeWayQualityGateTest {
public ExpectedException expectedException = ExpectedException.none();
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(PopulateQualityGatesIsBuiltInTest.class, "quality_gates.sql");
+ @Rule
+ public LogTester logTester = new LogTester();
private System2 system2 = new TestSystem2().setNow(NOW);
@@ -83,34 +84,18 @@ public class RenameOldSonarQubeWayQualityGateTest {
}
@Test
- public void should_not_fail_if_a_profile_with_same_name_is_present() throws SQLException {
- insertQualityGate(SONAR_WAY_OUTDATED_QUALITY_GATE, false);
- insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, false);
-
- underTest.execute();
-
- assertQualityGates(
- tuple(SONAR_WAY_OUTDATED_QUALITY_GATE, false, new Date(PAST), new Date(PAST)),
- tuple(SONAR_WAY_OUTDATED_QUALITY_GATE + " " + NOW, false, new Date(PAST), new Date(NOW))
- );
- }
-
- @Test
- public void should_throw_ISE_if_no_name_is_available() throws SQLException {
+ public void should_log_a_meaningful_info_if_outdated_copy_exists() throws SQLException {
insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, false);
insertQualityGate(SONAR_WAY_OUTDATED_QUALITY_GATE, false);
- insertQualityGate(SONAR_WAY_OUTDATED_QUALITY_GATE + " " + system2.now(), false);
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Error during processing of row:");
- expectedException.expectCause(
- new CauseMatcher(
- IllegalStateException.class,
- "There are already two quality profiles with name [Sonar way (outdated copy),Sonar way (outdated copy) 50000000000]"));
- underTest.execute();
+ try {
+ underTest.execute();
+ } catch (Exception ex) {
+ logTester.logs().contains("There is already a quality profile with name [Sonar way (outdated copy)]");
+ }
}
+
@Test
public void should_update_only_SonarQubeWay() throws SQLException {
insertQualityGate("Whatever", true);
@@ -157,28 +142,4 @@ public class RenameOldSonarQubeWayQualityGateTest {
"CREATED_AT", new Date(PAST),
"UPDATED_AT", new Date(PAST));
}
-
- private static class CauseMatcher extends TypeSafeMatcher<Throwable> {
- private final Class<? extends Throwable> type;
- private final String expectedMessage;
-
- public CauseMatcher(Class<? extends Throwable> type, String expectedMessage) {
- this.type = type;
- this.expectedMessage = expectedMessage;
- }
-
- @Override
- protected boolean matchesSafely(Throwable item) {
- return item.getClass().isAssignableFrom(type)
- && item.getMessage().contains(expectedMessage);
- }
-
- @Override
- public void describeTo(Description description) {
- description.appendText("expects type ")
- .appendValue(type)
- .appendText(" and a message ")
- .appendValue(expectedMessage);
- }
- }
}