Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dropping of CWE table failing due to FK constraint #3304

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/main/java/org/dependencytrack/upgrade/v4110/v4110Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,44 @@ public void executeUpgrade(final AlpineQueryManager qm, final Connection connect
}

private static void dropCweTable(final Connection connection) throws Exception {
LOGGER.info("Dropping CWE table");
try (final Statement stmt = connection.createStatement()) {
stmt.execute("DROP TABLE \"CWE\"");
final boolean shouldReEnableAutoCommit = connection.getAutoCommit();
connection.setAutoCommit(false);

try {
// In DT v4.4.x and older, the VULNERABILITY table had a foreign key constraint
// on the CWE table. Drop the constraint, the index on the CWE column, as well as
// the column itself if they still exist.
// Unfortunately, the FK constraint has a generic name, as it was generated by DataNucleus.
try (final Statement stmt = connection.createStatement()) {
LOGGER.info("Dropping foreign key constraint from \"VULNERABILITY\".\"CWE\"");
stmt.executeUpdate("""
ALTER TABLE "VULNERABILITY" DROP CONSTRAINT IF EXISTS "VULNERABILITY_FK1"
""");

LOGGER.info("Dropping index \"VULNERABILITY\".\"VULNERABILITY_CWE_IDX\"");
stmt.executeUpdate("""
DROP INDEX IF EXISTS "VULNERABILITY"."VULNERABILITY_CWE_IDX"
""");

LOGGER.info("Dropping column \"VULNERABILITY\".\"CWE\"");
stmt.executeUpdate("""
ALTER TABLE "VULNERABILITY" DROP COLUMN IF EXISTS "CWE"
""");

LOGGER.info("Dropping table CWE");
stmt.executeUpdate("""
DROP TABLE IF EXISTS "CWE"
""");
} catch (Exception e) {
connection.rollback();
throw e;
}

connection.commit();
} finally {
if (shouldReEnableAutoCommit) {
connection.setAutoCommit(true);
}
}
}

Expand Down