Fix primary key column order in ETL migrations#209
Conversation
Put entity ID columns first and txhash last in all composite primary keys, matching the production DP schema. This ensures the btree index is useful for entity lookups (which filter by ID, not txhash). Tables fixed: - users: (txhash, user_id) → (user_id, txhash) - tracks: (txhash, track_id) → (track_id, txhash) - playlists: (txhash, playlist_id) → (playlist_id, txhash) - follows: (followee_user_id, txhash, follower_user_id) → (follower_user_id, followee_user_id, txhash) - saves: (save_item_id, user_id, txhash, save_type) → (user_id, save_item_id, save_type, txhash) - reposts: (txhash, user_id, repost_item_id, repost_type) → (user_id, repost_item_id, repost_type, txhash) - developer_apps: (txhash, address) → (address, txhash) - grants: (user_id, txhash, grantee_address) → (grantee_address, user_id, txhash) - dashboard_wallet_users: (wallet) → (user_id, wallet) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Reorders primary key column order in ETL SQL migrations to match the production Discovery Provider schema, aiming to make primary-key-backed btree indexes usable for common entity-ID lookup patterns.
Changes:
- Reordered composite primary keys for core entity-manager tables (
users,tracks,playlists,follows,saves,reposts,developer_apps,grants) to place entity identifiers first andtxhashlast. - Updated
dashboard_wallet_usersprimary key to includeuser_idbeforewallet.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/etl/db/sql/migrations/0002_entity_manager_tables.up.sql | Reorders composite PK column ordering across multiple entity-manager domain tables. |
| pkg/etl/db/sql/migrations/0012_dashboard_wallet_user.up.sql | Changes dashboard_wallet_users PK definition (now (user_id, wallet)). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Update ON CONFLICT (wallet) to ON CONFLICT (user_id, wallet) to match the corrected primary key. Also drop the redundant user_id SET since it's part of the conflict key and can't change. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Updates ETL schema migrations to match the production discovery-provider primary key column ordering (entity identifier(s) first, txhash last) so Postgres btree indexes are more useful for common entity lookups; also aligns the dashboard_wallet_users upsert conflict target with the new key definition.
Changes:
- Reorders primary key column order for several entity-manager domain tables in
0002_entity_manager_tables.up.sql(e.g.,users,tracks,playlists,follows,saves,reposts,developer_apps,grants). - Changes
dashboard_wallet_usersprimary key to(user_id, wallet)in0012_dashboard_wallet_user.up.sql. - Updates
DashboardWalletCreateto useON CONFLICT (user_id, wallet).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/etl/processors/entity_manager/dashboard_wallet.go | Updates dashboard wallet upsert conflict target to match new composite PK. |
| pkg/etl/db/sql/migrations/0012_dashboard_wallet_user.up.sql | Changes dashboard_wallet_users PK column order to (user_id, wallet). |
| pkg/etl/db/sql/migrations/0002_entity_manager_tables.up.sql | Reorders PK column order across multiple entity-manager tables to put entity IDs first and txhash last. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Production was updated to use PRIMARY KEY (wallet) with a separate index on user_id. Revert the migration and ON CONFLICT clause to match. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the ETL schema migrations to reorder composite primary key columns so that entity identifiers come first and txhash is last, improving btree index usefulness for common entity-lookup query patterns.
Changes:
- Reordered composite primary key column order for several entity-manager tables in migration
0002(e.g.,users,tracks,playlists,follows,saves,reposts,developer_apps,grants). - Aligned the PK leading columns with how the ETL queries these tables (typically filtering by entity ID / address).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
## Summary - Reorders primary key columns in 10 tables in `sql/01_schema.sql` to match production: entity ID first, txhash last - Mismatched PK order means the btree index can't be used for entity lookups, masking performance issues in tests that wouldn't exist in production Tables fixed: `users`, `tracks`, `playlists`, `follows`, `saves`, `reposts`, `developer_apps`, `grants`, `subscriptions`, `dashboard_wallet_users` Companion ETL migration PR: OpenAudio/go-openaudio#209 ## Test plan - [x] Verified column orders match production `pg_dump` output - [ ] Existing tests pass with updated schema 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Summary
Tables fixed:
users,tracks,playlists,follows,saves,reposts,developer_apps,grantsTest plan
🤖 Generated with Claude Code