From: Sébastien Lesaint Date: Thu, 15 Sep 2016 10:21:09 +0000 (+0200) Subject: SONAR-8025 rewrite create table RULE_REPOSITORIES in java X-Git-Tag: 6.1-RC1~32 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F1250%2Fhead;p=sonarqube.git SONAR-8025 rewrite create table RULE_REPOSITORIES in java --- diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1319_create_table_rule_repositories.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1319_create_table_rule_repositories.rb index 66b86b6182f..c99e90a50a1 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1319_create_table_rule_repositories.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1319_create_table_rule_repositories.rb @@ -24,12 +24,6 @@ class CreateTableRuleRepositories < ActiveRecord::Migration def self.up - create_table 'rule_repositories', :id => false do |t| - t.column 'kee', :string, :limit => 200, :null => false - t.column 'language', :string, :limit => 20, :null => false - t.column 'name', :string, :limit => 4000, :null => false - t.column 'created_at', :big_integer, :null => false - end - add_primary_key 'rule_repositories', 'kee' + execute_java_migration('org.sonar.db.version.v61.CreateTableRuleRepositories') 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 967f3b6ae0d..d11f823784c 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 @@ -151,6 +151,7 @@ import org.sonar.db.version.v61.CreateTableCeTaskInput; import org.sonar.db.version.v61.CreateTableInternalProperties; import org.sonar.db.version.v61.CreateTableProperties2; import org.sonar.db.version.v61.CreateTableQprofileChanges; +import org.sonar.db.version.v61.CreateTableRuleRepositories; import org.sonar.db.version.v61.CreateTableScannerContext; import org.sonar.db.version.v61.DeleteProjectDashboards; import org.sonar.db.version.v61.DeleteReportsFromCeQueue; @@ -334,6 +335,7 @@ public class MigrationStepModule extends Module { PopulateTableProperties2.class, RemoveViewsDefinitionFromProperties.class, CreateTableQprofileChanges.class, - CopyActivitiesToQprofileChanges.class); + CopyActivitiesToQprofileChanges.class, + CreateTableRuleRepositories.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableRuleRepositories.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableRuleRepositories.java new file mode 100644 index 00000000000..d460bfd72d6 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableRuleRepositories.java @@ -0,0 +1,46 @@ +/* + * 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.v61; + +import java.sql.SQLException; +import java.util.List; +import org.sonar.db.Database; +import org.sonar.db.version.CreateTableBuilder; +import org.sonar.db.version.DdlChange; + +import static org.sonar.db.version.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateTableRuleRepositories extends DdlChange { + public CreateTableRuleRepositories(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + List stmts = new CreateTableBuilder(getDialect(), "rule_repositories") + .addPkColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(200).setIsNullable(false).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName("language").setLimit(20).setIsNullable(false).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName("name").setLimit(4000).setIsNullable(false).build()) + .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build()) + .build(); + context.execute(stmts); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index 37ac7916425..9cc6a947cb8 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -29,6 +29,6 @@ public class MigrationStepModuleTest { public void verify_count_of_added_MigrationStep_types() { ComponentContainer container = new ComponentContainer(); new MigrationStepModule().configure(container); - assertThat(container.size()).isEqualTo(140); + assertThat(container.size()).isEqualTo(141); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest.java new file mode 100644 index 00000000000..f3cb6303bd1 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest.java @@ -0,0 +1,63 @@ +/* + * 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.v61; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateTableRuleRepositoriesTest { + private static final String TABLE_RULE_REPOSITORIES = "rule_repositories"; + + @Rule + public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreateTableRuleRepositoriesTest.class, "empty.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private CreateTableRuleRepositories underTest = new CreateTableRuleRepositories(dbTester.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + assertThat(dbTester.countRowsOfTable(TABLE_RULE_REPOSITORIES)).isEqualTo(0); + + dbTester.assertColumnDefinition(TABLE_RULE_REPOSITORIES, "kee", Types.VARCHAR, 200, false); + dbTester.assertColumnDefinition(TABLE_RULE_REPOSITORIES, "language", Types.VARCHAR, 20, false); + dbTester.assertColumnDefinition(TABLE_RULE_REPOSITORIES, "name", Types.VARCHAR, 4000, false); + dbTester.assertColumnDefinition(TABLE_RULE_REPOSITORIES, "created_at", Types.BIGINT, null, false); + dbTester.assertPrimaryKey(TABLE_RULE_REPOSITORIES, "pk_" + TABLE_RULE_REPOSITORIES, "kee"); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest/empty.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest/empty.sql new file mode 100644 index 00000000000..e69de29bb2d