From a201df8720aab627050b9f14f0b35c75780e3a92 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Mon, 29 Jan 2018 18:03:46 +0100 Subject: [PATCH] SONAR-10311 Add migration for DEPRECATED_RULE_KEYS table --- .../org/sonar/db/version/schema-h2.ddl | 9 ++ .../v71/CreateDeprecatedRuleKeysTable.java | 103 ++++++++++++++++++ .../db/migration/version/v71/DbVersion71.java | 1 + .../CreateDeprecatedRuleKeysTableTest.java | 66 +++++++++++ .../version/v71/DbVersion71Test.java | 2 +- .../empty.sql | 0 6 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest/empty.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 175424d028c..5c287cea0ec 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -171,6 +171,15 @@ CREATE TABLE "RULE_REPOSITORIES" ( "CREATED_AT" BIGINT ); +CREATE TABLE "DEPRECATED_RULE_KEYS" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "RULE_ID" INTEGER NOT NULL, + "OLD_REPOSITORY_KEY" VARCHAR(200) NOT NULL, + "OLD_RULE_KEY" VARCHAR(255) NOT NULL, + "CREATED_AT" BIGINT +); +CREATE UNIQUE INDEX "UNIQ_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS" ("OLD_REPOSITORY_KEY", "OLD_RULE_KEY"); +CREATE INDEX "RULE_ID_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS" ("RULE_ID"); CREATE TABLE "RULES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTable.java new file mode 100644 index 00000000000..db353758a56 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTable.java @@ -0,0 +1,103 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v71; + +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef; +import org.sonar.server.platform.db.migration.def.IntegerColumnDef; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; +import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class CreateDeprecatedRuleKeysTable extends DdlChange { + + private static final String DEPRECATED_RULE_KEYS = "deprecated_rule_keys"; + private static final VarcharColumnDef UUID_COLUMN = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(false) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + private static final IntegerColumnDef RULE_ID_COLUMN = IntegerColumnDef.newIntegerColumnDefBuilder() + .setColumnName("rule_id") + .setIsNullable(false) + .build(); + private static final VarcharColumnDef OLD_REPOSITORY_KEY_COLUMN = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("old_repository_key") + .setIsNullable(false) + .setLimit(255) + .build(); + private static final VarcharColumnDef OLD_RULE_KEY_COLUMN = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("old_rule_key") + .setIsNullable(false) + .setLimit(200) + .build(); + private static final BigIntegerColumnDef CREATED_AT_COLUMN = BigIntegerColumnDef.newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build(); + + private Database db; + + public CreateDeprecatedRuleKeysTable(Database db) { + super(db); + this.db = db; + } + + @Override + public void execute(Context context) throws SQLException { + if (!tableExists()) { + context.execute(new CreateTableBuilder(getDialect(), DEPRECATED_RULE_KEYS) + .addPkColumn(UUID_COLUMN) + .addColumn(RULE_ID_COLUMN) + .addColumn(OLD_REPOSITORY_KEY_COLUMN) + .addColumn(OLD_RULE_KEY_COLUMN) + .addColumn(CREATED_AT_COLUMN) + .build() + ); + + context.execute(new CreateIndexBuilder(getDialect()) + .setTable(DEPRECATED_RULE_KEYS) + .addColumn(OLD_REPOSITORY_KEY_COLUMN) + .addColumn(OLD_RULE_KEY_COLUMN) + .setUnique(true) + .setName("uniq_deprecated_rule_keys") + .build() + ); + + context.execute(new CreateIndexBuilder(getDialect()) + .setTable(DEPRECATED_RULE_KEYS) + .addColumn(RULE_ID_COLUMN) + .setUnique(true) + .setName("rule_id_deprecated_rule_keys") + .build() + ); + } + } + + private boolean tableExists() throws SQLException { + try (Connection connection = db.getDataSource().getConnection()) { + return DatabaseUtils.tableExists(DEPRECATED_RULE_KEYS, connection); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java index 3d762638d8b..552c1f73abd 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java @@ -32,6 +32,7 @@ public class DbVersion71 implements DbVersion { .add(2002, "Set rules scope to MAIN", SetRuleScopeToMain.class) .add(2003, "Make scope not nullable in rules", MakeScopeNotNullableInRules.class) .add(2004, "Use rule id in QPROFILE_CHANGES", UseRuleIdInQPChangesData.class) + .add(2005, "Create table DEPRECATED_RULE_KEYS", CreateDeprecatedRuleKeysTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest.java new file mode 100644 index 00000000000..af22dc35a4e --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v71; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateDeprecatedRuleKeysTableTest { + + private static final String TABLE = "deprecated_rule_keys"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateDeprecatedRuleKeysTableTest.class, "empty.sql"); + + private CreateDeprecatedRuleKeysTable underTest = new CreateDeprecatedRuleKeysTable(db.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + checkTable(); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + checkTable(); + } + + private void checkTable() { + db.assertColumnDefinition(TABLE, "uuid", Types.VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "rule_id", Types.INTEGER, 11, false); + db.assertColumnDefinition(TABLE, "old_repository_key", Types.VARCHAR, 255, false); + db.assertColumnDefinition(TABLE, "old_rule_key", Types.VARCHAR, 200, false); + db.assertColumnDefinition(TABLE, "created_at", Types.BIGINT, 20, false); + + db.assertUniqueIndex(TABLE, "uniq_deprecated_rule_keys", "old_repository_key", "old_rule_key"); + db.assertUniqueIndex(TABLE, "rule_id_deprecated_rule_keys", "rule_id"); + assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java index e42251e445a..0d40b7520a9 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java @@ -36,7 +36,7 @@ public class DbVersion71Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 5); + verifyMigrationCount(underTest, 6); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest/empty.sql new file mode 100644 index 00000000000..e69de29bb2d -- 2.39.5