diff options
author | Janos Gyerik <janos.gyerik@sonarsource.com> | 2018-07-26 17:03:55 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-08-10 20:21:28 +0200 |
commit | c24345328a3d90bae66586bcf0a411eff7619c2d (patch) | |
tree | 98779733b52657a5ea29c6007e8754eecd195239 /server/sonar-db-migration/src | |
parent | b08814f7807c1443592af65cd68c2a51dfd4ee37 (diff) | |
download | sonarqube-c24345328a3d90bae66586bcf0a411eff7619c2d.tar.gz sonarqube-c24345328a3d90bae66586bcf0a411eff7619c2d.zip |
SONAR-11038 Add table: alm_project_mappings (#559)
Diffstat (limited to 'server/sonar-db-migration/src')
4 files changed, 192 insertions, 0 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTable.java new file mode 100644 index 00000000000..6264cef7962 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTable.java @@ -0,0 +1,120 @@ +/* + * 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.v73; + +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.UUID_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateAlmProjectMappingsTable extends DdlChange { + + private static final String TABLE_NAME = "alm_project_mappings"; + + private static final VarcharColumnDef UUID = newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setLimit(UUID_SIZE) + .setIsNullable(false) + .build(); + private static final VarcharColumnDef ALM_ID_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("alm_id") + .setIsNullable(false) + .setLimit(40) + .build(); + private static final VarcharColumnDef REPO_ID_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("repo_id") + .setIsNullable(false) + .setLimit(256) + .build(); + private static final VarcharColumnDef PROJECT_UUID_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("project_uuid") + .setIsNullable(false) + .setLimit(40) + .build(); + private static final VarcharColumnDef GITHUB_SLUG_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("github_slug") + .setIsNullable(true) + .setLimit(256) + .build(); + private static final VarcharColumnDef URL_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("url") + .setIsNullable(false) + .setLimit(2000) + .build(); + private static final BigIntegerColumnDef CREATED_AT_COLUMN = newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build(); + private static final BigIntegerColumnDef UPDATED_AT_COLUMN = newBigIntegerColumnDefBuilder() + .setColumnName("updated_at") + .setIsNullable(false) + .build(); + + public CreateAlmProjectMappingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + + if (!tableExists()) { + context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME) + .addPkColumn(UUID) + .addColumn(ALM_ID_COLUMN) + .addColumn(REPO_ID_COLUMN) + .addColumn(PROJECT_UUID_COLUMN) + .addColumn(GITHUB_SLUG_COLUMN) + .addColumn(URL_COLUMN) + .addColumn(CREATED_AT_COLUMN) + .addColumn(UPDATED_AT_COLUMN) + .build()); + + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(ALM_ID_COLUMN) + .addColumn(REPO_ID_COLUMN) + .setUnique(true) + .setTable(TABLE_NAME) + .setName(TABLE_NAME + "_alm_repo") + .build()); + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(PROJECT_UUID_COLUMN) + .setUnique(true) + .setTable(TABLE_NAME) + .setName(TABLE_NAME + "_project") + .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/v73/DbVersion73.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java index 6611ef0b301..b996ea12a5c 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java @@ -40,6 +40,7 @@ public class DbVersion73 implements DbVersion { .add(2210, "Add 'securityhotspotadmin' permission to templates characteristics already having 'issueadmin'", PopulateHotspotAdminPermissionOnTemplatesCharacteristics.class) .add(2211, "Set SUBSCRIPTION not nullable in ORGANIZATIONS", SetSubscriptionOnOrganizationsNotNullable.class) .add(2212, "Add index on ORGANIZATION_MEMBERS", AddIndexOnOrganizationMembers.class) + .add(2213, "Create table to store alm project mappings", CreateAlmProjectMappingsTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest.java new file mode 100644 index 00000000000..79557d02099 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest.java @@ -0,0 +1,71 @@ +/* + * 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.v73; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.BIGINT; +import static java.sql.Types.VARCHAR; +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateAlmProjectMappingsTableTest { + + private static final String TABLE = "alm_project_mappings"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateAlmProjectMappingsTableTest.class, "empty.sql"); + + private CreateAlmProjectMappingsTable underTest = new CreateAlmProjectMappingsTable(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() { + assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); + + db.assertColumnDefinition(TABLE, "uuid", VARCHAR, 40, false); + db.assertPrimaryKey(TABLE, "pk_" + TABLE, "uuid"); + db.assertColumnDefinition(TABLE, "alm_id", VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "repo_id", VARCHAR, 256, false); + db.assertColumnDefinition(TABLE, "project_uuid", VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "github_slug", VARCHAR, 256, true); + db.assertColumnDefinition(TABLE, "url", VARCHAR, 2000, false); + db.assertColumnDefinition(TABLE, "created_at", BIGINT, null, false); + db.assertColumnDefinition(TABLE, "updated_at", BIGINT, null, false); + + db.assertUniqueIndex(TABLE, TABLE + "_alm_repo", "alm_id", "repo_id"); + db.assertUniqueIndex(TABLE, TABLE + "_project", "project_uuid"); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest/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/v73/CreateAlmProjectMappingsTableTest/empty.sql |