diff options
author | Eric Hartmann <hartmann.eric@gmail.com> | 2017-12-01 11:57:03 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-12-14 17:03:35 +0100 |
commit | 9aa4ba88f6e5905fd17d20ec816defd69aa92f33 (patch) | |
tree | 3291b9bf74aba299f50f6d8f31b63e828618a662 /server/sonar-db-migration | |
parent | 40f3c0fc6919afdd118f60df003f94c96f37f5de (diff) | |
download | sonarqube-9aa4ba88f6e5905fd17d20ec816defd69aa92f33.tar.gz sonarqube-9aa4ba88f6e5905fd17d20ec816defd69aa92f33.zip |
SONAR-10134 Add QUALITY_GATES.UUID column
Diffstat (limited to 'server/sonar-db-migration')
11 files changed, 422 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGates.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGates.java new file mode 100644 index 00000000000..1b5fc194950 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGates.java @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class AddUuidToQualityGates extends DdlChange { + + public AddUuidToQualityGates(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), "quality_gates") + .addColumn(VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(true) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java index 05261e0d16c..f11b627b5d4 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform.db.migration.version.v70; +import java.util.UUID; import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; import org.sonar.server.platform.db.migration.version.DbVersion; @@ -39,6 +40,9 @@ public class DbVersion70 implements DbVersion { .add(1909, "Drop index on project_measures.person_id", DropIndexOnPersonMeasures.class) .add(1910, "Create ORG_QUALITY_GATES table", CreateOrgQualityGatesTable.class) .add(1911, "Add ORGANIZATIONS.DEFAULT_QUALITY_GATE_UUID", AddDefaultQualityGateUuidToOrganizations.class) + .add(1912, "Create QUALITY_GATES.UUID", AddUuidToQualityGates.class) + .add(1913, "Populate QUALITY_GATES.UUID", PopulateUuidOnQualityGates.class) + .add(1914, "Make QUALITY_GATES.UUID not nullable", MakeUuidNotNullableOnQualityGates.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGates.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGates.java new file mode 100644 index 00000000000..ae6af527b93 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGates.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class MakeUuidNotNullableOnQualityGates extends DdlChange { + + private static final VarcharColumnDef UUID_COLUMN = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setIsNullable(false) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build(); + private static final String TABLE_NAME = "quality_gates"; + + public MakeUuidNotNullableOnQualityGates(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME) + .updateColumn(UUID_COLUMN) + .build()); + + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(UUID_COLUMN) + .setUnique(true) + .setTable(TABLE_NAME) + .setName("uniq_quality_gates_uuid") + .build() + ); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGates.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGates.java new file mode 100644 index 00000000000..d6189710bdd --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGates.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +import java.util.Date; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class PopulateUuidOnQualityGates extends DataChange { + + private final System2 system2; + private final UuidFactory uuidFactory; + + public PopulateUuidOnQualityGates(Database db, System2 system2, UuidFactory uuidFactory) { + super(db); + this.system2 = system2; + this.uuidFactory = uuidFactory; + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id, name from quality_gates where uuid is null"); + massUpdate.rowPluralName("quality gates"); + massUpdate.update("update quality_gates set uuid=?, updated_at=? where id=?"); + massUpdate.execute((row, update) -> { + String name = row.getString(2); + update.setString(1, uuidFactory.create()); + update.setDate(2, new Date(system2.now())); + update.setLong(3, row.getLong(1)); + return true; + }); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGatesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGatesTest.java new file mode 100644 index 00000000000..e953b5dd18a --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGatesTest.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +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.VARCHAR; + +public class AddUuidToQualityGatesTest { + @Rule + public final CoreDbTester dbTester = CoreDbTester.createForSchema(AddUuidToQualityGatesTest.class, "quality_gates.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddUuidToQualityGates underTest = new AddUuidToQualityGates(dbTester.database()); + + @Test + public void column_is_added_to_table() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition("quality_gates", "uuid", VARCHAR, 40, true); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java index ff36b7f85f7..3ad5338b4e6 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java @@ -35,7 +35,7 @@ public class DbVersion70Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 11); + verifyMigrationCount(underTest, 15); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGatesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGatesTest.java new file mode 100644 index 00000000000..297d0abdd35 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGatesTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +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.VARCHAR; + +public class MakeUuidNotNullableOnQualityGatesTest { + @Rule + public final CoreDbTester dbTester = CoreDbTester.createForSchema(MakeUuidNotNullableOnQualityGatesTest.class, "quality_gates.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MakeUuidNotNullableOnQualityGates underTest = new MakeUuidNotNullableOnQualityGates(dbTester.database()); + + @Test + public void column_is_added_to_table() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition("quality_gates", "uuid", VARCHAR, 40, false); + dbTester.assertUniqueIndex("quality_gates", "uniq_quality_gates_uuid", "uuid"); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGatesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGatesTest.java new file mode 100644 index 00000000000..e0a8b5cc044 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGatesTest.java @@ -0,0 +1,126 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.v70; + +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import javax.annotation.Nullable; +import org.assertj.core.api.AbstractListAssert; +import org.assertj.core.groups.Tuple; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.TestSystem2; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; + +import static java.util.stream.Collectors.toList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class PopulateUuidOnQualityGatesTest { + + private final static long PAST = 10_000_000_000L; + private final static long NOW = 50_000_000_000L; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateUuidOnQualityGatesTest.class, "quality_gates.sql"); + + private System2 system2 = new TestSystem2().setNow(NOW); + + private PopulateUuidOnQualityGates underTest = new PopulateUuidOnQualityGates(db.database(), system2, UuidFactoryFast.getInstance()); + + @Test + public void has_no_effect_if_table_is_empty() throws SQLException { + underTest.execute(); + + assertThat(db.countRowsOfTable("quality_gates")).isEqualTo(0); + } + + @Test + public void updates_uuid_when_uuid_is_null() throws SQLException { + insertQualityGate("Test 1", null); + insertQualityGate("Test 2", null); + + underTest.execute(); + + List<Tuple> tuples = selectAllQualityGates("NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT"); + + assertThat(selectAllQualityGates("NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT")) + .containsExactlyInAnyOrder( + tuple("Test 1", false, new Date(PAST), new Date(NOW)), + tuple("Test 2", false, new Date(PAST), new Date(NOW))); + + selectAllQualityGates("UUID").forEach(c -> assertThat(c).isNotNull()); + } + + @Test + public void does_not_update_uuid_when_uuid_is_not_null() throws SQLException { + insertQualityGate("Test 1", "1"); + insertQualityGate("Test 2", "2"); + + underTest.execute(); + + assertThat(selectAllQualityGates("UUID", "NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT")) + .containsExactlyInAnyOrder( + tuple("1", "Test 1", false, new Date(PAST), new Date(PAST)), + tuple("2", "Test 2", false, new Date(PAST), new Date(PAST))); + } + + @Test + public void execute_is_reentreant() throws SQLException { + insertQualityGate("Test 1", null); + insertQualityGate("Test 2", null); + + underTest.execute(); + underTest.execute(); + + assertThat(selectAllQualityGates("NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT")) + .containsExactlyInAnyOrder( + tuple("Test 1", false, new Date(PAST), new Date(NOW)), + tuple("Test 2", false, new Date(PAST), new Date(NOW))); + + selectAllQualityGates("UUID").forEach(c -> assertThat(c).isNotNull()); + } + + private List<Tuple> selectAllQualityGates(String... columns) { + return db.select("SELECT UUID, NAME, IS_BUILT_IN, CREATED_AT, UPDATED_AT FROM QUALITY_GATES") + .stream() + .map(map -> new Tuple(Arrays.stream(columns).map(c -> map.get(c)).collect(toList()).toArray())) + .collect(toList()); + } + + private void insertQualityGate(String name, @Nullable String uuid) { + db.executeInsert( + "QUALITY_GATES", + "NAME", name, + "UUID", uuid, + "IS_BUILT_IN", false, + "CREATED_AT", new Date(PAST), + "UPDATED_AT", new Date(PAST)); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGatesTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGatesTest/quality_gates.sql new file mode 100644 index 00000000000..f7e89fee40c --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddUuidToQualityGatesTest/quality_gates.sql @@ -0,0 +1,8 @@ +CREATE TABLE "QUALITY_GATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL, + "IS_BUILT_IN" BOOLEAN NOT NULL, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, +); +CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGatesTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGatesTest/quality_gates.sql new file mode 100644 index 00000000000..8ea864766b2 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/MakeUuidNotNullableOnQualityGatesTest/quality_gates.sql @@ -0,0 +1,9 @@ +CREATE TABLE "QUALITY_GATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "UUID" VARCHAR(40), + "NAME" VARCHAR(100) NOT NULL, + "IS_BUILT_IN" BOOLEAN NOT NULL, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, +); +CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGatesTest/quality_gates.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGatesTest/quality_gates.sql new file mode 100644 index 00000000000..8ea864766b2 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/PopulateUuidOnQualityGatesTest/quality_gates.sql @@ -0,0 +1,9 @@ +CREATE TABLE "QUALITY_GATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "UUID" VARCHAR(40), + "NAME" VARCHAR(100) NOT NULL, + "IS_BUILT_IN" BOOLEAN NOT NULL, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, +); +CREATE UNIQUE INDEX "UNIQ_QUALITY_GATES" ON "QUALITY_GATES" ("NAME"); |