aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-09-15 12:21:09 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-09-16 12:22:12 +0200
commitdf422565d7e3a3718e1c321ec22c68260a72d799 (patch)
tree96c7f46e9a26b46b5a313d8eaf8572f5371d1c6c
parent82599634f9cfe4e96b4d1b7781d642440432ac78 (diff)
downloadsonarqube-df422565d7e3a3718e1c321ec22c68260a72d799.tar.gz
sonarqube-df422565d7e3a3718e1c321ec22c68260a72d799.zip
SONAR-8025 rewrite create table RULE_REPOSITORIES in java
-rw-r--r--server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1319_create_table_rule_repositories.rb8
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableRuleRepositories.java46
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java2
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest.java63
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest/empty.sql0
6 files changed, 114 insertions, 9 deletions
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<String> 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
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest/empty.sql