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

index 55f8870ac3f87cc26eadeed2e3d8b5f24ac790ef..643f006f679ee5d8cb868e5fb0f198e54362a6b9 100644 (file)
 class CreateInternalPropertiesTable < ActiveRecord::Migration
 
   def self.up
-    create_table 'internal_properties', :id => false do |t|
-      t.column 'kee', :string, :limit => 20, :null => false
-      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
-    add_primary_key 'internal_properties', 'kee'
+    execute_java_migration('org.sonar.db.version.v61.CreateTableInternalProperties')
   end
 
 end
index ddc6bb7a5b799b02ab389c8b8c81753ca488841f..744b4389c2e857a4d253a19489d7e3bb414337c0 100644 (file)
@@ -148,6 +148,7 @@ import org.sonar.db.version.v61.AddBUuidPathToProjects;
 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.CreateTableScannerContext;
 import org.sonar.db.version.v61.DeleteProjectDashboards;
 import org.sonar.db.version.v61.DeleteReportsFromCeQueue;
@@ -326,6 +327,7 @@ public class MigrationStepModule extends Module {
       AddBUuidPathToProjects.class,
       AddErrorColumnsToCeActivity.class,
       CreateTableScannerContext.class,
+      CreateTableInternalProperties.class,
       PopulateTableProperties2.class,
       RemoveViewsDefinitionFromProperties.class,
       CopyActivitiesToQprofileChanges.class);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableInternalProperties.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableInternalProperties.java
new file mode 100644 (file)
index 0000000..8bc0fcd
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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 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.VarcharColumnDef.MAX_SIZE;
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateTableInternalProperties extends DdlChange {
+  public CreateTableInternalProperties(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(
+      new CreateTableBuilder(getDialect(), "internal_properties")
+        .addPkColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(20).setIsNullable(false).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())
+        .build());
+  }
+}
index 2ee78b6f086ec3b4ea22582758e3d4be9070ff2b..657cd62dc11fc457b1ce0ddd1fb116924c9e2272 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(137);
+    assertThat(container.size()).isEqualTo(138);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableInternalPropertiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableInternalPropertiesTest.java
new file mode 100644 (file)
index 0000000..768144a
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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 CreateTableInternalPropertiesTest {
+  private static final String TABLE_INTERNAL_PROPERTIES = "internal_properties";
+
+  @Rule
+  public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreateTableInternalPropertiesTest.class, "empty.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private CreateTableInternalProperties underTest = new CreateTableInternalProperties(dbTester.database());
+
+  @Test
+  public void creates_table_on_empty_db() throws SQLException {
+    underTest.execute();
+
+    assertThat(dbTester.countRowsOfTable(TABLE_INTERNAL_PROPERTIES)).isEqualTo(0);
+
+    dbTester.assertColumnDefinition(TABLE_INTERNAL_PROPERTIES, "kee", Types.VARCHAR, 20, false);
+    dbTester.assertColumnDefinition(TABLE_INTERNAL_PROPERTIES, "is_empty", Types.BOOLEAN, null, false);
+    dbTester.assertColumnDefinition(TABLE_INTERNAL_PROPERTIES, "text_value", Types.VARCHAR, 4000, true);
+    dbTester.assertColumnDefinition(TABLE_INTERNAL_PROPERTIES, "clob_value", Types.CLOB, null, true);
+    dbTester.assertColumnDefinition(TABLE_INTERNAL_PROPERTIES, "created_at", Types.BIGINT, null, false);
+    dbTester.assertPrimaryKey(TABLE_INTERNAL_PROPERTIES, "pk_" + TABLE_INTERNAL_PROPERTIES, "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/CreateTableInternalPropertiesTest/empty.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CreateTableInternalPropertiesTest/empty.sql
new file mode 100644 (file)
index 0000000..e69de29