feat(db): constrain the network column to a typed value set (#252, #435) - #593
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reworked from #534 and #480. Both carried the right idea — stop a typo like
testnettfrom 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 (
mainnet→pubnet,standalone→local) and then constrained the columns to the new names. Nothing else in the repo moved with it:services/andcrates/write"mainnet"pubnetat all —crates/indexer/src/config.rs:442, and only as an accepted aliasMerging it would have migrated the stored data to a vocabulary the code never writes, so every subsequent
INSERTwould 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 by0027_webhook_secret_rotation.sql. This is0031—0030was claimed byfailed_events_dedupin the meantime.#480 replaced
crates/common/src/types.rswholesale instead of adding to it, deleting seven existing types and failing to compile (E0432onContractLiveness,SorobanEvent, and five others). Two further defects in the same file:FromStrwas implemented asfn fromStr, which does not satisfy the trait, and theFUTUREnetconstant broke naming convention. Its GoValidateNetworkalso collided with the existing query-param validator of the same name invalidators.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 todev's current schema.crates/common/src/types.rs—Networkenum appended, withfrom_strandFUTURENETcorrected.services/api/validation/network.go— renamed toValidateNetworkValue/TestValidateNetworkValueto clear the collision.Verification
All three layers agree on exactly one vocabulary —
mainnet,testnet,futurenet,sandbox:cargo clippy -p trident-common --all-targets -- -D warningspassesgo vet ./validation/andgo test ./validation/pass$$, 14 uniquely namedchk_*_networkconstraintsThe migration has not been run against a live Postgres locally — CI is the check for that.