]> Untitled Git - lemmy.git/blob - migrations/2023-07-26-222023_site-aggregates-one/up.sql
Delete migrations_testing folder (#3751)
[lemmy.git] / migrations / 2023-07-26-222023_site-aggregates-one / up.sql
1 create or replace function site_aggregates_site()
2 returns trigger language plpgsql
3 as $$
4 begin
5   -- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
6   -- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
7   IF (TG_OP = 'INSERT') AND NOT EXISTS (select id from site_aggregates limit 1) THEN
8     insert into site_aggregates (site_id) values (NEW.id);
9   ELSIF (TG_OP = 'DELETE') THEN
10     delete from site_aggregates where site_id = OLD.id;
11   END IF;
12   return null;
13 end $$;
14
15 delete from site_aggregates a where not exists (select id from local_site s where s.site_id = a.site_id);