]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8025 rewrite create table CE_SCANNER_CONTEXT in java
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 13 Sep 2016 12:35:24 +0000 (14:35 +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/1309_recreate_table_scanner_context.rb
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableScannerContext.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/CreateTableScannerContextTest.java [new file with mode: 0644]

index c63899bce40cefce59ef652c72898732ae0ef539..7982118d4ccc5e7dcc21369ec4ad17100b884461 100644 (file)
 class RecreateTableScannerContext < ActiveRecord::Migration
 
   def self.up
-    create_table 'ce_scanner_context', :id => false do |t|
-      t.column 'task_uuid', :string, :limit => 40, :null => false
-      t.column 'data', :binary, :null => false
-      t.column 'created_at', :big_integer, :null => false
-      t.column 'updated_at', :big_integer, :null => false
-    end
-    add_primary_key 'ce_scanner_context', 'task_uuid'
+    execute_java_migration('org.sonar.db.version.v61.CreateTableScannerContext')
   end
 end
index 85bf57f64c14cefdbf5f3b22623d51c934e2bcb9..ddc6bb7a5b799b02ab389c8b8c81753ca488841f 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.CreateTableScannerContext;
 import org.sonar.db.version.v61.DeleteProjectDashboards;
 import org.sonar.db.version.v61.DeleteReportsFromCeQueue;
 import org.sonar.db.version.v61.DropIsGlobalFromDashboards;
@@ -324,6 +325,7 @@ public class MigrationStepModule extends Module {
       ShrinkModuleUuidPathOfProjects.class,
       AddBUuidPathToProjects.class,
       AddErrorColumnsToCeActivity.class,
+      CreateTableScannerContext.class,
       PopulateTableProperties2.class,
       RemoveViewsDefinitionFromProperties.class,
       CopyActivitiesToQprofileChanges.class);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableScannerContext.java b/sonar-db/src/main/java/org/sonar/db/version/v61/CreateTableScannerContext.java
new file mode 100644 (file)
index 0000000..6d20e82
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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.BlobColumnDef.newBlobColumnDefBuilder;
+import static org.sonar.db.version.VarcharColumnDef.UUID_SIZE;
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateTableScannerContext extends DdlChange {
+  public CreateTableScannerContext(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(
+      new CreateTableBuilder(getDialect(), "ce_scanner_context")
+        .addPkColumn(newVarcharColumnDefBuilder().setColumnName("task_uuid").setLimit(UUID_SIZE).setIsNullable(false).build())
+        .addColumn(newBlobColumnDefBuilder().setColumnName("data").setIsNullable(false).build())
+        .addColumn(newBigIntegerColumnDefBuilder().setColumnName("created_at").setIsNullable(false).build())
+        .addColumn(newBigIntegerColumnDefBuilder().setColumnName("updated_at").setIsNullable(false).build())
+        .build());
+  }
+}
index e03d3eafbfe4d487b5ca921210c531dac8ffd837..2ee78b6f086ec3b4ea22582758e3d4be9070ff2b 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(136);
+    assertThat(container.size()).isEqualTo(137);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableScannerContextTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CreateTableScannerContextTest.java
new file mode 100644 (file)
index 0000000..97780fd
--- /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 CreateTableScannerContextTest {
+  private static final String TABLE_CE_SCANNER_CONTEXT = "ce_scanner_context";
+
+  @Rule
+  public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, CreateTableCeTaskInputTest.class, "empty.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private CreateTableScannerContext underTest = new CreateTableScannerContext(dbTester.database());
+
+  @Test
+  public void creates_table_on_empty_db() throws SQLException {
+    underTest.execute();
+
+    assertThat(dbTester.countRowsOfTable(TABLE_CE_SCANNER_CONTEXT)).isEqualTo(0);
+
+    dbTester.assertColumnDefinition(TABLE_CE_SCANNER_CONTEXT, "task_uuid", Types.VARCHAR, 40, false);
+    dbTester.assertColumnDefinition(TABLE_CE_SCANNER_CONTEXT, "data", Types.BLOB, null, false);
+    dbTester.assertColumnDefinition(TABLE_CE_SCANNER_CONTEXT, "created_at", Types.BIGINT, null, false);
+    dbTester.assertColumnDefinition(TABLE_CE_SCANNER_CONTEXT, "updated_at", Types.BIGINT, null, false);
+    dbTester.assertPrimaryKey(TABLE_CE_SCANNER_CONTEXT, "pk_" + TABLE_CE_SCANNER_CONTEXT, "task_uuid");
+  }
+
+  @Test
+  public void migration_is_not_reentrant() throws SQLException {
+    underTest.execute();
+
+    expectedException.expect(IllegalStateException.class);
+
+    underTest.execute();
+  }
+}