From: Benjamin Campomenosi Date: Fri, 14 Apr 2023 13:37:16 +0000 (+0200) Subject: [NO_JIRA] move is_main migration script in version10.1 X-Git-Tag: 10.1.0.73491~420 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5db0a10b86f273965bc8163c791a130aa03d5490;p=sonarqube.git [NO_JIRA] move is_main migration script in version10.1 --- diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranches.java deleted file mode 100644 index 7e4be1b97d4..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranches.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.v100; - -import java.sql.Connection; -import java.sql.SQLException; -import org.sonar.db.Database; -import org.sonar.db.DatabaseUtils; -import org.sonar.server.platform.db.migration.def.BooleanColumnDef; -import org.sonar.server.platform.db.migration.def.ColumnDef; -import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; -import org.sonar.server.platform.db.migration.step.DdlChange; - -public class AddIsMainColumnInProjectBranches extends DdlChange { - - private static final String TABLE_NAME = "project_branches"; - private static final String COLUMN_NAME = "is_main"; - - public AddIsMainColumnInProjectBranches(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - try (Connection c = getDatabase().getDataSource().getConnection()) { - if (!DatabaseUtils.tableColumnExists(c, TABLE_NAME, COLUMN_NAME)) { - ColumnDef columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() - .setColumnName(COLUMN_NAME) - .setIsNullable(true) - .build(); - context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME).addColumn(columnDef).build()); - } - } - - } -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranches.java deleted file mode 100644 index b3a602e2f81..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranches.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.v100; - -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; - -public class AlterIsMainColumnInProjectBranches extends DdlChange { - - private static final String TABLE_NAME = "project_branches"; - private static final String COLUMN_NAME = "is_main"; - - public AlterIsMainColumnInProjectBranches(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - BooleanColumnDef newColumnDef = new BooleanColumnDef.Builder() - .setColumnName(COLUMN_NAME) - .setIsNullable(false) - .build(); - context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME).updateColumn(newColumnDef).build()); - } -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranches.java deleted file mode 100644 index 238f99a98e6..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranches.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.v100; - -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 UpdateIsMainColumnInProjectBranches extends DataChange { - - public UpdateIsMainColumnInProjectBranches(Database db) { - super(db); - } - - @Override - protected void execute(Context context) throws SQLException { - MassUpdate massUpdate = context.prepareMassUpdate(); - - // we need to use case/when/then because Oracle doesn't accept simple solution uuid = project_uuid here - massUpdate.select("select uuid, case when uuid = project_uuid then 'true' else 'false' end from project_branches"); - massUpdate.update("update project_branches set is_main = ? where uuid = ?"); - massUpdate.execute((row, update) -> { - String uuid = row.getString(1); - boolean isMain = row.getBoolean(2); - update.setBoolean(1, isMain); - update.setString(2, uuid); - return true; - }); - - } -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranches.java new file mode 100644 index 00000000000..15a1cd7a7d3 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranches.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.v101; + +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.def.BooleanColumnDef; +import org.sonar.server.platform.db.migration.def.ColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class AddIsMainColumnInProjectBranches extends DdlChange { + + private static final String TABLE_NAME = "project_branches"; + private static final String COLUMN_NAME = "is_main"; + + public AddIsMainColumnInProjectBranches(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (Connection c = getDatabase().getDataSource().getConnection()) { + if (!DatabaseUtils.tableColumnExists(c, TABLE_NAME, COLUMN_NAME)) { + ColumnDef columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() + .setColumnName(COLUMN_NAME) + .setIsNullable(true) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME).addColumn(columnDef).build()); + } + } + + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranches.java new file mode 100644 index 00000000000..ffe748e4cae --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranches.java @@ -0,0 +1,45 @@ +/* + * 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.v101; + +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; + +public class AlterIsMainColumnInProjectBranches extends DdlChange { + + private static final String TABLE_NAME = "project_branches"; + private static final String COLUMN_NAME = "is_main"; + + public AlterIsMainColumnInProjectBranches(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + BooleanColumnDef newColumnDef = new BooleanColumnDef.Builder() + .setColumnName(COLUMN_NAME) + .setIsNullable(false) + .build(); + context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME).updateColumn(newColumnDef).build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java index 29da7ba8698..f2479a6bfd1 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/DbVersion101.java @@ -21,9 +21,6 @@ package org.sonar.server.platform.db.migration.version.v101; import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; import org.sonar.server.platform.db.migration.version.DbVersion; -import org.sonar.server.platform.db.migration.version.v100.AddIsMainColumnInProjectBranches; -import org.sonar.server.platform.db.migration.version.v100.AlterIsMainColumnInProjectBranches; -import org.sonar.server.platform.db.migration.version.v100.UpdateIsMainColumnInProjectBranches; // ignoring bad number formatting, as it's indented that we align the migration numbers to SQ versions @SuppressWarnings("java:S3937") diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranches.java new file mode 100644 index 00000000000..a37d9ca869a --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranches.java @@ -0,0 +1,49 @@ +/* + * 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.v101; + +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 UpdateIsMainColumnInProjectBranches extends DataChange { + + public UpdateIsMainColumnInProjectBranches(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + // we need to use case/when/then because Oracle doesn't accept simple solution uuid = project_uuid here + massUpdate.select("select uuid, case when uuid = project_uuid then 'true' else 'false' end from project_branches"); + massUpdate.update("update project_branches set is_main = ? where uuid = ?"); + massUpdate.execute((row, update) -> { + String uuid = row.getString(1); + boolean isMain = row.getBoolean(2); + update.setBoolean(1, isMain); + update.setString(2, uuid); + return true; + }); + + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranchesTest.java deleted file mode 100644 index 3e28adfcfeb..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranchesTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.v100; - -import java.sql.SQLException; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.db.CoreDbTester; - -import static java.sql.Types.BOOLEAN; - -public class AddIsMainColumnInProjectBranchesTest { - - private static final String TABLE_NAME = "project_branches"; - private static final String COLUMN_NAME = "is_main"; - - @Rule - public final CoreDbTester db = CoreDbTester.createForSchema(AddIsMainColumnInProjectBranchesTest.class, "schema.sql"); - - private final AddIsMainColumnInProjectBranches underTest = new AddIsMainColumnInProjectBranches(db.database()); - - @Test - public void is_main_column_exists_with_null_value() throws SQLException { - db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); - underTest.execute(); - db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, null); - } - - @Test - public void migration_is_reentrant() throws SQLException { - db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); - underTest.execute(); - underTest.execute(); - db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, null); - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranchesTest.java deleted file mode 100644 index cefc30bd618..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranchesTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.v100; - -import java.sql.SQLException; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.db.CoreDbTester; - -import static java.sql.Types.BOOLEAN; - -public class AlterIsMainColumnInProjectBranchesTest { - - private static final String TABLE_NAME = "project_branches"; - private static final String COLUMN_NAME = "is_main"; - - @Rule - public final CoreDbTester db = CoreDbTester.createForSchema(AlterIsMainColumnInProjectBranchesTest.class, "schema.sql"); - - private final AlterIsMainColumnInProjectBranches underTest = new AlterIsMainColumnInProjectBranches(db.database()); - - @Test - public void execute_shouldNotBeNullable() throws SQLException { - db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, true); - underTest.execute(); - db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); - } - - @Test - public void migration_is_reentrant() throws SQLException { - db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, true); - underTest.execute(); - underTest.execute(); - db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranchesTest.java deleted file mode 100644 index 33bf84117d6..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranchesTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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.v100; - -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Collectors; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.core.util.UuidFactory; -import org.sonar.core.util.UuidFactoryFast; -import org.sonar.db.CoreDbTester; -import org.sonar.server.platform.db.migration.step.DataChange; - -import static org.assertj.core.api.Assertions.assertThat; - -public class UpdateIsMainColumnInProjectBranchesTest { - - private final UuidFactory uuidFactory = UuidFactoryFast.getInstance(); - - @Rule - public CoreDbTester db = CoreDbTester.createForSchema(UpdateIsMainColumnInProjectBranchesTest.class, "schema.sql"); - - private final DataChange underTest = new UpdateIsMainColumnInProjectBranches(db.database()); - - private static int not_random_value_always_incremented = 0; - - @Test - public void migration_updates_is_main_if_row_has_the_same_uuids() throws SQLException { - String branchUuid1 = insertProjectBranch(true); - String branchUuid2 = insertProjectBranch(false); - - underTest.execute(); - - assertBranchIsMain(branchUuid1); - assertBranchIsNotMain(branchUuid2); - } - - @Test - public void migration_should_be_reentrant() throws SQLException { - String branchUuid1 = insertProjectBranch(true); - String branchUuid2 = insertProjectBranch(false); - - underTest.execute(); - // re-entrant - underTest.execute(); - - assertBranchIsMain(branchUuid1); - assertBranchIsNotMain(branchUuid2); - } - - private void assertBranchIsMain(String branchUuid) { - assertBranchIs(branchUuid, true); - } - - private void assertBranchIsNotMain(String branchUuid) { - assertBranchIs(branchUuid, false); - } - - private void assertBranchIs(String branchUuid, boolean isMain) { - String selectSql = String.format("select is_main from project_branches where uuid='%s'", branchUuid); - assertThat(db.select(selectSql).stream() - .map(row -> row.get("IS_MAIN")) - .collect(Collectors.toList())) - .containsExactlyInAnyOrder(isMain); - } - - private String insertProjectBranch(boolean sameUuids) { - Map map = new HashMap<>(); - String uuid = uuidFactory.create(); - map.put("UUID", uuid); - if(sameUuids) { - map.put("PROJECT_UUID", uuid); - } else { - map.put("PROJECT_UUID", "uuid" + not_random_value_always_incremented++); - } - map.put("KEE", "randomKey"); - map.put("BRANCH_TYPE", "BRANCH"); - map.put("MERGE_BRANCH_UUID", null); - map.put("CREATED_AT", System.currentTimeMillis()); - map.put("UPDATED_AT", System.currentTimeMillis()); - map.put("PULL_REQUEST_BINARY", null); - map.put("EXCLUDE_FROM_PURGE", true); - map.put("NEED_ISSUE_SYNC", false); - db.executeInsert("project_branches", map); - return uuid; - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranchesTest.java new file mode 100644 index 00000000000..1b4c344a769 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranchesTest.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.v101; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.BOOLEAN; + +public class AddIsMainColumnInProjectBranchesTest { + + private static final String TABLE_NAME = "project_branches"; + private static final String COLUMN_NAME = "is_main"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(AddIsMainColumnInProjectBranchesTest.class, "schema.sql"); + + private final AddIsMainColumnInProjectBranches underTest = new AddIsMainColumnInProjectBranches(db.database()); + + @Test + public void is_main_column_exists_with_null_value() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, null); + } + + @Test + public void migration_is_reentrant() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, null); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranchesTest.java new file mode 100644 index 00000000000..a96600f0f95 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranchesTest.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.v101; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.BOOLEAN; + +public class AlterIsMainColumnInProjectBranchesTest { + + private static final String TABLE_NAME = "project_branches"; + private static final String COLUMN_NAME = "is_main"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(AlterIsMainColumnInProjectBranchesTest.class, "schema.sql"); + + private final AlterIsMainColumnInProjectBranches underTest = new AlterIsMainColumnInProjectBranches(db.database()); + + @Test + public void execute_shouldNotBeNullable() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, true); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + } + + @Test + public void migration_is_reentrant() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, true); + underTest.execute(); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranchesTest.java new file mode 100644 index 00000000000..1b79cd4aab1 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranchesTest.java @@ -0,0 +1,106 @@ +/* + * 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.v101; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UpdateIsMainColumnInProjectBranchesTest { + + private final UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(UpdateIsMainColumnInProjectBranchesTest.class, "schema.sql"); + + private final DataChange underTest = new UpdateIsMainColumnInProjectBranches(db.database()); + + private static int not_random_value_always_incremented = 0; + + @Test + public void migration_updates_is_main_if_row_has_the_same_uuids() throws SQLException { + String branchUuid1 = insertProjectBranch(true); + String branchUuid2 = insertProjectBranch(false); + + underTest.execute(); + + assertBranchIsMain(branchUuid1); + assertBranchIsNotMain(branchUuid2); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + String branchUuid1 = insertProjectBranch(true); + String branchUuid2 = insertProjectBranch(false); + + underTest.execute(); + // re-entrant + underTest.execute(); + + assertBranchIsMain(branchUuid1); + assertBranchIsNotMain(branchUuid2); + } + + private void assertBranchIsMain(String branchUuid) { + assertBranchIs(branchUuid, true); + } + + private void assertBranchIsNotMain(String branchUuid) { + assertBranchIs(branchUuid, false); + } + + private void assertBranchIs(String branchUuid, boolean isMain) { + String selectSql = String.format("select is_main from project_branches where uuid='%s'", branchUuid); + assertThat(db.select(selectSql).stream() + .map(row -> row.get("IS_MAIN")) + .collect(Collectors.toList())) + .containsExactlyInAnyOrder(isMain); + } + + private String insertProjectBranch(boolean sameUuids) { + Map map = new HashMap<>(); + String uuid = uuidFactory.create(); + map.put("UUID", uuid); + if(sameUuids) { + map.put("PROJECT_UUID", uuid); + } else { + map.put("PROJECT_UUID", "uuid" + not_random_value_always_incremented++); + } + map.put("KEE", "randomKey"); + map.put("BRANCH_TYPE", "BRANCH"); + map.put("MERGE_BRANCH_UUID", null); + map.put("CREATED_AT", System.currentTimeMillis()); + map.put("UPDATED_AT", System.currentTimeMillis()); + map.put("PULL_REQUEST_BINARY", null); + map.put("EXCLUDE_FROM_PURGE", true); + map.put("NEED_ISSUE_SYNC", false); + db.executeInsert("project_branches", map); + return uuid; + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranchesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranchesTest/schema.sql deleted file mode 100644 index 4b20881e535..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/AddIsMainColumnInProjectBranchesTest/schema.sql +++ /dev/null @@ -1,15 +0,0 @@ -CREATE TABLE "PROJECT_BRANCHES"( - "UUID" CHARACTER VARYING(50) NOT NULL, - "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, - "KEE" CHARACTER VARYING(255) NOT NULL, - "BRANCH_TYPE" CHARACTER VARYING(12) NOT NULL, - "MERGE_BRANCH_UUID" CHARACTER VARYING(50), - "PULL_REQUEST_BINARY" BINARY LARGE OBJECT, - "MANUAL_BASELINE_ANALYSIS_UUID" CHARACTER VARYING(40), - "CREATED_AT" BIGINT NOT NULL, - "UPDATED_AT" BIGINT NOT NULL, - "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL, - "NEED_ISSUE_SYNC" BOOLEAN NOT NULL -); -ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID"); -CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranchesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranchesTest/schema.sql deleted file mode 100644 index 198c950996b..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/AlterIsMainColumnInProjectBranchesTest/schema.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TABLE "PROJECT_BRANCHES"( - "UUID" CHARACTER VARYING(50) NOT NULL, - "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, - "KEE" CHARACTER VARYING(255) NOT NULL, - "BRANCH_TYPE" CHARACTER VARYING(12) NOT NULL, - "MERGE_BRANCH_UUID" CHARACTER VARYING(50), - "PULL_REQUEST_BINARY" BINARY LARGE OBJECT, - "MANUAL_BASELINE_ANALYSIS_UUID" CHARACTER VARYING(40), - "CREATED_AT" BIGINT NOT NULL, - "UPDATED_AT" BIGINT NOT NULL, - "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL, - "NEED_ISSUE_SYNC" BOOLEAN NOT NULL, - "IS_MAIN" BOOLEAN -); -ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID"); -CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranchesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranchesTest/schema.sql deleted file mode 100644 index 198c950996b..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v100/UpdateIsMainColumnInProjectBranchesTest/schema.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TABLE "PROJECT_BRANCHES"( - "UUID" CHARACTER VARYING(50) NOT NULL, - "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, - "KEE" CHARACTER VARYING(255) NOT NULL, - "BRANCH_TYPE" CHARACTER VARYING(12) NOT NULL, - "MERGE_BRANCH_UUID" CHARACTER VARYING(50), - "PULL_REQUEST_BINARY" BINARY LARGE OBJECT, - "MANUAL_BASELINE_ANALYSIS_UUID" CHARACTER VARYING(40), - "CREATED_AT" BIGINT NOT NULL, - "UPDATED_AT" BIGINT NOT NULL, - "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL, - "NEED_ISSUE_SYNC" BOOLEAN NOT NULL, - "IS_MAIN" BOOLEAN -); -ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID"); -CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranchesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranchesTest/schema.sql new file mode 100644 index 00000000000..4b20881e535 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/AddIsMainColumnInProjectBranchesTest/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE "PROJECT_BRANCHES"( + "UUID" CHARACTER VARYING(50) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, + "KEE" CHARACTER VARYING(255) NOT NULL, + "BRANCH_TYPE" CHARACTER VARYING(12) NOT NULL, + "MERGE_BRANCH_UUID" CHARACTER VARYING(50), + "PULL_REQUEST_BINARY" BINARY LARGE OBJECT, + "MANUAL_BASELINE_ANALYSIS_UUID" CHARACTER VARYING(40), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL, + "NEED_ISSUE_SYNC" BOOLEAN NOT NULL +); +ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranchesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranchesTest/schema.sql new file mode 100644 index 00000000000..198c950996b --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/AlterIsMainColumnInProjectBranchesTest/schema.sql @@ -0,0 +1,16 @@ +CREATE TABLE "PROJECT_BRANCHES"( + "UUID" CHARACTER VARYING(50) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, + "KEE" CHARACTER VARYING(255) NOT NULL, + "BRANCH_TYPE" CHARACTER VARYING(12) NOT NULL, + "MERGE_BRANCH_UUID" CHARACTER VARYING(50), + "PULL_REQUEST_BINARY" BINARY LARGE OBJECT, + "MANUAL_BASELINE_ANALYSIS_UUID" CHARACTER VARYING(40), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL, + "NEED_ISSUE_SYNC" BOOLEAN NOT NULL, + "IS_MAIN" BOOLEAN +); +ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranchesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranchesTest/schema.sql new file mode 100644 index 00000000000..198c950996b --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v101/UpdateIsMainColumnInProjectBranchesTest/schema.sql @@ -0,0 +1,16 @@ +CREATE TABLE "PROJECT_BRANCHES"( + "UUID" CHARACTER VARYING(50) NOT NULL, + "PROJECT_UUID" CHARACTER VARYING(50) NOT NULL, + "KEE" CHARACTER VARYING(255) NOT NULL, + "BRANCH_TYPE" CHARACTER VARYING(12) NOT NULL, + "MERGE_BRANCH_UUID" CHARACTER VARYING(50), + "PULL_REQUEST_BINARY" BINARY LARGE OBJECT, + "MANUAL_BASELINE_ANALYSIS_UUID" CHARACTER VARYING(40), + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + "EXCLUDE_FROM_PURGE" BOOLEAN DEFAULT FALSE NOT NULL, + "NEED_ISSUE_SYNC" BOOLEAN NOT NULL, + "IS_MAIN" BOOLEAN +); +ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST);