Skip to content

Commit

Permalink
trigger fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Feb 10, 2025
1 parent 068aded commit a88845e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
11 changes: 4 additions & 7 deletions crates/db_schema/replaceable_schema/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
--
--
-- Create triggers for both post and comments
CREATE FUNCTION r.creator_id_from_post (agg post)
RETURNS int IMMUTABLE PARALLEL SAFE RETURN agg.creator_id;

CREATE PROCEDURE r.post_or_comment (table_name text)
LANGUAGE plpgsql
AS $a$
Expand All @@ -31,18 +28,18 @@ BEGIN
CALL r.create_triggers ('thing_actions', $$
BEGIN
WITH thing_diff AS ( UPDATE
thing_aggregates AS a
thing AS a
SET
score = a.score + diff.upvotes - diff.downvotes, upvotes = a.upvotes + diff.upvotes, downvotes = a.downvotes + diff.downvotes, controversy_rank = r.controversy_rank ((a.upvotes + diff.upvotes)::numeric, (a.downvotes + diff.downvotes)::numeric)
FROM (
SELECT
(thing_actions).thing_id, coalesce(sum(count_diff) FILTER (WHERE (thing_actions).like_score = 1), 0) AS upvotes, coalesce(sum(count_diff) FILTER (WHERE (thing_actions).like_score != 1), 0) AS downvotes FROM select_old_and_new_rows AS old_and_new_rows
WHERE (thing_actions).like_score IS NOT NULL GROUP BY (thing_actions).thing_id) AS diff
WHERE
a.thing_id = diff.thing_id
a.id = diff.thing_id
AND (diff.upvotes, diff.downvotes) != (0, 0)
RETURNING
r.creator_id_from_thing_aggregates (a.*) AS creator_id, diff.upvotes - diff.downvotes AS score)
a.creator_id AS creator_id, diff.upvotes - diff.downvotes AS score)
UPDATE
person_aggregates AS a
SET
Expand Down Expand Up @@ -947,7 +944,7 @@ BEGIN
SET
score = NEW.score
WHERE
post_id = NEW.post_id;
post_id = NEW.id;
RETURN NULL;
END
$$;
Expand Down
3 changes: 3 additions & 0 deletions crates/db_schema/replaceable_schema/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ DECLARE
CREATE TRIGGER delete_statement
AFTER DELETE ON thing REFERENCING OLD TABLE AS select_old_rows
FOR EACH STATEMENT
WHEN (pg_trigger_depth( ) = 0 )
EXECUTE FUNCTION r.thing_delete_statement ( );
-- Insert
CREATE FUNCTION r.thing_insert_statement ( )
Expand All @@ -101,6 +102,7 @@ DECLARE
CREATE TRIGGER insert_statement
AFTER INSERT ON thing REFERENCING NEW TABLE AS select_new_rows
FOR EACH STATEMENT
WHEN (pg_trigger_depth( ) = 0 )
EXECUTE FUNCTION r.thing_insert_statement ( );
-- Update
CREATE FUNCTION r.thing_update_statement ( )
Expand All @@ -110,6 +112,7 @@ DECLARE
CREATE TRIGGER update_statement
AFTER UPDATE ON thing REFERENCING OLD TABLE AS select_old_rows NEW TABLE AS select_new_rows
FOR EACH STATEMENT
WHEN (pg_trigger_depth( ) = 0 )
EXECUTE FUNCTION r.thing_update_statement ( );
$$;
select_old_and_new_rows text := $$ (
Expand Down
6 changes: 4 additions & 2 deletions crates/db_schema/src/impls/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl Post {
))
.get_result::<Self>(conn)
.await
}
}
pub fn local_url(&self, settings: &Settings) -> LemmyResult<DbUrl> {
let domain = settings.get_protocol_and_hostname();
Ok(Url::parse(&format!("{domain}/post/{}", self.id))?.into())
Expand Down Expand Up @@ -575,7 +575,9 @@ mod tests {
instance_id: InstanceId(0),
newest_comment_time: Utc::now(),
newest_comment_time_necro: Utc::now(),
report_count: 0,scaled_rank: 0.0,unresolved_report_count: 0
report_count: 0,
scaled_rank: 0.0,
unresolved_report_count: 0,
};

// Post Like
Expand Down

0 comments on commit a88845e

Please sign in to comment.