aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-22 16:23:37 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-29 13:26:54 +0100
commita530cb94df60f4293ee8524edc65eaa63c58e0a4 (patch)
treed6b07844249f5e360f4bad165322fa9c5ed0281a /sonar-db
parent2e404abaa6f46530f5322ec1ce09c4fc7ded14aa (diff)
downloadsonarqube-a530cb94df60f4293ee8524edc65eaa63c58e0a4.tar.gz
sonarqube-a530cb94df60f4293ee8524edc65eaa63c58e0a4.zip
SONAR-7330 Add long dates in active rules
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java5
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumns.java55
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumns.java80
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql2
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl4
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest.java50
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest.java60
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest/schema.sql9
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/execute.xml23
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/schema.sql11
10 files changed, 298 insertions, 1 deletions
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 ac74da47d49..b4d9ac00205 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
@@ -69,8 +69,10 @@ import org.sonar.db.version.v54.MigrateQualityGatesConditions;
import org.sonar.db.version.v54.MigrateUsersIdentity;
import org.sonar.db.version.v54.RemoveComponentPageProperties;
import org.sonar.db.version.v54.RemovePreviewPermission;
+import org.sonar.db.version.v55.AddActiveRulesLongDateColumns;
import org.sonar.db.version.v55.AddIssuesType;
import org.sonar.db.version.v55.AddRulesLongDateColumns;
+import org.sonar.db.version.v55.FeedActiveRulesLongDateColumns;
import org.sonar.db.version.v55.DeleteMeasuresWithCharacteristicId;
import org.sonar.db.version.v55.FeedIssueTypes;
import org.sonar.db.version.v55.FeedRulesLongDateColumns;
@@ -144,6 +146,9 @@ public class MigrationStepModule extends Module {
AddRulesLongDateColumns.class,
FeedRulesLongDateColumns.class,
DeleteMeasuresWithCharacteristicId.class,
+ AddActiveRulesLongDateColumns.class,
+ FeedActiveRulesLongDateColumns.class,
+ DeleteMeasuresWithCharacteristicId.class,
AddIssuesType.class,
FeedIssueTypes.class
);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumns.java b/sonar-db/src/main/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumns.java
new file mode 100644
index 00000000000..4074e238fb5
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumns.java
@@ -0,0 +1,55 @@
+/*
+ * 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.v55;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.AddColumnsBuilder;
+import org.sonar.db.version.DdlChange;
+
+import static org.sonar.db.version.BigDecimalColumnDef.newBigDecimalColumnDefBuilder;
+
+/**
+ * Add the following columns to the active_rules table :
+ * - created_at_ms
+ * - updated_at_ms
+ */
+public class AddActiveRulesLongDateColumns extends DdlChange {
+
+ private final Database db;
+
+ public AddActiveRulesLongDateColumns(Database db) {
+ super(db);
+ this.db = db;
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(generateSql());
+ }
+
+ private String generateSql() {
+ return new AddColumnsBuilder(db.getDialect(), "active_rules")
+ .addColumn(newBigDecimalColumnDefBuilder().setColumnName("created_at_ms").setIsNullable(true).build())
+ .addColumn(newBigDecimalColumnDefBuilder().setColumnName("updated_at_ms").setIsNullable(true).build())
+ .build();
+ }
+
+}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumns.java b/sonar-db/src/main/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumns.java
new file mode 100644
index 00000000000..53e5536deb5
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumns.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.v55;
+
+import java.sql.SQLException;
+import java.util.Date;
+import org.sonar.api.utils.System2;
+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;
+
+public class FeedActiveRulesLongDateColumns extends BaseDataChange {
+
+ private final System2 system;
+
+ public FeedActiveRulesLongDateColumns(Database db, System2 system) {
+ super(db);
+ this.system = system;
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ final long now = system.now();
+
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("SELECT ar.id, ar.created_at, ar.updated_at FROM active_rules ar WHERE created_at_ms IS NULL OR updated_at_ms IS NULL");
+ massUpdate.update("UPDATE active_rules SET created_at_ms=?, updated_at_ms=? WHERE id=?");
+ massUpdate.rowPluralName("active rules");
+ massUpdate.execute(new MigrationHandler(now));
+ }
+
+ private static class MigrationHandler implements MassUpdate.Handler{
+
+ private final long now;
+
+ public MigrationHandler(long now) {
+ this.now = now;
+ }
+
+ @Override
+ public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
+ Long id = row.getNullableLong(1);
+ Date createdAt = row.getNullableDate(2);
+ Date updatedAt = row.getNullableDate(3);
+
+ if (createdAt == null) {
+ update.setLong(1, now);
+ } else {
+ update.setLong(1, Math.min(now, createdAt.getTime()));
+ }
+ if (updatedAt == null) {
+ update.setLong(2, now);
+ } else {
+ update.setLong(2, Math.min(now, updatedAt.getTime()));
+ }
+ update.setLong(3, id);
+ 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 3c87dc8ed22..4ecc1757f51 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
@@ -381,6 +381,8 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1100');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1101');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1102');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1103');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1104');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1105');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1107');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl
index bd490bd5036..bedb09a40c6 100644
--- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl
+++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl
@@ -263,7 +263,9 @@ CREATE TABLE "ACTIVE_RULES" (
"FAILURE_LEVEL" INTEGER NOT NULL,
"INHERITANCE" VARCHAR(10),
"CREATED_AT" TIMESTAMP,
- "UPDATED_AT" TIMESTAMP
+ "UPDATED_AT" TIMESTAMP,
+ "CREATED_AT_MS" BIGINT,
+ "UPDATED_AT_MS" BIGINT
);
CREATE TABLE "NOTIFICATIONS" (
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest.java
new file mode 100644
index 00000000000..20b1d6bf2a9
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.v55;
+
+import java.sql.Types;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.version.MigrationStep;
+
+public class AddActiveRulesLongDateColumnsTest {
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddActiveRulesLongDateColumnsTest.class, "schema.sql");
+
+ MigrationStep migration;
+
+ @Before
+ public void setUp() {
+ migration = new AddActiveRulesLongDateColumns(db.database());
+ }
+
+ @Test
+ public void update_columns() throws Exception {
+ migration.execute();
+
+ db.assertColumnDefinition("active_rules", "created_at_ms", Types.BIGINT, null);
+ db.assertColumnDefinition("active_rules", "updated_at_ms", Types.BIGINT, null);
+ }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest.java
new file mode 100644
index 00000000000..623b33c92ca
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.v55;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+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 FeedActiveRulesLongDateColumnsTest {
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedActiveRulesLongDateColumnsTest.class, "schema.sql");
+
+ static final long NOW = 1500000000000L;
+
+ System2 system = mock(System2.class);
+
+ MigrationStep migration = new FeedActiveRulesLongDateColumns(db.database(), system);
+
+ @Before
+ public void setUp() throws Exception {
+ when(system.now()).thenReturn(NOW);
+ }
+
+ @Test
+ public void execute() throws Exception {
+ db.prepareDbUnit(getClass(), "execute.xml");
+
+ migration.execute();
+
+ assertThat(db.countSql("select count(*) from active_rules where created_at_ms is not null and updated_at_ms is not null")).isEqualTo(3);
+ // Only 1 row has not been updated
+ assertThat(db.countSql("select count(*) from active_rules where created_at_ms='1000000000000' and updated_at_ms='1000000000000'")).isEqualTo(1);
+ }
+
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest/schema.sql
new file mode 100644
index 00000000000..0342a4690cb
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/AddActiveRulesLongDateColumnsTest/schema.sql
@@ -0,0 +1,9 @@
+CREATE TABLE "ACTIVE_RULES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "PROFILE_ID" INTEGER NOT NULL,
+ "RULE_ID" INTEGER NOT NULL,
+ "FAILURE_LEVEL" INTEGER NOT NULL,
+ "INHERITANCE" VARCHAR(10),
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP
+);
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/execute.xml b/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/execute.xml
new file mode 100644
index 00000000000..e0f957df1d9
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/execute.xml
@@ -0,0 +1,23 @@
+<dataset>
+
+ <active_rules id="1" profile_id="10" rule_id="100"
+ failure_level="4" inheritance="[null]"
+ created_at="2014-05-12" updated_at="2014-05-13"
+ created_at_ms="[null]" updated_at_ms="[null]"
+ />
+
+ <!-- re-entrant migration - ignore the active rules that are already fed with new dates -->
+ <active_rules id="2" profile_id="10" rule_id="101"
+ failure_level="2" inheritance="[null]"
+ created_at="2014-05-10" updated_at="2014-05-11"
+ created_at_ms="1000000000000" updated_at_ms="1000000000000"
+ />
+
+ <!-- NULL dates -->
+ <active_rules id="3" profile_id="11" rule_id="100"
+ failure_level="3" inheritance="[null]"
+ created_at="[null]" updated_at="[null]"
+ created_at_ms="[null]" updated_at_ms="[null]"
+ />
+
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/schema.sql
new file mode 100644
index 00000000000..cbbff3d0d89
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedActiveRulesLongDateColumnsTest/schema.sql
@@ -0,0 +1,11 @@
+CREATE TABLE "ACTIVE_RULES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "PROFILE_ID" INTEGER NOT NULL,
+ "RULE_ID" INTEGER NOT NULL,
+ "FAILURE_LEVEL" INTEGER NOT NULL,
+ "INHERITANCE" VARCHAR(10),
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP,
+ "CREATED_AT_MS" BIGINT,
+ "UPDATED_AT_MS" BIGINT
+);