From: Julien Lancelot Date: Mon, 22 Feb 2016 15:23:37 +0000 (+0100) Subject: SONAR-7330 Add long dates in active rules X-Git-Tag: 5.5-M6~81 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a530cb94df60f4293ee8524edc65eaa63c58e0a4;p=sonarqube.git SONAR-7330 Add long dates in active rules --- diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1104_add_active_rules_long_date_columns.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1104_add_active_rules_long_date_columns.rb new file mode 100644 index 00000000000..2da4bd516c7 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1104_add_active_rules_long_date_columns.rb @@ -0,0 +1,31 @@ +# +# 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 diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1105_feed_active_rules_long_date_columns.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1105_feed_active_rules_long_date_columns.rb new file mode 100644 index 00000000000..175925583cf --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1105_feed_active_rules_long_date_columns.rb @@ -0,0 +1,31 @@ +# +# 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 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 @@ + + + + + + + + + + + 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 +);