Skip to content

fix: store ScanResult.language as text instead of an enum ordinal - #346

Merged
san-zrl merged 1 commit into
mainfrom
fix/scanresult-language-check-constraint
Sep 4, 2026
Merged

san-zrl merged 1 commit into
mainfrom
fix/scanresult-language-check-constraint

Conversation

@n1ckl0sk0rtge

Copy link
Copy Markdown
Contributor

Fixes #345.

Problem

ScanResult.language was an unannotated enum, so Hibernate stored it as an ordinal and generated a check constraint listing the languages that existed when the table was created. Databases created while Language had only JAVA(0) / PYTHON(1) still carry CHECK (language >= 0 AND language <= 1), and schema-management.strategy=update never rewrites a check constraint — so inserting GO (ordinal 2) fails.

This broke every scan, not just Go repositories: a ScanResult is written whenever the CBOM is non-null, and the Go scanner returns an empty CBOM even with zero Go projects, so the single transaction rolled back with the Java and Python results in it. ScanRepository.save swallowed the exception, so the scan still reported success over the WebSocket while ScanFinishedEvent was never emitted and nothing reached the read model.

Why not @Enumerated(EnumType.STRING)

It does not help. The constraint is emitted by EnumJavaType#getCheckCondition, keyed on the attribute's domain type being an enum rather than on the storage strategy — it would simply become check (language in ('JAVA','PYTHON','GO')) and break again on the next language. Its renderConvertedEnumCheckConstraint method means an AttributeConverter does not escape it either, and AvailableSettings has no global off-switch. Only a non-enum Java type keeps the set of languages out of the schema.

Changes

  • Map the column as String holding Language.name(), with a comment recording why it must not be "cleaned up" back into an enum
  • Skip unknown language names in Scan#asAggregate with a warning, so a language removed upstream cannot make stored scans unreadable
  • Let ScanRepository#save throw AggregatePersistenceFailed instead of swallowing failures, so ScanProcessManager dispatches an ERROR rather than reporting success. Every call site is already inside a try that dispatches ERROR, compensates and rethrows, so no changes were needed there
  • Add a round-trip test over Language.values(), so a language added to cbomkit-lib fails the build rather than production
  • Ship the one-off migration in migrations/, referenced from the README

Migration required

update cannot alter a column type or drop a constraint, so migrations/2026-08-17-scanresult-language-as-text.sql must be run once against every database created before Go support. Databases created after this change need nothing.

psql -U cbomkit -d postgres -f migrations/2026-08-17-scanresult-language-as-text.sql

Verification

  • Full suite passes (22/22), including the ArchUnit DomainTest
  • The new test is a genuine guard, not just a passing test: with a simulated stale constraint (CHECK (language IN ('JAVA','PYTHON'))) it errors. That run also confirmed the save change — it surfaced as an error propagating out of save, not a silent assertion failure
  • The async scan in ScanningResourceTest persisted a GO row (JAVA 74→75, PYTHON 72→73) against a migrated database — the exact insert that was failing
  • After Hibernate's schema update ran against the migrated database, no check constraint was recreated, which is the invariant this fix relies on

Deliberately out of scope

  • read and delete still swallow exceptions; read's empty-Optional contract is load-bearing for callers
  • ScanProcessManager still persists a ScanResult for an empty CBOM. Skipping empty results would have masked this bug rather than fixed it, so it is worth considering separately
  • Flyway. update cannot deploy constraint or column-type changes at all, so every future schema change needs a script like this one and a release note. Left for a separate discussion ([BUG] Stale scanresult_language_check constraint breaks all scans on pre-Go databases #345)

`ScanResult.language` was an unannotated enum, so Hibernate stored it as
an ordinal and generated a check constraint listing the languages that
existed when the table was created. Databases created while `Language`
had only JAVA and PYTHON still carry `CHECK (language >= 0 AND language
<= 1)`, and `schema-management.strategy=update` never rewrites a check
constraint, so inserting GO (ordinal 2) fails.

This broke every scan, not just Go repositories: a ScanResult is written
whenever the CBOM is non-null and the Go scanner returns an empty CBOM
even with zero Go projects, so the whole transaction rolled back with the
Java and Python results in it. `ScanRepository.save` swallowed the
exception, so the scan still reported success over the WebSocket while
ScanFinishedEvent was never emitted and nothing reached the read model.

`@Enumerated(STRING)` would not fix this: the constraint comes from
`EnumJavaType#getCheckCondition`, which fires for any enum typed
attribute including ones mapped through an AttributeConverter. Only a
non-enum Java type keeps the set of languages out of the schema.

- map the column as String holding Language.name()
- skip unknown language names in Scan#asAggregate with a warning instead
  of failing to reconstruct the whole aggregate
- let ScanRepository#save throw AggregatePersistenceFailed so the process
  manager dispatches an ERROR instead of reporting success
- add a round-trip test over Language.values(), so a language added to
  cbomkit-lib fails the build rather than production
- ship the one-off migration, which update cannot apply, in migrations/

Refs #345

@san-zrl san-zrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tx for spotting this. LGTM.

@san-zrl
san-zrl marked this pull request as ready for review September 4, 2026 12:44
@san-zrl
san-zrl requested a review from a team as a code owner September 4, 2026 12:44
@san-zrl
san-zrl merged commit 6f8883e into main Sep 4, 2026
2 of 3 checks passed
@san-zrl
san-zrl deleted the fix/scanresult-language-check-constraint branch September 4, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Stale scanresult_language_check constraint breaks all scans on pre-Go databases

2 participants