diff options
author | Eric Hartmann <hartmann.eric@gmail.com> | 2018-01-29 18:03:46 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-02-08 13:41:00 +0100 |
commit | a201df8720aab627050b9f14f0b35c75780e3a92 (patch) | |
tree | fb0afd216136adad2c9a465f3fcc5c85769af7f8 /server/sonar-db-migration | |
parent | 697cdc1de821864d77a507bdb3834f1239066c57 (diff) | |
download | sonarqube-a201df8720aab627050b9f14f0b35c75780e3a92.tar.gz sonarqube-a201df8720aab627050b9f14f0b35c75780e3a92.zip |
SONAR-10311 Add migration for DEPRECATED_RULE_KEYS table
Diffstat (limited to 'server/sonar-db-migration')
5 files changed, 171 insertions, 1 deletions
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 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest/empty.sql |