diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-03-21 10:58:22 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-03-23 17:54:56 +0100 |
commit | f48fcf82e6b0f0175cfa3c61b2905fe8d6913929 (patch) | |
tree | e31f994ff246157c90ec33348d9ceecc2bab7d9a | |
parent | b49e3ab7fda9d8a18f984a30b19b5382042f6436 (diff) | |
download | sonarqube-f48fcf82e6b0f0175cfa3c61b2905fe8d6913929.tar.gz sonarqube-f48fcf82e6b0f0175cfa3c61b2905fe8d6913929.zip |
SONAR-8867 create table RULES_METADATA
7 files changed, 190 insertions, 7 deletions
diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/rows-h2.sql b/server/sonar-db-core/src/main/resources/org/sonar/db/version/rows-h2.sql index 300a33d2194..6ab5e9034ca 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -547,6 +547,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1611'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1612'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1613'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1614'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1615'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', false, '1418215735482', '1418215735482'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; 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 96468bb9352..d2a2e5c4dca 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 @@ -157,6 +157,21 @@ CREATE TABLE "RULES" ( ); CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES" ("PLUGIN_NAME", "PLUGIN_RULE_KEY"); +CREATE TABLE "RULES_METADATA" ( + "RULE_ID" INTEGER NOT NULL, + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "NOTE_DATA" CLOB(2147483647), + "NOTE_USER_LOGIN" VARCHAR(255), + "NOTE_CREATED_AT" BIGINT, + "NOTE_UPDATED_AT" BIGINT, + "REMEDIATION_FUNCTION" VARCHAR(20), + "REMEDIATION_GAP_MULT" VARCHAR(20), + "REMEDIATION_BASE_EFFORT" VARCHAR(20), + "TAGS" VARCHAR(4000), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + CONSTRAINT PK_RULES_METADATA PRIMARY KEY (RULE_ID,ORGANIZATION_UUID) +); CREATE TABLE "EVENTS" ( "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/v64/CreateRulesMetadata.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadata.java new file mode 100644 index 00000000000..545a31f9011 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadata.java @@ -0,0 +1,101 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v64; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.ClobColumnDef; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; +import static org.sonar.server.platform.db.migration.def.IntegerColumnDef.newIntegerColumnDefBuilder; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateRulesMetadata extends DdlChange { + + private static final String TABLE_NAME = "rules_metadata"; + + public CreateRulesMetadata(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME) + .addPkColumn(newIntegerColumnDefBuilder() + .setColumnName("rule_id") + .setIsNullable(false) + .build()) + .addPkColumn(newVarcharColumnDefBuilder() + .setColumnName("organization_uuid") + .setLimit(VarcharColumnDef.UUID_SIZE) + .setIsNullable(false) + .build()) + .addColumn(ClobColumnDef.newClobColumnDefBuilder() + .setColumnName("note_data") + .setIsNullable(true) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("note_user_login") + .setLimit(255) + .setIsNullable(true) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("note_created_at") + .setIsNullable(true) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("note_updated_at") + .setIsNullable(true) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("remediation_function") + .setLimit(20) + .setIsNullable(true) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("remediation_gap_mult") + .setLimit(20) + .setIsNullable(true) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("remediation_base_effort") + .setLimit(20) + .setIsNullable(true) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("tags") + .setLimit(4000) + .setIsNullable(true) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("updated_at") + .setIsNullable(false) + .build()) + .withPkConstraintName("pk_" + TABLE_NAME) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64.java index 6e395c4d7a4..80f5e5d1555 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64.java @@ -24,7 +24,6 @@ import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; import org.sonar.server.platform.db.migration.version.DbVersion; public class DbVersion64 implements DbVersion { - @Override public void addSteps(MigrationStepRegistry registry) { registry @@ -33,19 +32,16 @@ public class DbVersion64 implements DbVersion { .add(1602, "Add RULES_PROFILES.ORGANIZATION_UUID", AddQualityProfileOrganizationUuid.class) .add(1603, "Set RULES_PROFILES.ORGANIZATION_UUID to default", SetQualityProfileOrganizationUuidToDefault.class) .add(1604, "Make RULES_PROFILES.ORGANIZATION_UUID not nullable", MakeQualityProfileOrganizationUuidNotNullable.class) - .add(1605, "Drop unique index on RULES_PROFILES.KEE", DropUniqueIndexOnQualityProfileKey.class) .add(1606, "Make RULES_PROFILES.ORGANIZATION_UUID and KEE unique", MakeQualityProfileOrganizationUuidAndKeyUnique.class) - .add(1607, "Create ORGANIZATION_MEMBERS table", CreateOrganizationMembersTable.class) .add(1608, "Populate ORGANIZATION_MEMBERS table", PopulateOrganizationMembersTable.class) - .add(1609, "Drop unique index on RULES_PROFILES.ORGANIZATION_UUID and KEE", DropUniqueIndexOnQualityProfileOrganizationUuidAndKey.class) .add(1610, "Make RULES_PROFILES.KEE unique", MakeQualityProfileKeyUnique.class) - .add(1611, "Clean LOADED_TEMPLATES rows without type", CleanLoadedTemplateOrphans.class) .add(1612, "Extend size of column LOADED_TEMPLATES.TEMPLATE_TYPE", ExtendLoadedTemplateTypeColumn.class) .add(1613, "Add index LOADED_TEMPLATES_TYPE", AddIndexLoadedTemplatesType.class) - .add(1614, "Upgrade loaded template entries for quality profiles", UpgradeQualityTemplateLoadedTemplates.class); + .add(1614, "Upgrade loaded template entries for quality profiles", UpgradeQualityTemplateLoadedTemplates.class) + .add(1615, "Create table RULES_METADATA", CreateRulesMetadata.class); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadataTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadataTest.java new file mode 100644 index 00000000000..6833454000b --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadataTest.java @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v64; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateRulesMetadataTest { + private static final String TABLE_RULES_METADATA = "rules_metadata"; + + @Rule + public final CoreDbTester dbTester = CoreDbTester.createForSchema(CreateRulesMetadataTest.class, "empty.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private CreateRulesMetadata underTest = new CreateRulesMetadata(dbTester.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + assertThat(dbTester.countRowsOfTable(TABLE_RULES_METADATA)).isEqualTo(0); + + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "rule_id", Types.INTEGER, null, false); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "organization_uuid", Types.VARCHAR, 40, false); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "note_data", Types.CLOB, null, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "note_user_login", Types.VARCHAR, 255, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "note_created_at", Types.BIGINT, null, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "note_updated_at", Types.BIGINT, null, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "remediation_function", Types.VARCHAR, 20, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "remediation_gap_mult", Types.VARCHAR, 20, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "remediation_base_effort", Types.VARCHAR, 20, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "tags", Types.VARCHAR, 4000, true); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "created_at", Types.BIGINT, null, false); + dbTester.assertColumnDefinition(TABLE_RULES_METADATA, "updated_at", Types.BIGINT, null, false); + dbTester.assertPrimaryKey(TABLE_RULES_METADATA, "pk_rules_metadata", "rule_id", "organization_uuid"); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64Test.java index 8912d8fa023..2b68829c2a0 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64Test.java @@ -35,7 +35,7 @@ public class DbVersion64Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 15); + verifyMigrationCount(underTest, 16); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadataTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/CreateRulesMetadataTest/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/v64/CreateRulesMetadataTest/empty.sql |