aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2024-10-28 12:56:36 +0100
committersonartech <sonartech@sonarsource.com>2024-10-29 20:02:49 +0000
commit122be7bb5860e7d4d643fbce2e5d5b153da8714c (patch)
tree5bdee4e1f4cfeb2bd0516e8d398c11aedf91f7ed /server/sonar-db-migration
parent62721916c9f1e0b2ca9a6fa3a3fbc3907834f531 (diff)
downloadsonarqube-122be7bb5860e7d4d643fbce2e5d5b153da8714c.tar.gz
sonarqube-122be7bb5860e7d4d643fbce2e5d5b153da8714c.zip
SONAR-23485 Fix NPE when importing FOSSA CVEs
Make published_at and last_modified_at nullable
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullableIT.java63
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullable.java49
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java3
3 files changed, 114 insertions, 1 deletions
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullableIT.java
new file mode 100644
index 00000000000..c2d5d754a07
--- /dev/null
+++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullableIT.java
@@ -0,0 +1,63 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v108;
+
+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.BIGINT;
+import static org.sonar.db.MigrationDbTester.createForMigrationStep;
+
+class AlterCveColumnsToNullableIT {
+
+ private static final String TABLE_NAME = "cves";
+
+ @RegisterExtension
+ public final MigrationDbTester db = createForMigrationStep(AlterCveColumnsToNullable.class);
+
+ private final DdlChange underTest = new AlterCveColumnsToNullable(db.database());
+
+ @Test
+ void execute_shouldUpdateConstraints() throws SQLException {
+ db.assertColumnDefinition(TABLE_NAME, "published_at", BIGINT, null, false);
+ db.assertColumnDefinition(TABLE_NAME, "last_modified_at", BIGINT, null, false);
+
+ underTest.execute();
+
+ db.assertColumnDefinition(TABLE_NAME, "published_at", BIGINT, null, true);
+ db.assertColumnDefinition(TABLE_NAME, "last_modified_at", BIGINT, null, true);
+ }
+
+ @Test
+ void execute_shouldBeReentrant() throws SQLException {
+ db.assertColumnDefinition(TABLE_NAME, "published_at", BIGINT, null, false);
+ db.assertColumnDefinition(TABLE_NAME, "last_modified_at", BIGINT, null, false);
+ underTest.execute();
+
+ underTest.execute();
+
+ db.assertColumnDefinition(TABLE_NAME, "published_at", BIGINT, null, true);
+ db.assertColumnDefinition(TABLE_NAME, "last_modified_at", BIGINT, null, true);
+ }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullable.java
new file mode 100644
index 00000000000..77d37626a66
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AlterCveColumnsToNullable.java
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v108;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef;
+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.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+
+public class AlterCveColumnsToNullable extends DdlChange {
+
+ public static final BigIntegerColumnDef LAST_MODIFIED_COLUMN = newBigIntegerColumnDefBuilder().setColumnName("last_modified_at").setIsNullable(true).build();
+ public static final BigIntegerColumnDef PUBLISHED_COLUMN = newBigIntegerColumnDefBuilder().setColumnName("published_at").setIsNullable(true).build();
+
+
+ public AlterCveColumnsToNullable(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(new AlterColumnsBuilder(getDialect(), "cves")
+ .updateColumn(LAST_MODIFIED_COLUMN)
+ .build());
+ context.execute(new AlterColumnsBuilder(getDialect(), "cves")
+ .updateColumn(PUBLISHED_COLUMN)
+ .build());
+ }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java
index 412306bceb7..a766c70ab7e 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java
@@ -57,7 +57,8 @@ public class DbVersion108 implements DbVersion {
.add(10_8_014, "Drop 'measures_migrated' column on 'project_branches' table", DropMeasuresMigratedColumnInProjectBranchesTable.class)
.add(10_8_015, "Add column 'impacts' in 'active_rules' table", AddImpactsColumnInActiveRulesTable.class)
.add(10_8_016, "Create 'project_dependencies' table", CreateProjectDependenciesTable.class)
- .add(10_8_017, "Enable specific MQR mode", EnableSpecificMqrMode.class);
+ .add(10_8_017, "Enable specific MQR mode", EnableSpecificMqrMode.class)
+ .add(10_8_018, "Make columns 'published_at' and 'last_modified_at' nullable on the 'cves' table", AlterCveColumnsToNullable.class);
}
}