Skip to content

Fix primary key column order in ETL migrations#209

Merged
rickyrombo merged 3 commits intomainfrom
mjp-fix-migration-pkeys
Apr 24, 2026
Merged

Fix primary key column order in ETL migrations#209
rickyrombo merged 3 commits intomainfrom
mjp-fix-migration-pkeys

Conversation

@rickyrombo
Copy link
Copy Markdown
Contributor

@rickyrombo rickyrombo commented Apr 23, 2026

Summary

  • Reorders primary key columns in 9 ETL migration tables to match the production DP schema: entity ID first, txhash last
  • This ensures the btree index is useful for entity lookups (which filter by entity ID, not txhash), avoiding sequential scans on large tables

Tables fixed: users, tracks, playlists, follows, saves, reposts, developer_apps, grants

Test plan

  • Verified column orders match production schema
  • Run ETL from clean DB to confirm migrations apply cleanly

🤖 Generated with Claude Code

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>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 and txhash last.
  • Updated dashboard_wallet_users primary key to include user_id before wallet.

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.

Comment thread pkg/etl/db/sql/migrations/0012_dashboard_wallet_user.up.sql Outdated
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>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_users primary key to (user_id, wallet) in 0012_dashboard_wallet_user.up.sql.
  • Updates DashboardWalletCreate to use ON 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.

Comment thread pkg/etl/db/sql/migrations/0002_entity_manager_tables.up.sql
Comment thread pkg/etl/processors/entity_manager/dashboard_wallet.go
Comment thread pkg/etl/processors/entity_manager/dashboard_wallet.go
Comment thread pkg/etl/db/sql/migrations/0012_dashboard_wallet_user.up.sql
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>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Comment thread pkg/etl/db/sql/migrations/0002_entity_manager_tables.up.sql
Comment thread pkg/etl/db/sql/migrations/0002_entity_manager_tables.up.sql
rickyrombo added a commit to AudiusProject/api that referenced this pull request Apr 24, 2026
## 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>
@rickyrombo rickyrombo merged commit 9d5b3c7 into main Apr 24, 2026
10 checks passed
@rickyrombo rickyrombo deleted the mjp-fix-migration-pkeys branch April 24, 2026 01:08
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.

2 participants