aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-01-26 18:39:28 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-01-29 15:59:36 +0100
commita2cf8a110df9213200ab495f75003ce7833a0033 (patch)
treed80a4742d1f5a6218af049c2a51deddcf19b05c1 /sonar-db
parent77af82fd520c9579a083c458ba68d55c5177d96a (diff)
downloadsonarqube-a2cf8a110df9213200ab495f75003ce7833a0033.tar.gz
sonarqube-a2cf8a110df9213200ab495f75003ce7833a0033.zip
SONAR-7070 Enforced leak period in quality gates - migration script
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/v54/MigrateQualityGatesConditions.java82
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql1
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java2
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java80
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml11
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml11
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql11
9 files changed, 201 insertions, 3 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
index 011218e5900..43be9647d12 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
@@ -29,7 +29,7 @@ import org.sonar.db.MyBatis;
public class DatabaseVersion {
- public static final int LAST_VERSION = 1012;
+ public static final int LAST_VERSION = 1013;
/**
* The minimum supported version which can be upgraded. Lower
diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
index 4591717227e..c51bed75928 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
@@ -63,6 +63,7 @@ import org.sonar.db.version.v53.FixMsSqlCollation;
import org.sonar.db.version.v53.UpdateCustomDashboardInLoadedTemplates;
import org.sonar.db.version.v54.AddUsersIdentityColumns;
import org.sonar.db.version.v54.InsertGateAdminPermissionForEachProfileAdmin;
+import org.sonar.db.version.v54.MigrateQualityGatesConditions;
import org.sonar.db.version.v54.MigrateUsersIdentity;
import org.sonar.db.version.v54.RemoveComponentPageProperties;
@@ -125,7 +126,8 @@ public class MigrationStepModule extends Module {
InsertGateAdminPermissionForEachProfileAdmin.class,
RemoveComponentPageProperties.class,
AddUsersIdentityColumns.class,
- MigrateUsersIdentity.class
+ MigrateUsersIdentity.class,
+ MigrateQualityGatesConditions.class
);
}
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v54/MigrateQualityGatesConditions.java b/sonar-db/src/main/java/org/sonar/db/version/v54/MigrateQualityGatesConditions.java
new file mode 100644
index 00000000000..bb06a9e9624
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/version/v54/MigrateQualityGatesConditions.java
@@ -0,0 +1,82 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.db.version.v54;
+
+import com.google.common.base.Joiner;
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Set;
+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.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+import org.sonar.db.version.Select;
+import org.sonar.db.version.SqlStatement;
+
+/**
+ * Update all quality gates condition periods to leak period when period is provided
+ */
+public class MigrateQualityGatesConditions extends BaseDataChange {
+ private static final Logger LOG = Loggers.get(MigrateQualityGatesConditions.class);
+
+ private final System2 system2;
+
+ public MigrateQualityGatesConditions(Database db, System2 system2) {
+ super(db);
+ this.system2 = system2;
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ MassUpdate update = context.prepareMassUpdate().rowPluralName("quality gate conditions");
+ update.select("select qgc.id, qg.name " +
+ "from quality_gate_conditions qgc " +
+ "inner join quality_gates qg on qgc.qgate_id=qg.id " +
+ "where qgc.period is not null and qgc.period <> 1");
+ update.update("update quality_gate_conditions set period=1, updated_at=? where id=?");
+ MigrationHandler migrationHandler = new MigrationHandler(system2.now());
+ update.execute(migrationHandler);
+ if (!migrationHandler.updatedQualityGates.isEmpty()) {
+ LOG.warn("The following Quality Gates have been updated to compare with the leak period: {}.", Joiner.on(", ").join(migrationHandler.updatedQualityGates));
+ }
+ }
+
+ private static class MigrationHandler implements MassUpdate.Handler {
+ private final Date now;
+ private final Set<String> updatedQualityGates;
+
+ public MigrationHandler(long now) {
+ this.updatedQualityGates = new LinkedHashSet<>();
+ this.now = new Date(now);
+ }
+
+ @Override
+ public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
+ update.setDate(1, now);
+ update.setLong(2, row.getLong(1));
+ updatedQualityGates.add(row.getString(2));
+ return true;
+ }
+ }
+}
diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
index 0b2d72f0028..c225c4c782a 100644
--- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
+++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
@@ -374,6 +374,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1009');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1010');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1011');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1012');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1013');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
index 4fbf2496389..a374e923cf7 100644
--- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
public void verify_count_of_added_MigrationStep_types() {
ComponentContainer container = new ComponentContainer();
new MigrationStepModule().configure(container);
- assertThat(container.size()).isEqualTo(47);
+ assertThat(container.size()).isEqualTo(48);
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java
new file mode 100644
index 00000000000..a0b2e231152
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java
@@ -0,0 +1,80 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.db.version.v54;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+import org.sonar.db.DbTester;
+import org.sonar.db.version.MigrationStep;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class MigrateQualityGatesConditionsTest {
+ static final String NOW = "1919-12-24";
+ private static final String MSG_WARNING_QG_CONDITIONS_UPDATED = "The following Quality Gates have been updated to compare with the leak period: qg-1, qg-2.";
+ final System2 system2 = mock(System2.class);
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, MigrateQualityGatesConditionsTest.class, "schema.sql");
+ @Rule
+ public LogTester log = new LogTester();
+
+ MigrationStep migration;
+
+ @Before
+ public void setUp() {
+ db.truncateTables();
+ when(system2.now()).thenReturn(DateUtils.parseDate(NOW).getTime());
+ migration = new MigrateQualityGatesConditions(db.database(), system2);
+ }
+
+ @Test
+ public void migrate_empty_db() throws Exception {
+ migration.execute();
+ }
+
+ @Test
+ public void migrate() throws Exception {
+ db.prepareDbUnit(this.getClass(), "migrate.xml");
+
+ migration.execute();
+
+ db.assertDbUnit(getClass(), "migrate-result.xml", "quality_gates", "quality_gate_conditions");
+ assertThat(log.logs(LoggerLevel.WARN)).contains(MSG_WARNING_QG_CONDITIONS_UPDATED);
+ }
+
+ @Test
+ public void nothing_to_do_on_already_migrated_data() throws Exception {
+ db.prepareDbUnit(this.getClass(), "migrate-result.xml");
+
+ migration.execute();
+
+ db.assertDbUnit(getClass(), "migrate-result.xml", "quality_gates", "quality_gate_conditions");
+ assertThat(log.logs(LoggerLevel.WARN)).doesNotContain(MSG_WARNING_QG_CONDITIONS_UPDATED);
+ }
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml
new file mode 100644
index 00000000000..0792ddd3e20
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml
@@ -0,0 +1,11 @@
+<dataset>
+ <quality_gates id="1" name="qg-1"/>
+ <quality_gates id="2" name="qg-2"/>
+ <quality_gates id="3" name="qg-3"/>
+ <quality_gate_conditions id="1" qgate_id="1" period="[null]" updated_at="1900-12-01"/>
+ <quality_gate_conditions id="2" qgate_id="1" period="1" updated_at="1919-12-24"/>
+ <quality_gate_conditions id="3" qgate_id="2" period="1" updated_at="1919-12-24"/>
+ <!-- quality gate 3 conditions don't need to be updated -->
+ <quality_gate_conditions id="4" qgate_id="3" period="[null]" updated_at="1900-12-01"/>
+ <quality_gate_conditions id="5" qgate_id="3" period="1" updated_at="1900-12-01"/>
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml
new file mode 100644
index 00000000000..f2397ae9f7e
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml
@@ -0,0 +1,11 @@
+<dataset>
+ <quality_gates id="1" name="qg-1"/>
+ <quality_gates id="2" name="qg-2"/>
+ <quality_gates id="3" name="qg-3"/>
+ <quality_gate_conditions id="1" qgate_id="1" period="[null]" updated_at="1900-12-01"/>
+ <quality_gate_conditions id="2" qgate_id="1" period="2" updated_at="1900-12-01"/>
+ <quality_gate_conditions id="3" qgate_id="2" period="2" updated_at="1900-12-01"/>
+ <!-- quality gate 3 conditions don't need to be updated -->
+ <quality_gate_conditions id="4" qgate_id="3" period="[null]" updated_at="1900-12-01"/>
+ <quality_gate_conditions id="5" qgate_id="3" period="1" updated_at="1900-12-01"/>
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql
new file mode 100644
index 00000000000..4aac9f1fe01
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql
@@ -0,0 +1,11 @@
+CREATE TABLE "QUALITY_GATES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "NAME" VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE "QUALITY_GATE_CONDITIONS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "QGATE_ID" INTEGER,
+ "PERIOD" INTEGER,
+ "UPDATED_AT" TIMESTAMP
+);