]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8025 rewrite create table PROPERTIES2 in java
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 13 Sep 2016 13:19:12 +0000 (15:19 +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/1312_create_table_properties_2.rb
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.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/CreateTableProperties2Test.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableProperties2Test/empty.sql [new file with mode: 0644]

index dc8456c9924cf4a4fff44e3bcbf94e2dbb5fbd44..5c5513bafcd3f53bf27b8c4c7341502c0108e1ac 100644 (file)
 class CreateTableProperties2 < ActiveRecord::Migration
 
   def self.up
-    create_table 'properties2' do |t|
-      t.column 'prop_key', :string, :limit => 512, :null => false
-      t.column :resource_id, :integer, :null => true
-      t.column :user_id, :integer, :null => true
-      t.column 'is_empty', :boolean, :null => false
-           t.column :text_value, :string, :limit => 4000, :null => true
-           t.column :clob_value, :text, :null => true
-      t.column 'created_at', :big_integer, :null => false
-    end
+    execute_java_migration('org.sonar.db.version.v61.CreateTableProperties2')
     add_varchar_index :properties2, :prop_key, :name => 'properties2_key'
   end
 end
index 744b4389c2e857a4d253a19489d7e3bb414337c0..97126ec1658b7db45cd75fb6b8743d4c148e7fab 100644 (file)
@@ -149,6 +149,7 @@ import org.sonar.db.version.v61.AddErrorColumnsToCeActivity;
 import org.sonar.db.version.v61.CopyActivitiesToQprofileChanges;
 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.CreateTableScannerContext;
 import org.sonar.db.version.v61.DeleteProjectDashboards;
 import org.sonar.db.version.v61.DeleteReportsFromCeQueue;
@@ -328,6 +329,7 @@ public class MigrationStepModule extends Module {
       AddErrorColumnsToCeActivity.class,
       CreateTableScannerContext.class,
       CreateTableInternalProperties.class,
+      CreateTableProperties2.class,
       PopulateTableProperties2.class,
       RemoveViewsDefinitionFromProperties.class,
       CopyActivitiesToQprofileChanges.class);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableProperties2.java
new file mode 100644 (file)
index 0000000..bd8a690
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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.BooleanColumnDef.newBooleanColumnDefBuilder;
+import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder;
+import static org.sonar.db.version.CreateTableBuilder.ColumnFlag.AUTO_INCREMENT;
+import static org.sonar.db.version.IntegerColumnDef.newIntegerColumnDefBuilder;
+import static org.sonar.db.version.VarcharColumnDef.MAX_SIZE;
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateTableProperties2 extends DdlChange {
+  public CreateTableProperties2(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    List<String> stmts = new CreateTableBuilder(getDialect(), "properties2")
+        .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT)
+        .addColumn(newVarcharColumnDefBuilder().setColumnName("prop_key").setLimit(512).setIsNullable(false).build())
+        .addColumn(newBigIntegerColumnDefBuilder().setColumnName("resource_id").setIsNullable(true).build())
+        .addColumn(newBigIntegerColumnDefBuilder().setColumnName("user_id").setIsNullable(true).build())
+        .addColumn(newBooleanColumnDefBuilder().setColumnName("is_empty").setIsNullable(false).build())
+        .addColumn(newVarcharColumnDefBuilder().setColumnName("text_value").setLimit(MAX_SIZE).setIsNullable(true).build())
+        .addColumn(newClobColumnDefBuilder().setColumnName("clob_value").setIsNullable(true).build())
+        .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build())
+        // table with be renamed to properties in following migration, use final constraint name right away
+        .withPkConstraintName("pk_properties")
+        .build();
+    context.execute(stmts);
+  }
+}
index 657cd62dc11fc457b1ce0ddd1fb116924c9e2272..e8c5df62134a4e536abbac66a49fb806ea06f1ea 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(138);
+    assertThat(container.size()).isEqualTo(139);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableProperties2Test.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableProperties2Test.java
new file mode 100644 (file)
index 0000000..223b84f
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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 CreateTableProperties2Test {
+  private static final String TABLE_PROPERTIES_2 = "properties2";
+
+  @Rule
+  public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreateTableProperties2Test.class, "empty.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private CreateTableProperties2 underTest = new CreateTableProperties2(dbTester.database());
+
+  @Test
+  public void creates_table_on_empty_db() throws SQLException {
+    underTest.execute();
+
+    assertThat(dbTester.countRowsOfTable(TABLE_PROPERTIES_2)).isEqualTo(0);
+
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "id", Types.INTEGER, null, false);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "prop_key", Types.VARCHAR, 512, false);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "resource_id", Types.BIGINT, null, true);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "user_id", Types.BIGINT, null, true);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "is_empty", Types.BOOLEAN, null, false);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "text_value", Types.VARCHAR, 4000, true);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "clob_value", Types.CLOB, null, true);
+    dbTester.assertColumnDefinition(TABLE_PROPERTIES_2, "created_at", Types.BIGINT, null, false);
+    dbTester.assertPrimaryKey(TABLE_PROPERTIES_2, "pk_properties", "id");
+  }
+
+  @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/CreateTableProperties2Test/empty.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableProperties2Test/empty.sql
new file mode 100644 (file)
index 0000000..e69de29