-
Notifications
You must be signed in to change notification settings - Fork 1
Backfill sol_purchases from usdc_purchases #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| BEGIN; | ||
| SET LOCAL statement_timeout = 0; | ||
|
|
||
| -- Parity with the legacy usdc_purchases.created_at column. The Go indexer | ||
| -- writes new rows close to on-chain time, so DEFAULT NOW() is acceptable; the | ||
| -- backfill below corrects rows that came from the legacy table. | ||
| ALTER TABLE sol_purchases | ||
| ADD COLUMN IF NOT EXISTS created_at TIMESTAMP DEFAULT NOW(); | ||
|
|
||
| -- Backfill historical purchases that predate the Go indexer. from_account is | ||
| -- resolved via the buyer's USDC user_bank so the NOT NULL column has a real | ||
| -- value. Falls back to '' for buyers whose bank account is unknown — the | ||
| -- sol_purchases_from_account_idx tolerates empty strings. | ||
| INSERT INTO sol_purchases ( | ||
| signature, instruction_index, amount, slot, | ||
| from_account, content_type, content_id, buyer_user_id, | ||
| access_type, valid_after_blocknumber, is_valid, | ||
| city, region, country, created_at | ||
| ) | ||
| SELECT | ||
| up.signature, | ||
| 0 AS instruction_index, | ||
| up.amount, | ||
| up.slot, | ||
| COALESCE(uuba.bank_account, '') AS from_account, | ||
| up.content_type::text, | ||
| up.content_id, | ||
| up.buyer_user_id, | ||
| up.access::text, | ||
| 0 AS valid_after_blocknumber, | ||
| TRUE AS is_valid, | ||
| up.city, up.region, up.country, | ||
| up.created_at | ||
| FROM usdc_purchases up | ||
| LEFT JOIN users u | ||
| ON u.user_id = up.buyer_user_id AND u.is_current = TRUE | ||
| LEFT JOIN usdc_user_bank_accounts uuba | ||
| ON uuba.ethereum_address = u.wallet | ||
| ON CONFLICT (signature, instruction_index) DO NOTHING; | ||
|
|
||
| -- Correct created_at for rows the Go indexer wrote before this migration ran: | ||
| -- those rows got NOW() from the column default, but the legacy table has the | ||
| -- real on-chain time. Only updates rows whose existing created_at is later | ||
| -- than the legacy value, so it leaves accurate Go-indexer writes alone. | ||
| UPDATE sol_purchases sp | ||
| SET created_at = up.created_at | ||
| FROM usdc_purchases up | ||
| WHERE up.signature = sp.signature | ||
| AND up.created_at < sp.created_at; | ||
|
|
||
| -- Explode legacy usdc_purchases.splits JSONB into sol_payments rows. The | ||
| -- element shape is {payout_wallet, amount, percentage, user_id, eth_wallet} | ||
| -- per add_wallet_info_to_splits() in | ||
| -- discovery-provider/src/queries/get_extended_purchase_gate.py. | ||
| INSERT INTO sol_payments (signature, instruction_index, route_index, to_account, amount, slot) | ||
| SELECT | ||
| up.signature, | ||
| 0 AS instruction_index, | ||
| (ord - 1)::int AS route_index, | ||
| elem->>'payout_wallet' AS to_account, | ||
| (elem->>'amount')::bigint AS amount, | ||
| up.slot | ||
| FROM usdc_purchases up | ||
| CROSS JOIN LATERAL jsonb_array_elements(up.splits) WITH ORDINALITY arr(elem, ord) | ||
| WHERE elem->>'payout_wallet' IS NOT NULL | ||
| ON CONFLICT (signature, instruction_index, route_index) DO NOTHING; | ||
|
|
||
| -- Default sort across the purchases / sales / library routes is by created_at; | ||
| -- restore the index parity the legacy table had via idx_usdc_purchases_created_at. | ||
| CREATE INDEX IF NOT EXISTS sol_purchases_created_at_idx | ||
| ON sol_purchases (created_at); | ||
|
|
||
| COMMIT; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| DROP VIEW IF EXISTS v_usdc_purchases; | ||
| CREATE VIEW v_usdc_purchases AS | ||
| SELECT | ||
| sp.signature, | ||
| sp.slot, | ||
| sp.buyer_user_id, | ||
| CASE sp.content_type | ||
| WHEN 'track' THEN t.owner_id | ||
| WHEN 'album' THEN p.playlist_owner_id | ||
| WHEN 'playlist' THEN p.playlist_owner_id | ||
| END AS seller_user_id, | ||
| sp.amount, | ||
| sp.content_type::usdc_purchase_content_type AS content_type, | ||
| sp.content_id, | ||
| sp.created_at, | ||
| GREATEST( | ||
| sp.amount - COALESCE( | ||
| CASE sp.content_type | ||
| WHEN 'track' THEN ( | ||
| SELECT tph.total_price_cents * 10000 | ||
| FROM track_price_history tph | ||
| WHERE tph.track_id = sp.content_id | ||
| AND tph.block_timestamp <= sp.created_at | ||
| ORDER BY tph.block_timestamp DESC | ||
| LIMIT 1 | ||
| ) | ||
| ELSE ( | ||
| SELECT aph.total_price_cents * 10000 | ||
| FROM album_price_history aph | ||
| WHERE aph.playlist_id = sp.content_id | ||
| AND aph.block_timestamp <= sp.created_at | ||
| ORDER BY aph.block_timestamp DESC | ||
| LIMIT 1 | ||
| ) | ||
| END, | ||
| 0 | ||
| ), | ||
| 0 | ||
| ) AS extra_amount, | ||
| sp.access_type::usdc_purchase_access_type AS access, | ||
| sp.city, sp.region, sp.country, | ||
| ( | ||
| SELECT COALESCE( | ||
| jsonb_agg( | ||
| jsonb_build_object( | ||
| 'user_id', COALESCE(u_payout.user_id, u_sca.user_id), | ||
| 'payout_wallet', pay.to_account, | ||
| 'amount', pay.amount, | ||
| 'percentage', pay.amount * 100.0 / NULLIF(sp.amount, 0) | ||
| ) | ||
| ORDER BY pay.route_index | ||
| ), | ||
| '[]'::jsonb | ||
| ) | ||
| FROM sol_payments pay | ||
| LEFT JOIN users u_payout | ||
| ON u_payout.spl_usdc_payout_wallet = pay.to_account | ||
| AND u_payout.is_current = TRUE | ||
| LEFT JOIN sol_claimable_accounts sca | ||
| ON sca.account = pay.to_account | ||
| AND sca.mint = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' | ||
| LEFT JOIN users u_sca | ||
| ON u_sca.wallet = sca.ethereum_address | ||
| AND u_sca.is_current = TRUE | ||
| WHERE pay.signature = sp.signature | ||
| AND pay.instruction_index = sp.instruction_index | ||
| ) AS splits | ||
| FROM sol_purchases sp | ||
| LEFT JOIN tracks t | ||
| ON sp.content_type = 'track' | ||
| AND t.track_id = sp.content_id | ||
| AND t.is_current = TRUE | ||
| LEFT JOIN playlists p | ||
| ON sp.content_type IN ('album', 'playlist') | ||
| AND p.playlist_id = sp.content_id | ||
| AND p.is_current = TRUE | ||
| WHERE sp.is_valid IS TRUE; | ||
|
|
||
| COMMENT ON VIEW v_usdc_purchases IS 'Compatibility view exposing sol_purchases + sol_payments in the column shape API routes used to read from usdc_purchases. seller_user_id is the current content owner (not snapshotted at purchase time). extra_amount is amount paid minus base price from price history. vendor is intentionally dropped.'; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting is this not really important for ordering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it doesn't exist in production
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol