From: Nolwenn Cadic Date: Tue, 15 Aug 2023 15:40:11 +0000 (+0200) Subject: SONAR-20156 Make project_uuid nullable in user_dismissed_messages X-Git-Tag: 10.2.0.77647~108 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d09789d782ba82e138d940b2505dff0c95d32099;p=sonarqube.git SONAR-20156 Make project_uuid nullable in user_dismissed_messages --- diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 1d99f362b29..7944ece37b2 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -1023,7 +1023,7 @@ CREATE INDEX "SNAPSHOTS_ROOT_COMPONENT_UUID" ON "SNAPSHOTS"("ROOT_COMPONENT_UUID CREATE TABLE "USER_DISMISSED_MESSAGES"( "UUID" CHARACTER VARYING(40) NOT NULL, "USER_UUID" CHARACTER VARYING(255) NOT NULL, - "PROJECT_UUID" CHARACTER VARYING(40) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(40), "MESSAGE_TYPE" CHARACTER VARYING(255) NOT NULL, "CREATED_AT" BIGINT NOT NULL ); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java index 58a9a59067c..c988d839b0a 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java @@ -99,6 +99,7 @@ public class DbVersion102 implements DbVersion { .add(10_2_042, "Create table 'github_orgs_groups'", CreateGithubOrganizationsGroupsTable.class) .add(10_2_043, "Create 'previous_non_compliant_value' in 'new_code_periods' table", CreatePreviousNonCompliantValueInNewCodePeriods.class) .add(10_2_044, "Update column 'value' and populate column 'previous_non_compliant_value' in 'new_code_periods' table", - UpdateValueAndPopulatePreviousNonCompliantValueInNewCodePeriods.class); + UpdateValueAndPopulatePreviousNonCompliantValueInNewCodePeriods.class) + .add(10_2_045, "Alter 'project_uuid' in 'user_dismissed_messages' - make it nullable", MakeProjectUuidNullableInUserDismissedMessages.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessages.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessages.java new file mode 100644 index 00000000000..0cfc56d13e3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessages.java @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.v102; + +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; + +public class MakeProjectUuidNullableInUserDismissedMessages extends DdlChange { + + private static final String TABLE_NAME = "user_dismissed_messages"; + private static final String COLUMN_NAME = "project_uuid"; + + public MakeProjectUuidNullableInUserDismissedMessages(Database db) { + super(db); + } + + @Override + public void execute(DdlChange.Context context) throws SQLException { + VarcharColumnDef newColumnDef = new VarcharColumnDef.Builder() + .setColumnName(COLUMN_NAME) + .setIsNullable(true) + .setLimit(40) + .build(); + context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME).updateColumn(newColumnDef).build()); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessagesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessagesTest.java new file mode 100644 index 00000000000..3ed3ab232c9 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessagesTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.v102; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import static java.sql.Types.VARCHAR; + +public class MakeProjectUuidNullableInUserDismissedMessagesTest { + + private static final String TABLE_NAME = "user_dismissed_messages"; + private static final String COLUMN_NAME = "project_uuid"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(MakeProjectUuidNullableInUserDismissedMessagesTest.class, "schema.sql"); + + private final MakeProjectUuidNullableInUserDismissedMessages underTest = new MakeProjectUuidNullableInUserDismissedMessages(db.database()); + + @Test + public void execute_shouldBeNullable() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, VARCHAR, 40, false); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, VARCHAR, 40, true); + } + + @Test + public void migration_is_reentrant() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, VARCHAR, 40, false); + underTest.execute(); + underTest.execute(); + + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, VARCHAR, 40, true); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessagesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessagesTest/schema.sql new file mode 100644 index 00000000000..aa1795f6042 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/MakeProjectUuidNullableInUserDismissedMessagesTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "USER_DISMISSED_MESSAGES"( + "UUID" CHARACTER VARYING(40) NOT NULL, + "USER_UUID" CHARACTER VARYING(255) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(40) NOT NULL, + "MESSAGE_TYPE" CHARACTER VARYING(255) NOT NULL, + "CREATED_AT" BIGINT NOT NULL +); +ALTER TABLE "USER_DISMISSED_MESSAGES" ADD CONSTRAINT "PK_USER_DISMISSED_MESSAGES" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_USER_DISMISSED_MESSAGES" ON "USER_DISMISSED_MESSAGES"("USER_UUID" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "MESSAGE_TYPE" NULLS FIRST); +CREATE INDEX "UDM_PROJECT_UUID" ON "USER_DISMISSED_MESSAGES"("PROJECT_UUID" NULLS FIRST); +CREATE INDEX "UDM_MESSAGE_TYPE" ON "USER_DISMISSED_MESSAGES"("MESSAGE_TYPE" NULLS FIRST); \ No newline at end of file