2023-07-20 11:13:21 -04:00
|
|
|
-- Your SQL goes here
|
|
|
|
ALTER TABLE post_aggregates
|
|
|
|
ADD COLUMN community_id integer REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE,
|
|
|
|
ADD COLUMN creator_id integer REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE;
|
|
|
|
|
2023-08-02 12:44:51 -04:00
|
|
|
CREATE OR REPLACE FUNCTION post_aggregates_post ()
|
|
|
|
RETURNS TRIGGER
|
2023-07-20 11:13:21 -04:00
|
|
|
LANGUAGE plpgsql
|
2023-08-02 12:44:51 -04:00
|
|
|
AS $$
|
2023-07-20 11:13:21 -04:00
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
2023-08-02 12:44:51 -04:00
|
|
|
INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro, community_id, creator_id)
|
|
|
|
VALUES (NEW.id, NEW.published, NEW.published, NEW.published, NEW.community_id, NEW.creator_id);
|
2023-07-20 11:13:21 -04:00
|
|
|
ELSIF (TG_OP = 'DELETE') THEN
|
2023-08-02 12:44:51 -04:00
|
|
|
DELETE FROM post_aggregates
|
|
|
|
WHERE post_id = OLD.id;
|
2023-07-20 11:13:21 -04:00
|
|
|
END IF;
|
|
|
|
RETURN NULL;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
2023-08-02 12:44:51 -04:00
|
|
|
UPDATE
|
|
|
|
post_aggregates
|
|
|
|
SET
|
|
|
|
community_id = post.community_id,
|
|
|
|
creator_id = post.creator_id
|
|
|
|
FROM
|
|
|
|
post
|
|
|
|
WHERE
|
|
|
|
post.id = post_aggregates.post_id;
|
2023-07-20 11:13:21 -04:00
|
|
|
|
|
|
|
ALTER TABLE post_aggregates
|
|
|
|
ALTER COLUMN community_id SET NOT NULL,
|
2023-08-02 12:44:51 -04:00
|
|
|
ALTER COLUMN creator_id SET NOT NULL;
|
|
|
|
|