Browse Source

SONAR-10311 Add migration for DEPRECATED_RULE_KEYS table

tags/7.5
Eric Hartmann 6 years ago
parent
commit
a201df8720

+ 9
- 0
server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl View File

@@ -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),

+ 103
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTable.java View File

@@ -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);
}
}
}

+ 1
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java View File

@@ -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)
;
}
}

+ 66
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest.java View File

@@ -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);
}
}

+ 1
- 1
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java View File

@@ -36,7 +36,7 @@ public class DbVersion71Test {

@Test
public void verify_migration_count() {
verifyMigrationCount(underTest, 5);
verifyMigrationCount(underTest, 6);
}

}

+ 0
- 0
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/CreateDeprecatedRuleKeysTableTest/empty.sql View File


Loading…
Cancel
Save