]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9662 Create "plugins" table
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 3 Aug 2017 13:34:42 +0000 (15:34 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 7 Sep 2017 06:33:31 +0000 (08:33 +0200)
server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/AddPluginKeyToRules.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/CreateTablePlugins.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/CreateTablePluginsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/CreateTablePluginsTest/empty.sql [new file with mode: 0644]

index aa131c20e116996f1b2af7f399369ddfbe3f4f30..2b1dc43cb0a3ea5bcbd7f61dda9560a30a6aad62 100644 (file)
@@ -680,3 +680,13 @@ CREATE TABLE "ES_QUEUE" (
 );
 CREATE UNIQUE INDEX "PK_ES_QUEUE" ON "ES_QUEUE" ("UUID");
 CREATE INDEX "ES_QUEUE_CREATED_AT" ON "ES_QUEUE" ("CREATED_AT");
+
+CREATE TABLE "PLUGINS" (
+  "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
+  "KEE" VARCHAR(200) NOT NULL,
+  "BASE_PLUGIN_KEY" VARCHAR(200),
+  "HASH" VARCHAR(200) NOT NULL,
+  "CREATED_AT" BIGINT NOT NULL,
+  "UPDATED_AT" BIGINT NOT NULL
+);
+CREATE UNIQUE INDEX "PLUGINS_KEY" ON "PLUGINS" ("KEE");
index 4fa05dc192738ff802d80d8bb1c345cd40fe5e13..3443213e5bbdcbb494b49651a94a1f8d4159796f 100644 (file)
@@ -38,7 +38,7 @@ public class AddPluginKeyToRules extends DdlChange {
     context.execute(new AddColumnsBuilder(getDialect(), "rules")
       .addColumn(newVarcharColumnDefBuilder()
         .setColumnName("plugin_key")
-        .setLimit(200)
+        .setLimit(CreateTablePlugins.PLUGIN_KEY_MAX_SIZE)
         .setIsNullable(true)
         .build())
       .build());
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/CreateTablePlugins.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/CreateTablePlugins.java
new file mode 100644 (file)
index 0000000..9b28fa4
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.server.platform.db.migration.version.v66;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class CreateTablePlugins extends DdlChange {
+
+  static final int PLUGIN_KEY_MAX_SIZE = 200;
+  private static final String TABLE_NAME = "plugins";
+
+  public CreateTablePlugins(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    VarcharColumnDef keyColumn = newVarcharColumnDefBuilder()
+      .setColumnName("kee")
+      .setLimit(PLUGIN_KEY_MAX_SIZE)
+      .setIsNullable(false)
+      .build();
+    context.execute(
+      new CreateTableBuilder(getDialect(), TABLE_NAME)
+        .addPkColumn(newVarcharColumnDefBuilder().setColumnName("uuid").setLimit(UUID_SIZE).setIsNullable(false).setIgnoreOracleUnit(true).build())
+        .addColumn(keyColumn)
+        .addColumn(newVarcharColumnDefBuilder()
+          .setColumnName("base_plugin_key")
+          .setLimit(PLUGIN_KEY_MAX_SIZE)
+          .setIsNullable(true)
+          .build())
+        .addColumn(newVarcharColumnDefBuilder()
+          .setColumnName("hash")
+          .setLimit(200)
+          .setIsNullable(false)
+          .build())
+        .addColumn(newBigIntegerColumnDefBuilder()
+          .setColumnName("created_at")
+          .setIsNullable(false)
+          .build())
+        .addColumn(newBigIntegerColumnDefBuilder()
+          .setColumnName("updated_at")
+          .setIsNullable(false)
+          .build())
+        .build());
+
+    context.execute(
+      new CreateIndexBuilder(getDialect())
+        .setTable(TABLE_NAME)
+        .setName("plugins_key")
+        .addColumn(keyColumn)
+        .setUnique(true)
+        .build());
+  }
+}
index 3ac1c79b7fa9ee4e6a12e2c2ea921e67ac85dd0e..1880ebc31d63c9b98c976019e21bde78a0f2ea6e 100644 (file)
@@ -31,6 +31,7 @@ public class DbVersion66 implements DbVersion {
       .add(1801, "Create table CE task characteristics", CreateTableCeTaskCharacteristics.class)
       .add(1802, "Delete leak settings on views", DeleteLeakSettingsOnViews.class)
       .add(1803, "Fix empty USERS.EXTERNAL_IDENTITY and USERS.EXTERNAL_IDENTITY_PROVIDER", FixEmptyIdentityProviderInUsers.class)
-      .add(1804, "Add rules.plugin_key", AddPluginKeyToRules.class);
+      .add(1804, "Add rules.plugin_key", AddPluginKeyToRules.class)
+      .add(1805, "Create table plugins", CreateTablePlugins.class);
   }
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/CreateTablePluginsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/CreateTablePluginsTest.java
new file mode 100644 (file)
index 0000000..91f3f4b
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.server.platform.db.migration.version.v66;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CreateTablePluginsTest {
+
+  private static final String TABLE = "plugins";
+
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(CreateTablePluginsTest.class, "empty.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private CreateTablePlugins underTest = new CreateTablePlugins(db.database());
+
+  @Test
+  public void creates_table_on_empty_db() throws SQLException {
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0);
+
+    db.assertColumnDefinition(TABLE, "uuid", Types.VARCHAR, 40, false);
+    db.assertPrimaryKey(TABLE, "pk_plugins", "uuid");
+    db.assertColumnDefinition(TABLE, "kee", Types.VARCHAR, 200, false);
+    db.assertColumnDefinition(TABLE, "base_plugin_key", Types.VARCHAR, 200, true);
+    db.assertColumnDefinition(TABLE, "hash", Types.VARCHAR, 200, false);
+    db.assertColumnDefinition(TABLE, "created_at", Types.BIGINT, null, false);
+    db.assertColumnDefinition(TABLE, "updated_at", Types.BIGINT, null, false);
+
+    db.assertUniqueIndex(TABLE, "plugins_key", "kee");
+  }
+
+  @Test
+  public void migration_is_not_reentrant() throws SQLException {
+    underTest.execute();
+
+    expectedException.expect(IllegalStateException.class);
+
+    underTest.execute();
+  }
+
+}
index 591df1a3dd4eb6890143e927d5be4e53b9367486..bce2e551bc19b346ac8d8a66b911a1d07a84f400 100644 (file)
@@ -35,6 +35,6 @@ public class DbVersion66Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 5);
+    verifyMigrationCount(underTest, 6);
   }
 }
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/CreateTablePluginsTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/CreateTablePluginsTest/empty.sql
new file mode 100644 (file)
index 0000000..e69de29