state_changes table: Replace JSONB columns with typed columns#476
Open
aditya1702 wants to merge 447 commits intomainfrom
Open
state_changes table: Replace JSONB columns with typed columns#476aditya1702 wants to merge 447 commits intomainfrom
state_changes table: Replace JSONB columns with typed columns#476aditya1702 wants to merge 447 commits intomainfrom
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Split trustline operations from AccountTokensModel into dedicated model. - TrustlineBalance struct (renamed from Trustline) - TrustlineBalanceModelInterface with GetByAccount, BatchUpsert, BatchInsert - Updated table name to account_trustline_balances
Split contract operations from AccountTokensModel into dedicated model. - AccountContractTokensModelInterface with GetByAccount, BatchInsert - Manages account_contracts junction table
- Renamed account_trustlines to account_trustline_balances - Merged balance columns from separate migration into main table creation - Removed redundant migration file (2026-01-13.0-add-trustline-balance-fields.sql)
Replace AccountTokens with: - TrustlineBalance (TrustlineBalanceModelInterface) - AccountContractTokens (AccountContractTokensModelInterface)
- account_trustline_balances.sql for TrustlineBalanceModel - account_contract_tokens.sql for AccountContractTokensModel - Removed combined migration file
- Replace accountTokensModel with trustlineBalanceModel and accountContractTokensModel - Update trustlineBatch to use TrustlineBalance struct - Update all model method calls to use new interfaces
- Replace AccountTokensReader with BalanceReader + AccountContractTokensModelInterface - Create balanceReaderAdapter to wrap TrustlineBalanceModelInterface - Update queries.resolvers.go to use new interface methods - Update accountKeyInfo to use TrustlineBalance type
- Replace AccountTokensModel with TrustlineBalanceModel and AccountContractTokensModel - Use NewBalanceReader adapter for GraphQL resolver injection
Pass TrustlineBalance and AccountContractTokens models to NewTokenIngestionService
There was a problem hiding this comment.
Pull request overview
This PR refactors the state_changes table schema by replacing JSONB columns with explicit typed columns to improve query performance, type safety, and storage efficiency. The changes maintain backward compatibility at the API layer by formatting the typed columns back to JSON in GraphQL resolvers.
Changes:
- Replaces JSONB/JSON columns (
flags,signer_weights,thresholds,trustline_limit) with typed columns (SMALLINT for numeric values, TEXT for limits) - Extracts entity identifiers (
claimable_balance_id,liquidity_pool_id,sponsored_data) from thekey_valueJSONB into dedicated columns - Implements bitmask encoding/decoding for flags instead of JSON arrays
- Updates GraphQL resolvers to format typed values as JSON for backward compatibility
- Removes the unused
offer_idcolumn - Fixes spelling of "authorized_to_maintain_liabilities" (was "authorized_to_maintain_liabilites" in several places)
Reviewed changes
Copilot reviewed 21 out of 22 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/db/migrations/2025-06-10.4-create_indexer_table_state_changes.sql | Defines new table schema with typed columns instead of JSONB |
| internal/indexer/types/types.go | Adds bitmask constants/helpers, removes NullableJSON, updates StateChange struct with typed fields |
| internal/indexer/processors/state_change_builder.go | Updates builder methods to accept typed values instead of JSONB maps |
| internal/indexer/processors/effects.go | Updates effects processor to use typed columns and new sponsorship field structure |
| internal/indexer/processors/effects_horizon.go | Fixes flag name spelling to "authorized_to_maintain_liabilities" |
| internal/indexer/processors/utils.go | Adds getContractIDFromAssetString helper for sponsorship asset conversion |
| internal/indexer/processors/contracts/sac.go | Fixes flag name spelling constant |
| internal/data/statechanges.go | Updates batch insert/copy logic for new column structure |
| internal/data/query_utils.go | Adds pgtypeInt2FromNullInt16 helper, removes jsonbFromSlice |
| internal/serve/graphql/schema/statechange.graphqls | Updates GraphQL schema to use new field names (removes keyValue, adds specific fields) |
| internal/serve/graphql/resolvers/statechange.resolvers.go | Implements resolvers that format typed values back to JSON for compatibility |
| internal/serve/graphql/resolvers/utils.go | Updates column mapping for old/new paired fields |
| internal/serve/graphql/generated/generated.go | Auto-generated code updates for schema changes |
| pkg/wbclient/types/types.go | Removes unused StateChange type |
| pkg/wbclient/types/statechange.go | Updates client types to match new GraphQL schema |
| pkg/wbclient/queries.go | Updates GraphQL queries to use new field names |
| internal/integrationtests/data_validation_test.go | Updates validation functions to use new typed fields |
| internal/serve/graphql/resolvers/statechange_resolvers_test.go | Updates unit tests for typed field resolvers |
| internal/indexer/processors/effects_test.go | Updates tests to use typed fields and bitmasks |
| internal/indexer/processors/contracts/sac_test.go | Updates tests to use bitmask values for flags |
| internal/data/statechanges_test.go | Updates test data generation with typed fields |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
state_changes table: Replace JSONB columns with typed columns
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
What
Refactors the
state_changestable schema to replace JSONB columns with explicit typed columns for better query performance, type safety and data integrity.Note that the API still returns a response as key-value JSON with
oldandnewkeys. Only the storage design has changedWhy
The previous schema used JSONB columns (
flags,key_value,signer_weights,thresholds,trustline_limit,offer_id) for storingvariable structured data. This approach had several drawbacks:
This PR introduces explicit typed columns:
signer_weight_old,signer_weight_newsigner_weightsJSONBthreshold_old,threshold_newthresholdsJSONBtrustline_limit_old,trustline_limit_newtrustline_limitJSONBflagsflagsJSON arrayclaimable_balance_id,liquidity_pool_id,sponsored_datakey_valueJSONBAdditionally:
offer_idcolumnkey_valueJSONB only for truly variable data (data entries, home domain) that cannot be flattened into a fixed schemaKnown limitations
N/A
Issue that this PR addresses
Closes #477