lemmy/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/down.sql
İsmail Karslı fc0b7a9638
sql format
2023-11-16 00:06:25 +03:00

35 lines
841 B
PL/PgSQL

ALTER TABLE community_aggregates
DROP COLUMN local_subscribers;
CREATE OR REPLACE FUNCTION community_aggregates_subscriber_count ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
UPDATE
community_aggregates
SET
subscribers = subscribers + 1
FROM
community
WHERE
community.id = community_id
AND community.local
AND community_id = NEW.community_id;
ELSIF (TG_OP = 'DELETE') THEN
UPDATE
community_aggregates
SET
subscribers = subscribers - 1
FROM
community
WHERE
community.id = community_id
AND community.local
AND community_id = OLD.community_id;
END IF;
RETURN NULL;
END
$$;