]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14334 new 'monorepo' column in project_alm_settings table
authorZipeng WU <zipeng.wu@sonarsource.com>
Mon, 18 Jan 2021 13:57:07 +0000 (14:57 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 25 Jan 2021 20:32:26 +0000 (20:32 +0000)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTable.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/AddMonorepoColumnToProjectAlmSettingsTableTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MakeMonorepoColumnInProjectAlmSettingsTableNotNullableTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/PopulateMonorepoColumnToProjectAlmSettingsTableTest/schema.sql [new file with mode: 0644]

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 (file)
index 0000000..f7c673c
--- /dev/null
@@ -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());
+  }
+}
index d1f844bf0c2e611c92d6d61ecb1806e48fef3044..fc4c4de15bb36ea976779409cdd1bc44bbff25ea 100644 (file)
@@ -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 (file)
index 0000000..8fe101c
--- /dev/null
@@ -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 (file)
index 0000000..6d0660c
--- /dev/null
@@ -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 (file)
index 0000000..100b22f
--- /dev/null
@@ -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 (file)
index 0000000..26fad4d
--- /dev/null
@@ -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 (file)
index 0000000..bfc7647
--- /dev/null
@@ -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 (file)
index 0000000..6da26ad
--- /dev/null
@@ -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 (file)
index 0000000..cf1dd72
--- /dev/null
@@ -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 (file)
index 0000000..cf1dd72
--- /dev/null
@@ -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");