Skip to content
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

Remove expensive function in migration #890

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,61 +28,6 @@ CREATE POLICY "Users can see own and referrals affiliate stats" ON affiliate_sta
WHERE
referrer_id = auth.uid() AND referred_id = affiliate_stats.user_id));

CREATE OR REPLACE FUNCTION initialize_send_plus_minus()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
UPDATE
affiliate_stats a
SET
send_plus_minus =( WITH sent_amount AS(
SELECT
COALESCE(SUM(stt.v::numeric), 0) AS total
FROM
send_token_transfers stt
INNER JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.f, 'hex'))::citext
WHERE
sa.user_id = a.user_id),
received_amount AS(
SELECT
COALESCE(SUM(stt.v::numeric), 0) AS total
FROM
send_token_transfers stt
INNER JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.t, 'hex'))::citext
LEFT JOIN referrals r ON r.referrer_id = sa.user_id
WHERE
sa.user_id = a.user_id
AND concat('0x', encode(stt.f, 'hex'))::citext NOT IN(
SELECT
sa2.address
FROM
send_accounts sa2
INNER JOIN referrals r2 ON r2.referrer_id = sa2.user_id
WHERE
r2.referred_id = a.user_id))
SELECT
(
SELECT
total
FROM
sent_amount) -(
SELECT
total
FROM
received_amount));
END;
$$;

-- Execute the function
SELECT
initialize_send_plus_minus();

-- Drop the function after use
DROP FUNCTION initialize_send_plus_minus();

CREATE OR REPLACE FUNCTION public.update_affiliate_stats_on_transfer()
RETURNS TRIGGER
LANGUAGE plpgsql
Expand Down
Loading