Skip to content

feat(db): constrain the network column to a typed value set (#252, #435) - #593

Merged
Depo-dev merged 1 commit into
devfrom
integration/network-constraint
Aug 31, 2026
Merged

feat(db): constrain the network column to a typed value set (#252, #435)#593
Depo-dev merged 1 commit into
devfrom
integration/network-constraint

Conversation

@Depo-dev

Copy link
Copy Markdown
Collaborator

Reworked from #534 and #480. Both carried the right idea — stop a typo like testnett from silently splitting the dataset — but neither was mergeable as written.

Why the source PRs could not be merged directly

#534 did not only add constraints; it renamed the vocabulary across every table (mainnetpubnet, standalonelocal) and then constrained the columns to the new names. Nothing else in the repo moved with it:

  • 16 files under services/ and crates/ write "mainnet"
  • exactly one place mentions pubnet at all — crates/indexer/src/config.rs:442, and only as an accepted alias

Merging it would have migrated the stored data to a vocabulary the code never writes, so every subsequent INSERT would violate the constraints the same migration had just added. The rename is dropped here; the constraints keep the vocabulary the code actually uses.

It also numbered its migration 0027, already taken by 0027_webhook_secret_rotation.sql. This is 00310030 was claimed by failed_events_dedup in the meantime.

#480 replaced crates/common/src/types.rs wholesale instead of adding to it, deleting seven existing types and failing to compile (E0432 on ContractLiveness, SorobanEvent, and five others). Two further defects in the same file: FromStr was implemented as fn fromStr, which does not satisfy the trait, and the FUTUREnet constant broke naming convention. Its Go ValidateNetwork also collided with the existing query-param validator of the same name in validators.go, as did its test function.

What this branch does

  • database/migrations/0031_network_enum_constraint.sql — CHECK constraints on the 14 network-scoped tables, preceded by a normalisation pass so a database seeded by hand or by feat(db): store network as typed enum/constraint and validate on write (#252) #534's earlier draft is migrated safely rather than failing constraint validation.
  • database/schema.sql — the same constraints, appended to dev's current schema.
  • crates/common/src/types.rsNetwork enum appended, with from_str and FUTURENET corrected.
  • services/api/validation/network.go — renamed to ValidateNetworkValue / TestValidateNetworkValue to clear the collision.

Verification

All three layers agree on exactly one vocabulary — mainnet, testnet, futurenet, sandbox:

  • cargo clippy -p trident-common --all-targets -- -D warnings passes
  • go vet ./validation/ and go test ./validation/ pass
  • migration SQL structure checked: balanced $$, 14 uniquely named chk_*_network constraints

The migration has not been run against a live Postgres locally — CI is the check for that.

Reworked from #534 and #480, both of which were unmergeable as written.

#534 also renamed the vocabulary (mainnet -> pubnet, standalone -> local)
across every table. That rename is dropped: 16 files in services/ and crates/
write 'mainnet' and only the indexer config mentions 'pubnet' at all, as an
accepted alias — so migrating the data without changing those writers would
have made every subsequent INSERT violate the new constraints. It also
numbered its migration 0027, already taken by webhook_secret_rotation; this
is 0031, the next free slot.

#480's crates/common/src/types.rs replaced the file rather than adding to it,
deleting seven existing types (E0432 on ContractLiveness, SorobanEvent and
five others). The Network enum is appended here instead. Its FromStr impl
declared 'fn fromStr', which does not satisfy the trait, and the FUTUREnet
constant broke naming convention; both fixed.

#480's Go ValidateNetwork collided with the existing query-param validator of
the same name in validators.go, as did its test function. Renamed to
ValidateNetworkValue / TestValidateNetworkValue.

The migration normalises any legacy pubnet/standalone/local rows before adding
the constraints, so it is safe against a database seeded by the earlier draft.

All three layers now agree on mainnet/testnet/futurenet/sandbox:
cargo clippy -p trident-common --all-targets -- -D warnings passes,
go vet and go test ./validation/ pass.
@Depo-dev
Depo-dev merged commit 0b4a80c into dev Aug 31, 2026
22 checks passed
@Depo-dev
Depo-dev deleted the integration/network-constraint branch August 31, 2026 14:41
Depo-dev added a commit that referenced this pull request Aug 31, 2026
- database/migrations/0031 + schema.sql: both branches created a 0031
  network-constraint migration. Kept dev's (via #593): #592's allowed only
  ('mainnet','testnet'), which would reject the futurenet and sandbox values
  the Network enum and the rest of the schema accept.
- streamer: both implement reorg detection for #196. Kept dev's
  check_and_handle_reorg (from #535) — it enforces max_reorg_depth and has a
  passing integration test — and dropped #592's detect_and_repair_reorg,
  its end-to-end test, and its now-unused make_event/mock_ledger_hash helpers.
- db/mod.rs: removed #592's recent_ledger_hashes and rewind_for_reorg with
  their tests. get_recent_ledger_metadata covers the first and the rollback
  inside check_and_handle_reorg covers the second.
- metrics.rs: both defined REORGS_TOTAL and record_reorg. Kept #592's
  documented versions, dropped the terse duplicates.
- .env.example: kept the idempotency encryption key and dropped the API_KEY
  entry, which documents a fallback #385 removed — auth.go now reads only
  API_KEY_HASHES and API_KEY_SALT. Corrected the stale comments too.

cargo clippy -p trident-indexer --all-targets -- -D warnings, go build and
go vet all pass.
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.

1 participant