diff options
Diffstat (limited to 'server')
18 files changed, 693 insertions, 7 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java index b06fe15e72f..e1fb868b7c0 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java @@ -1980,13 +1980,14 @@ oldCreationDate)); "license_expression", "MIT", "known", true, "known_package", true, + "is_new", false, "created_at", 0L, "updated_at", 0L); db.executeInsert("sca_releases", merge(releaseBase, Map.of("uuid", "release-uuid1", "component_uuid", branch1Uuid))); db.executeInsert("sca_releases", merge(releaseBase, Map.of("uuid", "release-uuid2", "component_uuid", branch2Uuid))); assertThat(db.countRowsOfTable(dbSession, "sca_releases")).isEqualTo(2); var dependencyBase = Map.of("created_at", 0L, "updated_at", 0L, - "direct", true, "scope", "compile", "new_in_pull_request", true); + "direct", true, "scope", "compile", "is_new", true); db.executeInsert("sca_dependencies", merge(dependencyBase, Map.of("uuid", "dependency-uuid1", "sca_release_uuid", "release-uuid1"))); db.executeInsert("sca_dependencies", merge(dependencyBase, Map.of("uuid", "dependency-uuid2", "sca_release_uuid", "release-uuid2"))); assertThat(db.countRowsOfTable(dbSession, "sca_dependencies")).isEqualTo(2); diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index d102e2cd209..3ca28a1b342 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -1052,8 +1052,8 @@ CREATE TABLE "SCA_DEPENDENCIES"( "CHAINS" CHARACTER LARGE OBJECT, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL, - "NEW_IN_PULL_REQUEST" BOOLEAN DEFAULT FALSE NOT NULL, - "PRODUCTION_SCOPE" BOOLEAN DEFAULT TRUE NOT NULL + "PRODUCTION_SCOPE" BOOLEAN DEFAULT TRUE NOT NULL, + "IS_NEW" BOOLEAN DEFAULT FALSE NOT NULL ); ALTER TABLE "SCA_DEPENDENCIES" ADD CONSTRAINT "PK_SCA_DEPENDENCIES" PRIMARY KEY("UUID"); CREATE INDEX "SCA_DEPENDENCIES_RELEASE_UUID" ON "SCA_DEPENDENCIES"("SCA_RELEASE_UUID" NULLS FIRST); @@ -1096,9 +1096,9 @@ CREATE TABLE "SCA_RELEASES"( "KNOWN" BOOLEAN NOT NULL, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL, - "NEW_IN_PULL_REQUEST" BOOLEAN DEFAULT FALSE NOT NULL, "DECLARED_LICENSE_EXPRESSION" CHARACTER VARYING(400) DEFAULT 'NOASSERTION' NOT NULL, - "KNOWN_PACKAGE" BOOLEAN NOT NULL + "KNOWN_PACKAGE" BOOLEAN NOT NULL, + "IS_NEW" BOOLEAN DEFAULT FALSE NOT NULL ); ALTER TABLE "SCA_RELEASES" ADD CONSTRAINT "PK_SCA_RELEASES" PRIMARY KEY("UUID"); CREATE INDEX "SCA_RELEASES_COMP_UUID_UUID" ON "SCA_RELEASES"("COMPONENT_UUID" NULLS FIRST, "UUID" NULLS FIRST); diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaDependenciesTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaDependenciesTableIT.java new file mode 100644 index 00000000000..e619add3bb7 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaDependenciesTableIT.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.BOOLEAN; +import static org.sonar.db.MigrationDbTester.createForMigrationStep; + +class AddIsNewToScaDependenciesTableIT { + private static final String TABLE_NAME = "sca_dependencies"; + private static final String COLUMN_NAME = "is_new"; + + @RegisterExtension + public final MigrationDbTester db = createForMigrationStep(AddIsNewToScaDependenciesTable.class); + private final DdlChange underTest = new AddIsNewToScaDependenciesTable(db.database()); + + @Test + void execute_shouldAddColumn() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + } + + @Test + void execute_shouldBeReentrant() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaReleasesTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaReleasesTableIT.java new file mode 100644 index 00000000000..eca405f0738 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaReleasesTableIT.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.BOOLEAN; +import static org.sonar.db.MigrationDbTester.createForMigrationStep; + +class AddIsNewToScaReleasesTableIT { + private static final String TABLE_NAME = "sca_releases"; + private static final String COLUMN_NAME = "is_new"; + + @RegisterExtension + public final MigrationDbTester db = createForMigrationStep(AddIsNewToScaReleasesTable.class); + private final DdlChange underTest = new AddIsNewToScaReleasesTable(db.database()); + + @Test + void execute_shouldAddColumn() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + } + + @Test + void execute_shouldBeReentrant() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaDependenciesTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaDependenciesTableIT.java new file mode 100644 index 00000000000..0ee0034af74 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaDependenciesTableIT.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.BOOLEAN; +import static org.sonar.db.MigrationDbTester.createForMigrationStep; + +class DropNewInPullRequestFromScaDependenciesTableIT { + private static final String TABLE_NAME = "sca_dependencies"; + private static final String COLUMN_NAME = "new_in_pull_request"; + + @RegisterExtension + public final MigrationDbTester db = createForMigrationStep(DropNewInPullRequestFromScaDependenciesTable.class); + private final DdlChange underTest = new DropNewInPullRequestFromScaDependenciesTable(db.database()); + + @Test + void execute_shouldAddColumn() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + underTest.execute(); + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + } + + @Test + void execute_shouldBeReentrant() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + underTest.execute(); + underTest.execute(); + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaReleasesTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaReleasesTableIT.java new file mode 100644 index 00000000000..252537bfcc8 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaReleasesTableIT.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import java.sql.SQLException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.sql.Types.BOOLEAN; +import static org.sonar.db.MigrationDbTester.createForMigrationStep; + +class DropNewInPullRequestFromScaReleasesTableIT { + private static final String TABLE_NAME = "sca_releases"; + private static final String COLUMN_NAME = "new_in_pull_request"; + + @RegisterExtension + public final MigrationDbTester db = createForMigrationStep(DropNewInPullRequestFromScaReleasesTable.class); + private final DdlChange underTest = new DropNewInPullRequestFromScaReleasesTable(db.database()); + + @Test + void execute_shouldAddColumn() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + underTest.execute(); + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + } + + @Test + void execute_shouldBeReentrant() throws SQLException { + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, BOOLEAN, null, false); + underTest.execute(); + underTest.execute(); + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaDependenciesIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaDependenciesIT.java new file mode 100644 index 00000000000..e50f8fe6d4d --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaDependenciesIT.java @@ -0,0 +1,82 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import java.sql.SQLException; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.MigrationDbTester.createForMigrationStep; + +class MigrateToIsNewOnScaDependenciesIT { + private static final String COLUMN_NAME = "is_new"; + + @RegisterExtension + public final MigrationDbTester db = createForMigrationStep(MigrateToIsNewOnScaDependencies.class); + private final DataChange underTest = new MigrateToIsNewOnScaDependencies(db.database()); + + private static Stream<Arguments> provideParamsForExecute() { + return Stream.of( + Arguments.of(false, false, false), + Arguments.of(false, true, false), + Arguments.of(true, false, true), + Arguments.of(true, true, true)); + } + + @Test + void execute_doesNotCreateRecords() throws SQLException { + underTest.execute(); + assertThat(db.countSql("select count(*) from sca_dependencies")).isZero(); + } + + @ParameterizedTest + @MethodSource("provideParamsForExecute") + void execute_updatesCorrectly(boolean newInPullRequest, boolean isNew, boolean expectedIsNew) throws SQLException { + insertDependency("1", newInPullRequest, isNew); + + assertThat(db.selectFirst("select * from sca_dependencies where uuid = '%s'".formatted("scaReleaseUuid1"))).containsEntry(COLUMN_NAME, isNew); + underTest.execute(); + assertThat(db.selectFirst("select * from sca_dependencies where uuid = '%s'".formatted("scaReleaseUuid1"))).containsEntry(COLUMN_NAME, expectedIsNew); + } + + private void insertDependency(String suffix, boolean newInPullRequest, @Nullable Boolean isNew) { + db.executeInsert("sca_dependencies", + "uuid", "scaReleaseUuid" + suffix, + "sca_release_uuid", "componentUuid" + suffix, + "direct", true, + "scope", "development", + "production_scope", true, + "user_dependency_file_path", "Gemfile", + "lockfile_dependency_file_path", "Gemfile.lock", + "chains", "[]", + "is_new", isNew, + "new_in_pull_request", newInPullRequest, + "updated_at", 1L, + "created_at", 2L); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaReleasesIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaReleasesIT.java new file mode 100644 index 00000000000..50f2c66a8b4 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaReleasesIT.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import java.sql.SQLException; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.sonar.db.MigrationDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.MigrationDbTester.createForMigrationStep; + +class MigrateToIsNewOnScaReleasesIT { + private static final String COLUMN_NAME = "is_new"; + + @RegisterExtension + public final MigrationDbTester db = createForMigrationStep(MigrateToIsNewOnScaReleases.class); + private final DataChange underTest = new MigrateToIsNewOnScaReleases(db.database()); + + private static Stream<Arguments> provideParamsForExecute() { + return Stream.of( + Arguments.of(false, false, false), + Arguments.of(false, true, false), + Arguments.of(true, false, true), + Arguments.of(true, true, true)); + } + + @Test + void execute_doesNotCreateRecords() throws SQLException { + underTest.execute(); + assertThat(db.countSql("select count(*) from sca_releases")).isZero(); + } + + @ParameterizedTest + @MethodSource("provideParamsForExecute") + void execute_updatesCorrectly(boolean newInPullRequest, boolean isNew, boolean expectedIsNew) throws SQLException { + insertRelease("1", newInPullRequest, isNew); + + assertThat(db.selectFirst("select * from sca_releases where uuid = '%s'".formatted("scaReleaseUuid1"))).containsEntry(COLUMN_NAME, isNew); + underTest.execute(); + assertThat(db.selectFirst("select * from sca_releases where uuid = '%s'".formatted("scaReleaseUuid1"))).containsEntry(COLUMN_NAME, expectedIsNew); + } + + private void insertRelease(String suffix, boolean newInPullRequest, @Nullable Boolean isNew) { + db.executeInsert("sca_releases", + "uuid", "scaReleaseUuid" + suffix, + "component_uuid", "componentUuid" + suffix, + "package_url", "packageUrl", + "package_manager", "MAVEN", + "package_name", "packageName" + suffix, + "version", "1.0.0", + "license_expression", "MIT", + "declared_license_expression", "MIT", + "is_new", isNew, + "new_in_pull_request", newInPullRequest, + "known", true, + "known_package", true, + "updated_at", 1L, + "created_at", 2L); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropColumnsBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropColumnsBuilder.java index 5da5ab75c03..9be457e7574 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropColumnsBuilder.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropColumnsBuilder.java @@ -30,7 +30,11 @@ import org.sonar.db.dialect.Oracle; import org.sonar.db.dialect.PostgreSql; /** - * Generate a SQL query to drop multiple columns from a table + * Generate a SQL query to drop multiple columns from a table. You probably want to + * use DropColumnChange instead, as it will handle dropping autogenerated constraints + * on the column for you. + * + * @see org.sonar.server.platform.db.migration.step.DropColumnChange */ public class DropColumnsBuilder { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropColumnChange.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropColumnChange.java index b32fd2a5923..6836025742a 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropColumnChange.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropColumnChange.java @@ -26,6 +26,18 @@ import org.sonar.db.dialect.MsSql; import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder; import org.sonar.server.platform.db.migration.sql.DropMsSQLDefaultConstraintsBuilder; +/** + * Drop a column from a table. This also takes care of removing any autogenerated + * constraints on the column for you. + * + * <p> + * Usage: + * <ol> + * <li>Extend your migration class from this class instead of `DdlChange`.</li> + * <li>Call `super` in the constructor with your table and column names.</li> + * </ol> + * </p> + */ public abstract class DropColumnChange extends DdlChange { private final String tableName; diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaDependenciesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaDependenciesTable.java new file mode 100644 index 00000000000..a96ad27592e --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaDependenciesTable.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +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.db.DatabaseUtils.tableColumnExists; + +public class AddIsNewToScaDependenciesTable extends DdlChange { + static final String TABLE_NAME = "sca_dependencies"; + static final String COLUMN_NAME = "is_new"; + + public AddIsNewToScaDependenciesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + if (!tableColumnExists(connection, TABLE_NAME, COLUMN_NAME)) { + var columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() + .setColumnName(COLUMN_NAME) + .setIsNullable(false) + .setDefaultValue(false) + .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/v202503/AddIsNewToScaReleasesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaReleasesTable.java new file mode 100644 index 00000000000..f6eca6ddc67 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/AddIsNewToScaReleasesTable.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +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.db.DatabaseUtils.tableColumnExists; + +public class AddIsNewToScaReleasesTable extends DdlChange { + static final String TABLE_NAME = "sca_releases"; + static final String COLUMN_NAME = "is_new"; + + public AddIsNewToScaReleasesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + if (!tableColumnExists(connection, TABLE_NAME, COLUMN_NAME)) { + var columnDef = BooleanColumnDef.newBooleanColumnDefBuilder() + .setColumnName(COLUMN_NAME) + .setIsNullable(false) + .setDefaultValue(false) + .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/v202503/DbVersion202503.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DbVersion202503.java index ea3c50af92d..bdf7cca14c8 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DbVersion202503.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DbVersion202503.java @@ -41,6 +41,12 @@ public class DbVersion202503 implements DbVersion { .add(2025_03_002, "Update known_package on SCA release to be not nullable", UpdateKnownPackageColumnNotNullable.class) .add(2025_03_003, "Add status column to SCA issues releases join table", AddStatusToScaIssuesReleasesTable.class) .add(2025_03_004, "Populate status column to SCA issues releases join table", PopulateStatusColumnForScaIssuesReleasesTable.class) - .add(2025_03_005, "Update status column on SCA issues releases join table to be not nullable", UpdateScaIssuesReleasesStatusColumnNotNullable.class); + .add(2025_03_005, "Update status column on SCA issues releases join table to be not nullable", UpdateScaIssuesReleasesStatusColumnNotNullable.class) + .add(2025_03_006, "Add is_new column to SCA releases", AddIsNewToScaReleasesTable.class) + .add(2025_03_007, "Add is_new column to SCA dependencies", AddIsNewToScaDependenciesTable.class) + .add(2025_03_008, "Migrate to is_new on SCA releases", MigrateToIsNewOnScaReleases.class) + .add(2025_03_009, "Migrate to is_new on SCA dependencies", MigrateToIsNewOnScaDependencies.class) + .add(2025_03_010, "Drop new_in_pull_request column from SCA releases", DropNewInPullRequestFromScaReleasesTable.class) + .add(2025_03_011, "Drop new_in_pull_request column from SCA dependencies", DropNewInPullRequestFromScaDependenciesTable.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaDependenciesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaDependenciesTable.java new file mode 100644 index 00000000000..eee944c8d9e --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaDependenciesTable.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DropColumnChange; + +public class DropNewInPullRequestFromScaDependenciesTable extends DropColumnChange { + static final String TABLE_NAME = "sca_dependencies"; + static final String COLUMN_NAME = "new_in_pull_request"; + + public DropNewInPullRequestFromScaDependenciesTable(Database db) { + super(db, TABLE_NAME, COLUMN_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaReleasesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaReleasesTable.java new file mode 100644 index 00000000000..31f08ea6166 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/DropNewInPullRequestFromScaReleasesTable.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DropColumnChange; + +public class DropNewInPullRequestFromScaReleasesTable extends DropColumnChange { + static final String TABLE_NAME = "sca_releases"; + static final String COLUMN_NAME = "new_in_pull_request"; + + public DropNewInPullRequestFromScaReleasesTable(Database db) { + super(db, TABLE_NAME, COLUMN_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaDependencies.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaDependencies.java new file mode 100644 index 00000000000..f014c602565 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaDependencies.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import org.sonar.db.Database; + +public class MigrateToIsNewOnScaDependencies extends MigrateToIsNewOnScaTable { + private static final String SELECT_QUERY = "select new_in_pull_request, uuid from sca_dependencies where new_in_pull_request <> is_new"; + private static final String UPDATE_QUERY = "update sca_dependencies set is_new = ? where uuid = ?"; + + public MigrateToIsNewOnScaDependencies(Database db) { + super(db, SELECT_QUERY, UPDATE_QUERY); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaReleases.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaReleases.java new file mode 100644 index 00000000000..546ed7345ce --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaReleases.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +import org.sonar.db.Database; + +public class MigrateToIsNewOnScaReleases extends MigrateToIsNewOnScaTable { + private static final String SELECT_QUERY = "select new_in_pull_request, uuid from sca_releases where new_in_pull_request <> is_new"; + private static final String UPDATE_QUERY = "update sca_releases set is_new = ? where uuid = ?"; + + public MigrateToIsNewOnScaReleases(Database db) { + super(db, SELECT_QUERY, UPDATE_QUERY); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaTable.java new file mode 100644 index 00000000000..3f1944fc4d2 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202503/MigrateToIsNewOnScaTable.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 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.v202503; + +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 MigrateToIsNewOnScaTable extends DataChange { + private String selectQuery; + private String updateQuery; + + public MigrateToIsNewOnScaTable( + Database db, + String selectQuery, + String updateQuery) { + super(db); + this.selectQuery = selectQuery; + this.updateQuery = updateQuery; + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select(selectQuery); + massUpdate.update(updateQuery); + + massUpdate.execute((row, update, index) -> { + update + .setBoolean(1, row.getBoolean(1)) + .setString(2, row.getString(2)); + return true; + }); + } +} |