Skip to content

Commit 7e1b6fb

Browse files
Remove expensive function in migration
1 parent 1e970b7 commit 7e1b6fb

File tree

1 file changed

+0
-55
lines changed

1 file changed

+0
-55
lines changed

supabase/migrations/20241119033709_change_affiliate_stats_table.sql

-55
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,6 @@ CREATE POLICY "Users can see own and referrals affiliate stats" ON affiliate_sta
2828
WHERE
2929
referrer_id = auth.uid() AND referred_id = affiliate_stats.user_id));
3030

31-
CREATE OR REPLACE FUNCTION initialize_send_plus_minus()
32-
RETURNS void
33-
LANGUAGE plpgsql
34-
SECURITY DEFINER
35-
SET search_path = public
36-
AS $$
37-
BEGIN
38-
UPDATE
39-
affiliate_stats a
40-
SET
41-
send_plus_minus =( WITH sent_amount AS(
42-
SELECT
43-
COALESCE(SUM(stt.v::numeric), 0) AS total
44-
FROM
45-
send_token_transfers stt
46-
INNER JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.f, 'hex'))::citext
47-
WHERE
48-
sa.user_id = a.user_id),
49-
received_amount AS(
50-
SELECT
51-
COALESCE(SUM(stt.v::numeric), 0) AS total
52-
FROM
53-
send_token_transfers stt
54-
INNER JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.t, 'hex'))::citext
55-
LEFT JOIN referrals r ON r.referrer_id = sa.user_id
56-
WHERE
57-
sa.user_id = a.user_id
58-
AND concat('0x', encode(stt.f, 'hex'))::citext NOT IN(
59-
SELECT
60-
sa2.address
61-
FROM
62-
send_accounts sa2
63-
INNER JOIN referrals r2 ON r2.referrer_id = sa2.user_id
64-
WHERE
65-
r2.referred_id = a.user_id))
66-
SELECT
67-
(
68-
SELECT
69-
total
70-
FROM
71-
sent_amount) -(
72-
SELECT
73-
total
74-
FROM
75-
received_amount));
76-
END;
77-
$$;
78-
79-
-- Execute the function
80-
SELECT
81-
initialize_send_plus_minus();
82-
83-
-- Drop the function after use
84-
DROP FUNCTION initialize_send_plus_minus();
85-
8631
CREATE OR REPLACE FUNCTION public.update_affiliate_stats_on_transfer()
8732
RETURNS TRIGGER
8833
LANGUAGE plpgsql

0 commit comments

Comments
 (0)