aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration/src
diff options
context:
space:
mode:
authorEric Giffon <eric.giffon@sonarsource.com>2024-08-27 11:15:55 +0200
committersonartech <sonartech@sonarsource.com>2024-10-09 20:02:45 +0000
commita4da413ce8b4eab78aadc9e492d437f45d9b7e6f (patch)
tree0de148fd6be461677198ce16f846efb7d9e7b4e4 /server/sonar-db-migration/src
parent20bb622af8982eba8fa419d9ae22e4aa66569ed5 (diff)
downloadsonarqube-a4da413ce8b4eab78aadc9e492d437f45d9b7e6f.tar.gz
sonarqube-a4da413ce8b4eab78aadc9e492d437f45d9b7e6f.zip
SONAR-22871 Create measures table
Diffstat (limited to 'server/sonar-db-migration/src')
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTableIT.java58
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTableIT.java70
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTableIT.java58
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTable.java57
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTable.java57
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTable.java48
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java5
7 files changed, 352 insertions, 1 deletions
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
index 00000000000..e6f0b018b4e
--- /dev/null
+++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTableIT.java
@@ -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
index 00000000000..30203ef8b95
--- /dev/null
+++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTableIT.java
@@ -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
index 00000000000..56bdcc0a55f
--- /dev/null
+++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTableIT.java
@@ -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
index 00000000000..3eb07ceb6e9
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateIndexOnMeasuresTable.java
@@ -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
index 00000000000..8a7a362b01c
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreateMeasuresTable.java
@@ -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
index 00000000000..7fa897952ee
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/CreatePrimaryKeyOnMeasuresTable.java
@@ -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());
+ }
+ }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java
index 6735d32f0ee..1c6bbd95afa 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java
@@ -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);
}
}