]> Untitled Git - lemmy.git/blob - migrations/2023-07-26-222023_site-aggregates-one/up.sql
add enable_federated_downvotes site option
[lemmy.git] / migrations / 2023-07-26-222023_site-aggregates-one / up.sql
1 CREATE OR REPLACE FUNCTION site_aggregates_site ()
2     RETURNS TRIGGER
3     LANGUAGE plpgsql
4     AS $$
5 BEGIN
6     -- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
7     -- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
8     IF (TG_OP = 'INSERT') AND NOT EXISTS (
9     SELECT
10         id
11     FROM
12         site_aggregates
13     LIMIT 1) THEN
14         INSERT INTO site_aggregates (site_id)
15             VALUES (NEW.id);
16     ELSIF (TG_OP = 'DELETE') THEN
17         DELETE FROM site_aggregates
18         WHERE site_id = OLD.id;
19     END IF;
20     RETURN NULL;
21 END
22 $$;
23
24 DELETE FROM site_aggregates a
25 WHERE NOT EXISTS (
26         SELECT
27             id
28         FROM
29             local_site s
30         WHERE
31             s.site_id = a.site_id);
32