From c70a0d972d892d61b7a0f2279402779cf15fa0ed Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 12 Dec 2017 10:55:47 +0100 Subject: [PATCH] SONAR-10134 Make Make ORGANIZATIONS.DEFAULT_QUALITY_GATE_UUID not nullable --- .../org/sonar/db/version/schema-h2.ddl | 2 +- .../db/migration/version/v70/DbVersion70.java | 1 + ...yGateUuidAsNotNullableInOrganizations.java | 47 +++++++++++++++ .../version/v70/DbVersion70Test.java | 2 +- ...eUuidAsNotNullableInOrganizationsTest.java | 58 +++++++++++++++++++ .../organizations.sql | 19 ++++++ 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizations.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest/organizations.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index a70eeaa3e1e..3b335d6ccd6 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -10,7 +10,7 @@ CREATE TABLE "ORGANIZATIONS" ( "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40), "DEFAULT_PERM_TEMPLATE_VIEW" VARCHAR(40), "DEFAULT_GROUP_ID" INTEGER, - "DEFAULT_QUALITY_GATE_UUID" VARCHAR(40), // TODO : NOT NULL + "DEFAULT_QUALITY_GATE_UUID" VARCHAR(40) NOT NULL, "NEW_PROJECT_PRIVATE" BOOLEAN NOT NULL, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL 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 1da53519c93..7d355c5753c 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 @@ -48,6 +48,7 @@ public class DbVersion70 implements DbVersion { .add(1918, "Populate default quality gate on organization", PopulateDefaultQualityGate.class) .add(1919, "Associate existing quality gates to default organization", AssociateQualityGatesToDefaultOrganization.class) .add(1920, "Delete 'sonar.qualitygate' setting at global level", DeleteGlobalSonarQualityGateSetting.class) + .add(1921, "Make ORGANIZATIONS.DEFAULT_QUALITY_GATE_UUID not nullable", SetDefaultQualityGateUuidAsNotNullableInOrganizations.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizations.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizations.java new file mode 100644 index 00000000000..8f2168c730e --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizations.java @@ -0,0 +1,47 @@ +/* + * 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.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class SetDefaultQualityGateUuidAsNotNullableInOrganizations extends DdlChange { + + public SetDefaultQualityGateUuidAsNotNullableInOrganizations(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), "organizations") + .updateColumn(newVarcharColumnDefBuilder() + .setColumnName("default_quality_gate_uuid") + .setIsNullable(false) + .setLimit(VarcharColumnDef.UUID_SIZE) + .build()) + .build()); + } +} 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 628ce06a0db..5fd33f8206a 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, 21); + verifyMigrationCount(underTest, 22); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest.java new file mode 100644 index 00000000000..dbc3f6a4d13 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest.java @@ -0,0 +1,58 @@ +/* + * 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 org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.VARCHAR; + +public class SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest { + + @Rule + public final CoreDbTester dbTester = CoreDbTester.createForSchema(SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest.class, "organizations.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private DdlChange underTest = new SetDefaultQualityGateUuidAsNotNullableInOrganizations(dbTester.database()); + + @Test + public void default_quality_gate_uuid_is_set_as_not_nullable() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition("organizations", "default_quality_gate_uuid", VARCHAR, 40, false); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + dbTester.assertColumnDefinition("organizations", "default_quality_gate_uuid", VARCHAR, 40, false); + + underTest.execute(); + dbTester.assertColumnDefinition("organizations", "default_quality_gate_uuid", VARCHAR, 40, false); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest/organizations.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest/organizations.sql new file mode 100644 index 00000000000..161b7f8e71c --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/SetDefaultQualityGateUuidAsNotNullableInOrganizationsTest/organizations.sql @@ -0,0 +1,19 @@ +CREATE TABLE "ORGANIZATIONS" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "KEE" VARCHAR(32) NOT NULL, + "NAME" VARCHAR(64) NOT NULL, + "DESCRIPTION" VARCHAR(256), + "URL" VARCHAR(256), + "AVATAR_URL" VARCHAR(256), + "GUARDED" BOOLEAN NOT NULL, + "USER_ID" INTEGER, + "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_VIEW" VARCHAR(40), + "DEFAULT_GROUP_ID" INTEGER, + "DEFAULT_QUALITY_GATE_UUID" VARCHAR(40), + "NEW_PROJECT_PRIVATE" BOOLEAN NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "PK_ORGANIZATIONS" ON "ORGANIZATIONS" ("UUID"); +CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS" ("KEE"); -- 2.39.5