]> Untitled Git - lemmy.git/blob - migrations/2022-11-20-032430_sticky_local/up.sql
Add support for Featured Posts (#2585)
[lemmy.git] / migrations / 2022-11-20-032430_sticky_local / up.sql
1
2 DROP TRIGGER IF EXISTS post_aggregates_stickied ON post;
3 drop function 
4   post_aggregates_stickied;
5
6
7 alter table post ADD featured_community boolean NOT NULL DEFAULT false;
8 alter table post ADD featured_local boolean NOT NULL DEFAULT false;
9 update post
10 set featured_community = stickied;
11 alter table post DROP COLUMN stickied;
12
13 alter table post_aggregates ADD featured_community boolean NOT NULL DEFAULT false;
14 alter table post_aggregates ADD featured_local boolean NOT NULL DEFAULT false;
15 update post_aggregates
16 set featured_community = stickied;
17 alter table post_aggregates DROP COLUMN stickied;
18
19 alter table mod_sticky_post
20 rename column stickied TO featured;
21
22 alter table mod_sticky_post
23 alter column featured SET NOT NULL;
24
25 alter table mod_sticky_post
26 ADD is_featured_community boolean NOT NULL DEFAULT true;
27
28 alter table mod_sticky_post
29 Rename To mod_feature_post;
30
31 create function post_aggregates_featured_community()
32 returns trigger language plpgsql
33 as $$
34 begin
35   update post_aggregates pa
36   set featured_community = NEW.featured_community
37   where pa.post_id = NEW.id;
38   return null;
39 end $$;
40
41 create function post_aggregates_featured_local()
42 returns trigger language plpgsql
43 as $$
44 begin
45   update post_aggregates pa
46   set featured_local = NEW.featured_local
47   where pa.post_id = NEW.id;
48   return null;
49 end $$;
50
51 CREATE TRIGGER post_aggregates_featured_community
52     AFTER UPDATE 
53     ON public.post
54     FOR EACH ROW
55     WHEN (old.featured_community IS DISTINCT FROM new.featured_community)
56     EXECUTE FUNCTION public.post_aggregates_featured_community();
57
58 CREATE TRIGGER post_aggregates_featured_local
59     AFTER UPDATE 
60     ON public.post
61     FOR EACH ROW
62     WHEN (old.featured_local IS DISTINCT FROM new.featured_local)
63     EXECUTE FUNCTION public.post_aggregates_featured_local();