--- /dev/null
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 5.5
+# SONAR-7330
+#
+class AddActiveRulesLongDateColumns < ActiveRecord::Migration
+
+ def self.up
+ execute_java_migration('org.sonar.db.version.v55.AddActiveRulesLongDateColumns')
+ end
+
+end
--- /dev/null
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 5.5
+# SONAR-7330
+#
+class FeedActiveRulesLongDateColumns < ActiveRecord::Migration
+
+ def self.up
+ execute_java_migration('org.sonar.db.version.v55.FeedActiveRulesLongDateColumns')
+ end
+
+end
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;
AddRulesLongDateColumns.class,
FeedRulesLongDateColumns.class,
DeleteMeasuresWithCharacteristicId.class,
+ AddActiveRulesLongDateColumns.class,
+ FeedActiveRulesLongDateColumns.class,
+ DeleteMeasuresWithCharacteristicId.class,
AddIssuesType.class,
FeedIssueTypes.class
);
--- /dev/null
+/*
+ * 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();
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+ }
+ }
+
+}
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);
"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" (
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+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
+);
--- /dev/null
+<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>
--- /dev/null
+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
+);