diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-03-30 10:13:33 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-05-16 20:20:45 +0200 |
commit | 5073eb0f98e99ab0efbdd3f34b07a97f11e1d26c (patch) | |
tree | 418dbeb6b1c0b512b9d06ab542dec9887431c05f /server/sonar-db-migration | |
parent | ac3c3089d82cc78390f761776a848be77306538d (diff) | |
download | sonarqube-5073eb0f98e99ab0efbdd3f34b07a97f11e1d26c.tar.gz sonarqube-5073eb0f98e99ab0efbdd3f34b07a97f11e1d26c.zip |
SONAR-10517 Create table project_mappings
* Migration
* Add DAO
* Purge on project deletion
Diffstat (limited to 'server/sonar-db-migration')
5 files changed, 172 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTable.java new file mode 100644 index 00000000000..0417cb1c877 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTable.java @@ -0,0 +1,103 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +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.def.BigIntegerColumnDef; +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.MAX_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateProjectMappingsTable extends DdlChange { + + private static final String TABLE_NAME = "project_mappings"; + + private static final VarcharColumnDef UUID_COLUMN = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(false) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + private static final VarcharColumnDef KEY_TYPE_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("key_type") + .setIsNullable(false) + .setLimit(200) + .build(); + private static final VarcharColumnDef KEE_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("kee") + .setIsNullable(false) + .setLimit(MAX_SIZE) + .build(); + private static final VarcharColumnDef PROJECT_UUID = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("project_uuid") + .setIsNullable(false) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + private static final BigIntegerColumnDef CREATED_AT_COLUMN = newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build(); + + public CreateProjectMappingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + + if (!tableExists()) { + context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME) + .addPkColumn(UUID_COLUMN) + .addColumn(KEY_TYPE_COLUMN) + .addColumn(KEE_COLUMN) + .addColumn(PROJECT_UUID) + .addColumn(CREATED_AT_COLUMN) + .build()); + + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(KEY_TYPE_COLUMN) + .addColumn(KEE_COLUMN) + .setUnique(true) + .setTable(TABLE_NAME) + .setName("key_type_kee") + .build()); + + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(PROJECT_UUID) + .setUnique(false) + .setTable(TABLE_NAME) + .setName("project_uuid") + .build()); + } + } + + private boolean tableExists() throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.tableExists(TABLE_NAME, connection); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java index 803381a4234..8400eb1a14e 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java @@ -33,6 +33,7 @@ public class DbVersion72 implements DbVersion { .add(2103, "Add isExternal boolean to rules", AddRuleExternal.class) .add(2104, "Create ALM_APP_INSTALLS table", CreateAlmAppInstallsTable.class) .add(2105, "Add LINE_HASHES_VERSION to table FILE_SOURCES", AddLineHashesVersionToFileSources.class) + .add(2106, "Create PROJECT_MAPPINGS table", CreateProjectMappingsTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTableTest.java new file mode 100644 index 00000000000..19f3d6bbfd8 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTableTest.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.v72; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.VARCHAR; +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateProjectMappingsTableTest { + + private static final String TABLE = "project_mappings"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateProjectMappingsTableTest.class, "empty.sql"); + + private CreateProjectMappingsTable underTest = new CreateProjectMappingsTable(db.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + checkTable(); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + checkTable(); + } + + private void checkTable() { + db.assertColumnDefinition(TABLE, "uuid", Types.VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "key_type", VARCHAR, 200, false); + db.assertColumnDefinition(TABLE, "kee", VARCHAR, 4000, false); + db.assertColumnDefinition(TABLE, "project_uuid", Types.VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "created_at", Types.BIGINT, 20, false); + + db.assertUniqueIndex(TABLE, "key_type_kee", "key_type", "kee"); + db.assertIndex(TABLE, "project_uuid", "project_uuid"); + assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java index eb251a545de..1cb135035ac 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java @@ -34,7 +34,7 @@ public class DbVersion72Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 6); + verifyMigrationCount(underTest, 7); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTableTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTableTest/empty.sql new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/CreateProjectMappingsTableTest/empty.sql |