]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8025 rewrite create table RULE_REPOSITORIES in java 1250/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 15 Sep 2016 10:21:09 +0000 (12:21 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 16 Sep 2016 10:22:12 +0000 (12:22 +0200)
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1319_create_table_rule_repositories.rb
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableRuleRepositories.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableRuleRepositoriesTest/empty.sql [new file with mode: 0644]

index 66b86b6182f935ec2d7a077c80df3f41044dccfe..c99e90a50a1e8762adeb019f7cf27e34b87ae0d2 100644 (file)
 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
index 967f3b6ae0d338834eaa0c6712bef657e9fa0d2f..d11f823784cff416f2485d38c81ac84c264f8fe3 100644 (file)
@@ -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 (file)
index 0000000..d460bfd
--- /dev/null
@@ -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);
+  }
+}
index 37ac79164256ed7cbabccf8c7d5b52be58daab17..9cc6a947cb89a4c4c219ee1ea2d380bef3054cac 100644 (file)
@@ -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 (file)
index 0000000..f3cb630
--- /dev/null
@@ -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 (file)
index 0000000..e69de29