]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22871 Create measures table
authorEric Giffon <eric.giffon@sonarsource.com>
Tue, 27 Aug 2024 09:15:55 +0000 (11:15 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 9 Oct 2024 20:02:45 +0000 (20:02 +0000)
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTableIT.java [new file with mode: 0644]
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTableIT.java [new file with mode: 0644]
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTableIT.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java

index ea869a1910b420c43f4f7824938376c7a6df46ed..f19c8ed7a706d9ae8942400c7a01cdb930994774 100644 (file)
@@ -532,6 +532,17 @@ ALTER TABLE "LIVE_MEASURES" ADD CONSTRAINT "PK_LIVE_MEASURES" PRIMARY KEY("UUID"
 CREATE INDEX "LIVE_MEASURES_PROJECT" ON "LIVE_MEASURES"("PROJECT_UUID" NULLS FIRST);
 CREATE UNIQUE NULLS NOT DISTINCT INDEX "LIVE_MEASURES_COMPONENT" ON "LIVE_MEASURES"("COMPONENT_UUID" NULLS FIRST, "METRIC_UUID" NULLS FIRST);
 
+CREATE TABLE "MEASURES"(
+    "COMPONENT_UUID" CHARACTER VARYING(40) NOT NULL,
+    "BRANCH_UUID" CHARACTER VARYING(40) NOT NULL,
+    "JSON_VALUE" CHARACTER LARGE OBJECT NOT NULL,
+    "JSON_VALUE_HASH" BIGINT NOT NULL,
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "MEASURES" ADD CONSTRAINT "PK_MEASURES" PRIMARY KEY("COMPONENT_UUID");
+CREATE INDEX "MEASURES_BRANCH_UUID" ON "MEASURES"("BRANCH_UUID" NULLS FIRST);
+
 CREATE TABLE "METRICS"(
     "UUID" CHARACTER VARYING(40) NOT NULL,
     "NAME" CHARACTER VARYING(64) NOT NULL,
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTableIT.java
new file mode 100644 (file)
index 0000000..e6f0b01
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v107;
+
+import java.sql.SQLException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.db.MigrationDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.version.v107.CreateIndexOnMeasuresTable.INDEX_NAME;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_BRANCH_UUID;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.MEASURES_TABLE_NAME;
+
+
+class CreateIndexOnMeasuresTableIT {
+
+  @RegisterExtension
+  public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreateIndexOnMeasuresTable.class);
+
+  private final DdlChange underTest = new CreateIndexOnMeasuresTable(db.database());
+
+  @Test
+  void migration_should_create_index() throws SQLException {
+    db.assertIndexDoesNotExist(MEASURES_TABLE_NAME, INDEX_NAME);
+
+    underTest.execute();
+
+    db.assertIndex(MEASURES_TABLE_NAME, INDEX_NAME, COLUMN_BRANCH_UUID);
+  }
+
+  @Test
+  void migration_should_be_reentrant() throws SQLException {
+    db.assertIndexDoesNotExist(MEASURES_TABLE_NAME, INDEX_NAME);
+
+    underTest.execute();
+    underTest.execute();
+
+    db.assertIndex(MEASURES_TABLE_NAME, INDEX_NAME, COLUMN_BRANCH_UUID);
+  }
+}
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTableIT.java
new file mode 100644 (file)
index 0000000..30203ef
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v107;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.db.MigrationDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_BRANCH_UUID;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_COMPONENT_UUID;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_CREATED_AT;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_JSON_VALUE;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_JSON_VALUE_HASH;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_UPDATED_AT;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.MEASURES_TABLE_NAME;
+
+class CreateMeasuresTableIT {
+
+  @RegisterExtension
+  public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreateMeasuresTable.class);
+
+  private final DdlChange underTest = new CreateMeasuresTable(db.database());
+
+  @Test
+  void execute_shouldCreateTable() throws SQLException {
+    db.assertTableDoesNotExist(MEASURES_TABLE_NAME);
+
+    underTest.execute();
+
+    db.assertTableExists(MEASURES_TABLE_NAME);
+    db.assertNoPrimaryKey(MEASURES_TABLE_NAME);
+    db.assertColumnDefinition(MEASURES_TABLE_NAME, COLUMN_COMPONENT_UUID, Types.VARCHAR, UUID_SIZE, false);
+    db.assertColumnDefinition(MEASURES_TABLE_NAME, COLUMN_BRANCH_UUID, Types.VARCHAR, UUID_SIZE, false);
+    db.assertColumnDefinition(MEASURES_TABLE_NAME, COLUMN_JSON_VALUE, Types.CLOB, null, false);
+    db.assertColumnDefinition(MEASURES_TABLE_NAME, COLUMN_JSON_VALUE_HASH, Types.BIGINT, null, false);
+    db.assertColumnDefinition(MEASURES_TABLE_NAME, COLUMN_CREATED_AT, Types.BIGINT, null, false);
+    db.assertColumnDefinition(MEASURES_TABLE_NAME, COLUMN_UPDATED_AT, Types.BIGINT, null, false);
+  }
+
+  @Test
+  void execute_shouldBeReentrant() throws SQLException {
+    db.assertTableDoesNotExist(MEASURES_TABLE_NAME);
+
+    underTest.execute();
+    underTest.execute();
+
+    db.assertTableExists(MEASURES_TABLE_NAME);
+  }
+}
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTableIT.java
new file mode 100644 (file)
index 0000000..56bdcc0
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v107;
+
+import java.sql.SQLException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.db.MigrationDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_COMPONENT_UUID;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.MEASURES_TABLE_NAME;
+
+class CreatePrimaryKeyOnMeasuresTableIT {
+
+  private static final String PK_NAME = "pk_measures";
+
+  @RegisterExtension
+  public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreatePrimaryKeyOnMeasuresTable.class);
+
+  private final DdlChange underTest = new CreatePrimaryKeyOnMeasuresTable(db.database());
+
+  @Test
+  void execute_shouldCreatePrimaryKey() throws SQLException {
+    db.assertNoPrimaryKey(MEASURES_TABLE_NAME);
+
+    underTest.execute();
+
+    db.assertPrimaryKey(MEASURES_TABLE_NAME, PK_NAME, COLUMN_COMPONENT_UUID);
+  }
+
+  @Test
+  void execute_shouldBeReentrant() throws SQLException {
+    db.assertNoPrimaryKey(MEASURES_TABLE_NAME);
+
+    underTest.execute();
+    underTest.execute();
+
+    db.assertPrimaryKey(MEASURES_TABLE_NAME, PK_NAME, COLUMN_COMPONENT_UUID);
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTable.java
new file mode 100644 (file)
index 0000000..3eb07ce
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v107;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_BRANCH_UUID;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.MEASURES_TABLE_NAME;
+
+public class CreateIndexOnMeasuresTable extends DdlChange {
+
+  static final String INDEX_NAME = "measures_branch_uuid";
+
+  public CreateIndexOnMeasuresTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    try (Connection connection = getDatabase().getDataSource().getConnection()) {
+      createUniqueIndex(context, connection);
+    }
+  }
+
+  private void createUniqueIndex(Context context, Connection connection) {
+    if (!DatabaseUtils.indexExistsIgnoreCase(MEASURES_TABLE_NAME, INDEX_NAME, connection)) {
+      context.execute(new CreateIndexBuilder(getDialect())
+        .setTable(MEASURES_TABLE_NAME)
+        .setName(INDEX_NAME)
+        .addColumn(COLUMN_BRANCH_UUID, false)
+        .setUnique(false)
+        .build());
+    }
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTable.java
new file mode 100644 (file)
index 0000000..8a7a362
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v107;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.CreateTableChange;
+
+import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+import static org.sonar.server.platform.db.migration.def.ClobColumnDef.newClobColumnDefBuilder;
+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 CreateMeasuresTable extends CreateTableChange {
+
+  static final String MEASURES_TABLE_NAME = "measures";
+  static final String COLUMN_COMPONENT_UUID = "component_uuid";
+  static final String COLUMN_BRANCH_UUID = "branch_uuid";
+  static final String COLUMN_JSON_VALUE = "json_value";
+  static final String COLUMN_JSON_VALUE_HASH = "json_value_hash";
+  static final String COLUMN_CREATED_AT = "created_at";
+  static final String COLUMN_UPDATED_AT = "updated_at";
+
+  protected CreateMeasuresTable(Database db) {
+    super(db, MEASURES_TABLE_NAME);
+  }
+
+  @Override
+  public void execute(Context context, String tableName) throws SQLException {
+    context.execute(new CreateTableBuilder(getDialect(), tableName)
+      .addColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_COMPONENT_UUID).setIsNullable(false).setLimit(UUID_SIZE).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_BRANCH_UUID).setIsNullable(false).setLimit(UUID_SIZE).build())
+      .addColumn(newClobColumnDefBuilder().setColumnName(COLUMN_JSON_VALUE).setIsNullable(false).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_JSON_VALUE_HASH).setIsNullable(false).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_CREATED_AT).setIsNullable(false).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_UPDATED_AT).setIsNullable(false).build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTable.java
new file mode 100644 (file)
index 0000000..7fa8979
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v107;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AddPrimaryKeyBuilder;
+import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.COLUMN_COMPONENT_UUID;
+import static org.sonar.server.platform.db.migration.version.v107.CreateMeasuresTable.MEASURES_TABLE_NAME;
+
+public class CreatePrimaryKeyOnMeasuresTable extends DdlChange {
+
+  public CreatePrimaryKeyOnMeasuresTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    createPrimaryKey(context);
+  }
+
+  private void createPrimaryKey(Context context) throws SQLException {
+    boolean pkExists = new DbPrimaryKeyConstraintFinder(getDatabase()).findConstraintName(MEASURES_TABLE_NAME).isPresent();
+    if (!pkExists) {
+      context.execute(new AddPrimaryKeyBuilder(MEASURES_TABLE_NAME, COLUMN_COMPONENT_UUID).build());
+    }
+  }
+}
index 6735d32f0ee4b29d7b5562d75dbb3bc3cf89d098..1c6bbd95afa26451f10a8789ef88a75bb9c000ce 100644 (file)
@@ -57,7 +57,10 @@ public class DbVersion107 implements DbVersion {
       .add(10_7_013, "Create 'cve_cwe' table", CreateCveCweTable.class)
       .add(10_7_014, "Create 'issues_dependency' table", CreateIssuesDependencyTable.class)
       .add(10_7_015, "Add 'ai_code_assurance' column to 'projects' table", AddAiCodeAssuranceColumnInProjectsTable.class)
-    ;
+      .add(10_7_016, "Create 'measures' table", CreateMeasuresTable.class)
+      // TODO data migration
+      .add(10_7_018, "Create primary key on 'measures' table", CreatePrimaryKeyOnMeasuresTable.class)
+      .add(10_7_019, "Create index on column 'branch_uuid' in 'measures' table", CreateIndexOnMeasuresTable.class);
   }
 
 }