From 3f2324e63166f2834559c031bde0fb3ddaf32bfb Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Mon, 18 Jan 2021 14:57:07 +0100 Subject: [PATCH] SONAR-14334 new 'monorepo' column in project_alm_settings table --- ...norepoColumnToProjectAlmSettingsTable.java | 30 ++++++++++++ .../db/migration/version/v87/DbVersion87.java | 4 ++ ...nInProjectAlmSettingsTableNotNullable.java | 29 +++++++++++ ...norepoColumnToProjectAlmSettingsTable.java | 24 ++++++++++ ...poColumnToProjectAlmSettingsTableTest.java | 43 +++++++++++++++++ ...rojectAlmSettingsTableNotNullableTest.java | 45 +++++++++++++++++ ...poColumnToProjectAlmSettingsTableTest.java | 48 +++++++++++++++++++ .../schema.sql | 14 ++++++ .../schema.sql | 15 ++++++ .../schema.sql | 15 ++++++ 10 files changed, 267 insertions(+) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest/schema.sql diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTable.java new file mode 100644 index 00000000000..f7c673c1b3b --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTable.java @@ -0,0 +1,30 @@ +package org.sonar.server.platform.db.migration.version.v87; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.BooleanColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder; + +public class AddMonorepoColumnToProjectAlmSettingsTable extends DdlChange { + + private static final String TABLE = "project_alm_settings"; + + private static final BooleanColumnDef MONOREPO = newBooleanColumnDefBuilder() + .setColumnName("monorepo") + .setIsNullable(true) + .build(); + + public AddMonorepoColumnToProjectAlmSettingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), TABLE) + .addColumn(MONOREPO) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java index d1f844bf0c2..fc4c4de15bb 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java @@ -39,6 +39,10 @@ public class DbVersion87 implements DbVersion { .add(4207, "Drop table 'alm_app_installs'", DropAlmAppInstallsTable.class) .add(4208, "Drop table 'organizations'", DropOrganizationsTable.class) .add(4209, "Drop table 'organization_members'", DropOrgMembersTable.class) + + .add(4210, "Add column 'monorepo' to table 'project_alm_settings'", AddMonorepoColumnToProjectAlmSettingsTable.class) + .add(4211, "Populate column 'monorepo' to false in table 'project_alm_settings'", PopulateMonorepoColumnToProjectAlmSettingsTable.class) + .add(4212, "Make column 'monorepo' in table 'project_alm_settings' not Nullable", MakeMonorepoColumnInProjectAlmSettingsTableNotNullable.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullable.java new file mode 100644 index 00000000000..8fe101c6861 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullable.java @@ -0,0 +1,29 @@ +package org.sonar.server.platform.db.migration.version.v87; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.BooleanColumnDef; +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.BooleanColumnDef.newBooleanColumnDefBuilder; + +public class MakeMonorepoColumnInProjectAlmSettingsTableNotNullable extends DdlChange { + private static final String TABLE = "project_alm_settings"; + + private static final BooleanColumnDef MONOREPO = newBooleanColumnDefBuilder() + .setColumnName("monorepo") + .setIsNullable(false) + .build(); + + public MakeMonorepoColumnInProjectAlmSettingsTableNotNullable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE) + .updateColumn(MONOREPO) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTable.java new file mode 100644 index 00000000000..6d0660c7ab3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTable.java @@ -0,0 +1,24 @@ +package org.sonar.server.platform.db.migration.version.v87; + +import java.sql.SQLException; +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 PopulateMonorepoColumnToProjectAlmSettingsTable extends DataChange { + public PopulateMonorepoColumnToProjectAlmSettingsTable(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select uuid from project_alm_settings where monorepo is null"); + massUpdate.update("update project_alm_settings set monorepo = ? where uuid = ?"); + massUpdate.execute((row, update) -> { + update.setBoolean(1, false); + update.setString(2, row.getString(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest.java new file mode 100644 index 00000000000..100b22fdbb1 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest.java @@ -0,0 +1,43 @@ +package org.sonar.server.platform.db.migration.version.v87; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddMonorepoColumnToProjectAlmSettingsTableTest { + private static final String TABLE_NAME = "project_alm_settings"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddMonorepoColumnToProjectAlmSettingsTableTest.class, "schema.sql"); + + private final DdlChange underTest = new AddMonorepoColumnToProjectAlmSettingsTable(db.database()); + + @Test + public void add_monorepo_column() throws SQLException { + insertProjectAlmSettings(1); + insertProjectAlmSettings(2); + insertProjectAlmSettings(3); + + underTest.execute(); + + db.assertColumnDefinition("project_alm_settings", "monorepo", Types.BOOLEAN, null, true); + assertThat(db.countRowsOfTable(TABLE_NAME)).isEqualTo(3); + } + + private void insertProjectAlmSettings(int id) { + db.executeInsert("project_alm_settings", + "UUID", "uuid-" + id, + "ALM_SETTING_UUID", "ALM_SETTING_UUID", + "PROJECT_UUID", "PROJECT_UUID-" + id, + "ALM_REPO", "ALM_REPO", + "ALM_SLUG", "ALM_SLUG", + "UPDATED_AT", 12342342, + "CREATED_AT",1232342 + ); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest.java new file mode 100644 index 00000000000..26fad4d8a8c --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest.java @@ -0,0 +1,45 @@ +package org.sonar.server.platform.db.migration.version.v87; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest { + private static final String TABLE_NAME = "project_alm_settings"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest.class, "schema.sql"); + + private final DdlChange underTest = new MakeMonorepoColumnInProjectAlmSettingsTableNotNullable(db.database()); + + @Test + public void verify_monorepo_column_not_nullable() throws SQLException { + insertProjectAlmSettings(1); + insertProjectAlmSettings(2); + insertProjectAlmSettings(3); + + underTest.execute(); + + db.assertColumnDefinition(TABLE_NAME, "monorepo", Types.BOOLEAN, null, false); + + assertThat(db.countRowsOfTable(TABLE_NAME)).isEqualTo(3); + } + + private void insertProjectAlmSettings(int id) { + db.executeInsert("project_alm_settings", + "UUID", "uuid-" + id, + "ALM_SETTING_UUID", "ALM_SETTING_UUID", + "PROJECT_UUID", "PROJECT_UUID-" + id, + "ALM_REPO", "ALM_REPO", + "ALM_SLUG", "ALM_SLUG", + "MONOREPO", false, + "UPDATED_AT", 12342342, + "CREATED_AT",1232342 + ); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest.java new file mode 100644 index 00000000000..bfc7647a384 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest.java @@ -0,0 +1,48 @@ +package org.sonar.server.platform.db.migration.version.v87; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateMonorepoColumnToProjectAlmSettingsTableTest { + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateMonorepoColumnToProjectAlmSettingsTableTest.class, "schema.sql"); + + private final DataChange underTest = new PopulateMonorepoColumnToProjectAlmSettingsTable(db.database()); + + @Test + public void populate_monorepo_on_empty_table() throws SQLException { + underTest.execute(); + + assertThat(db.countSql("select count(*) from project_alm_settings")).isZero(); + } + + @Test + public void populate_monorepo_column() throws SQLException { + insertProjectAlmSettings(1); + insertProjectAlmSettings(2); + insertProjectAlmSettings(3); + + underTest.execute(); + + db.assertColumnDefinition("project_alm_settings", "monorepo", Types.BOOLEAN, null, true); + assertThat(db.countSql("select count(uuid) from project_alm_settings where monorepo = false")).isEqualTo(3); + } + + private void insertProjectAlmSettings(int id) { + db.executeInsert("project_alm_settings", + "UUID", "uuid-" + id, + "ALM_SETTING_UUID", "ALM_SETTING_UUID", + "PROJECT_UUID", "PROJECT_UUID-" + id, + "ALM_REPO", "ALM_REPO", + "ALM_SLUG", "ALM_SLUG", + "UPDATED_AT", 12342342, + "CREATED_AT",1232342 + ); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest/schema.sql new file mode 100644 index 00000000000..6da26ad8dc2 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest/schema.sql @@ -0,0 +1,14 @@ +CREATE TABLE "PROJECT_ALM_SETTINGS"( + "UUID" VARCHAR(40) NOT NULL, + "ALM_SETTING_UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "ALM_REPO" VARCHAR(256), + "ALM_SLUG" VARCHAR(256), + "UPDATED_AT" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "SUMMARY_COMMENT_ENABLED" BOOLEAN +); +ALTER TABLE "PROJECT_ALM_SETTINGS" ADD CONSTRAINT "PK_PROJECT_ALM_SETTINGS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PROJECT_ALM_SETTINGS" ON "PROJECT_ALM_SETTINGS"("PROJECT_UUID"); +CREATE INDEX "PROJECT_ALM_SETTINGS_ALM" ON "PROJECT_ALM_SETTINGS"("ALM_SETTING_UUID"); +CREATE INDEX "PROJECT_ALM_SETTINGS_SLUG" ON "PROJECT_ALM_SETTINGS"("ALM_SLUG"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest/schema.sql new file mode 100644 index 00000000000..cf1dd7252b3 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE "PROJECT_ALM_SETTINGS"( + "UUID" VARCHAR(40) NOT NULL, + "ALM_SETTING_UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "ALM_REPO" VARCHAR(256), + "ALM_SLUG" VARCHAR(256), + "UPDATED_AT" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "SUMMARY_COMMENT_ENABLED" BOOLEAN, + "MONOREPO" BOOLEAN +); +ALTER TABLE "PROJECT_ALM_SETTINGS" ADD CONSTRAINT "PK_PROJECT_ALM_SETTINGS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PROJECT_ALM_SETTINGS" ON "PROJECT_ALM_SETTINGS"("PROJECT_UUID"); +CREATE INDEX "PROJECT_ALM_SETTINGS_ALM" ON "PROJECT_ALM_SETTINGS"("ALM_SETTING_UUID"); +CREATE INDEX "PROJECT_ALM_SETTINGS_SLUG" ON "PROJECT_ALM_SETTINGS"("ALM_SLUG"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest/schema.sql new file mode 100644 index 00000000000..cf1dd7252b3 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE "PROJECT_ALM_SETTINGS"( + "UUID" VARCHAR(40) NOT NULL, + "ALM_SETTING_UUID" VARCHAR(40) NOT NULL, + "PROJECT_UUID" VARCHAR(50) NOT NULL, + "ALM_REPO" VARCHAR(256), + "ALM_SLUG" VARCHAR(256), + "UPDATED_AT" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "SUMMARY_COMMENT_ENABLED" BOOLEAN, + "MONOREPO" BOOLEAN +); +ALTER TABLE "PROJECT_ALM_SETTINGS" ADD CONSTRAINT "PK_PROJECT_ALM_SETTINGS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PROJECT_ALM_SETTINGS" ON "PROJECT_ALM_SETTINGS"("PROJECT_UUID"); +CREATE INDEX "PROJECT_ALM_SETTINGS_ALM" ON "PROJECT_ALM_SETTINGS"("ALM_SETTING_UUID"); +CREATE INDEX "PROJECT_ALM_SETTINGS_SLUG" ON "PROJECT_ALM_SETTINGS"("ALM_SLUG"); -- 2.39.5