diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2019-10-10 16:37:30 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-11-06 10:04:26 +0100 |
commit | ab573e2a2597785da80ac461a1c0a3722e5dfdbc (patch) | |
tree | a7833b89a2a7148fe75104faf015df2605867831 /server/sonar-db-migration | |
parent | 87faff43bcc6dcd1f2ddc6b51ba8eaf7e65ed034 (diff) | |
download | sonarqube-ab573e2a2597785da80ac461a1c0a3722e5dfdbc.tar.gz sonarqube-ab573e2a2597785da80ac461a1c0a3722e5dfdbc.zip |
SONAR-12512 Allow project binding on multiple GitHub instances
Diffstat (limited to 'server/sonar-db-migration')
4 files changed, 174 insertions, 2 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/CreateProjectAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/CreateProjectAlmSettingsTable.java new file mode 100644 index 00000000000..af1507db728 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/CreateProjectAlmSettingsTable.java @@ -0,0 +1,108 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.v81; + +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.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.UUID_VARCHAR_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateProjectAlmSettingsTable extends DdlChange { + + private static final String TABLE_NAME = "project_alm_settings"; + + private static final VarcharColumnDef PROJECT_UUID = newVarcharColumnDefBuilder() + .setColumnName("project_uuid") + .setIsNullable(false) + .setLimit(UUID_VARCHAR_SIZE) + .build(); + + private static final VarcharColumnDef ALM_SETTING_UUID = newVarcharColumnDefBuilder() + .setColumnName("alm_setting_uuid") + .setIsNullable(false) + .setLimit(UUID_SIZE) + .build(); + + public CreateProjectAlmSettingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + if (tableExists()) { + return; + } + context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME) + .addPkColumn(newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(false) + .setLimit(UUID_SIZE) + .build()) + .addColumn(ALM_SETTING_UUID) + .addColumn(PROJECT_UUID) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("alm_repo") + .setIsNullable(true) + .setLimit(256) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("alm_slug") + .setIsNullable(true) + .setLimit(256) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("updated_at") + .setIsNullable(false) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build()) + .build()); + + context.execute(new CreateIndexBuilder() + .setTable(TABLE_NAME) + .addColumn(PROJECT_UUID) + .setName("uniq_project_alm_settings") + .setUnique(true) + .build()); + + context.execute(new CreateIndexBuilder() + .setTable(TABLE_NAME) + .addColumn(ALM_SETTING_UUID) + .setName("project_alm_settings_alm") + .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/v81/DbVersion81.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java index b5b2897a92b..f946622d850 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java @@ -26,6 +26,7 @@ public class DbVersion81 implements DbVersion { @Override public void addSteps(MigrationStepRegistry registry) { registry - .add(3100, "Create ALM_SETTINGS table", CreateAlmSettingsTable.class); + .add(3100, "Create ALM_SETTINGS table", CreateAlmSettingsTable.class) + .add(3101, "Create PROJECT_ALM_SETTINGS table", CreateProjectAlmSettingsTable.class); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/CreateProjectAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/CreateProjectAlmSettingsTableTest.java new file mode 100644 index 00000000000..e5b41e28940 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/CreateProjectAlmSettingsTableTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.v81; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.BIGINT; +import static java.sql.Types.VARCHAR; + +public class CreateProjectAlmSettingsTableTest { + private static final String TABLE_NAME = "project_alm_settings"; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createEmpty(); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private CreateProjectAlmSettingsTable underTest = new CreateProjectAlmSettingsTable(dbTester.database()); + + @Test + public void table_has_been_created() throws SQLException { + underTest.execute(); + + dbTester.assertTableExists(TABLE_NAME); + dbTester.assertPrimaryKey(TABLE_NAME, "pk_project_alm_settings", "uuid"); + dbTester.assertUniqueIndex(TABLE_NAME, "uniq_project_alm_settings", "project_uuid"); + dbTester.assertIndex(TABLE_NAME, "project_alm_settings_alm", "alm_setting_uuid"); + + dbTester.assertColumnDefinition(TABLE_NAME, "uuid", VARCHAR, 40, false); + dbTester.assertColumnDefinition(TABLE_NAME, "alm_setting_uuid", VARCHAR, 40, false); + dbTester.assertColumnDefinition(TABLE_NAME, "project_uuid", VARCHAR, 50, false); + dbTester.assertColumnDefinition(TABLE_NAME, "alm_repo", VARCHAR, 256, true); + dbTester.assertColumnDefinition(TABLE_NAME, "alm_slug", VARCHAR, 256, true); + dbTester.assertColumnDefinition(TABLE_NAME, "updated_at", BIGINT, 20, false); + dbTester.assertColumnDefinition(TABLE_NAME, "created_at", BIGINT, 20, false); + + // script should not fail if executed twice + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java index a8dcfb748ca..8e6e0fa4f1e 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java @@ -37,7 +37,7 @@ public class DbVersion81Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 1); + verifyMigrationCount(underTest, 2); } } |