fix sql errors

This commit is contained in:
Dull Bananas 2023-12-30 19:11:03 +00:00
parent 8d1c633378
commit 0ca591e0ec
3 changed files with 101 additions and 67 deletions

View file

@ -657,6 +657,25 @@ BEGIN
END END
$$; $$;
CREATE FUNCTION site_aggregates_community_delete ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
IF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
UPDATE
site_aggregates sa
SET
communities = communities - 1
FROM
site s
WHERE
sa.site_id = s.id;
END IF;
RETURN NULL;
END
$$;
CREATE FUNCTION site_aggregates_community_insert () CREATE FUNCTION site_aggregates_community_insert ()
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE plpgsql LANGUAGE plpgsql

View file

@ -1,5 +1,5 @@
-- Drop functions and use `CASCADE` to drop the triggers that use them -- Drop functions and use `CASCADE` to drop the triggers that use them
DROP FUNCTION comment_aggregates_comment, comment_aggregates_score, comment_removed_resolve_reports, community_aggregates_comment_count, community_aggregates_community, community_aggregates_post_count, community_aggregates_post_count_insert, community_aggregates_subscriber_count, person_aggregates_comment_count, person_aggregates_comment_score, person_aggregates_person, person_aggregates_post_count, person_aggregates_post_insert, person_aggregates_post_score, post_aggregates_comment_count, post_aggregates_featured_community, post_aggregates_featured_local, post_aggregates_post, post_aggregates_score, post_removed_resolve_reports, site_aggregates_comment_delete, site_aggregates_comment_insert, site_aggregates_community_insert, site_aggregates_person_delete, site_aggregates_person_insert, site_aggregates_post_delete, site_aggregates_post_insert, site_aggregates_post_update, site_aggregates_site, was_removed_or_deleted, was_restored_or_created CASCADE; DROP FUNCTION comment_aggregates_comment, comment_aggregates_score, comment_removed_resolve_reports, community_aggregates_comment_count, community_aggregates_community, community_aggregates_post_count, community_aggregates_post_count_insert, community_aggregates_subscriber_count, person_aggregates_comment_count, person_aggregates_comment_score, person_aggregates_person, person_aggregates_post_count, person_aggregates_post_insert, person_aggregates_post_score, post_aggregates_comment_count, post_aggregates_featured_community, post_aggregates_featured_local, post_aggregates_post, post_aggregates_score, post_removed_resolve_reports, site_aggregates_comment_delete, site_aggregates_comment_insert, site_aggregates_community_delete, site_aggregates_community_insert, site_aggregates_person_delete, site_aggregates_person_insert, site_aggregates_post_delete, site_aggregates_post_insert, site_aggregates_post_update, site_aggregates_site, was_removed_or_deleted, was_restored_or_created CASCADE;
-- Fix values that might be incorrect because of the old triggers -- Fix values that might be incorrect because of the old triggers
UPDATE UPDATE

View file

@ -44,9 +44,9 @@ $$;
-- * Transition tables are only provided to the trigger function, not to functions that it calls. -- * Transition tables are only provided to the trigger function, not to functions that it calls.
-- --
-- This function can only be called once per table. The command to run is given as the 2nd argument -- This function can only be called once per table. The command to run is given as the 2nd argument
-- and has access to these tables: -- and can use these tables in `FROM` clauses:
-- * `old_table` with old rows -- * `old_table` with old rows (alias that doesn't contain `old_table` must be added)
-- * `new_table` with new rows -- * `new_table` with new rows (alias that doesn't contain `new_table` must be added)
-- * `combined_transition_tables` with both old and new rows, with 2 columns: -- * `combined_transition_tables` with both old and new rows, with 2 columns:
-- 1. `count_diff`: `-1` for old rows and `1` for new rows, which can be used with `sum` to get the number -- 1. `count_diff`: `-1` for old rows and `1` for new rows, which can be used with `sum` to get the number
-- to add to a count -- to add to a count
@ -61,52 +61,65 @@ DECLARE
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
delete_command; delete_command;
RETURN NULL; RETURN NULL;
END $$; END $$;
CREATE TRIGGER delete_statement CREATE TRIGGER delete_statement
AFTER DELETE ON thing REFERENCING OLD TABLE AS old_table AFTER DELETE ON thing REFERENCING OLD TABLE AS old_table
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION r.thing_delete_statement ( ); EXECUTE FUNCTION r.thing_delete_statement ( );
-- Insert -- Insert
CREATE FUNCTION r.thing_insert_statement ( ) CREATE FUNCTION r.thing_insert_statement ( )
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
insert_command; insert_command;
RETURN NULL; RETURN NULL;
END $$; END $$;
CREATE TRIGGER insert_statement CREATE TRIGGER insert_statement
AFTER INSERT ON thing REFERENCING NEW TABLE AS new_table AFTER INSERT ON thing REFERENCING NEW TABLE AS new_table
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION r.thing_insert_statement ( ); EXECUTE FUNCTION r.thing_insert_statement ( );
-- Update -- Update
CREATE FUNCTION r.thing_update_statement ( ) CREATE FUNCTION r.thing_update_statement ( )
RETURNS TRIGGER RETURNS TRIGGER
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
update_command; update_command;
RETURN NULL; RETURN NULL;
END $$; END $$;
CREATE TRIGGER update_statement CREATE TRIGGER update_statement
AFTER UPDATE ON thing REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table AFTER UPDATE ON thing REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION r.thing_update_statement ( ); EXECUTE FUNCTION r.thing_update_statement ( );
$b$; $b$;
BEGIN BEGIN
-- Couldn't get `combined_transition_tables` to work using CTE -- Couldn't get these to work using CTE
defs := replace(defs, 'delete_command', replace(command, 'combined_transition_tables', '(select_old_table) AS combined_transition_tables')); command := replace(command, 'combined_transition_tables', $$ (
defs := replace(defs, 'insert_command', replace(command, 'combined_transition_tables', '(select_new_table) AS combined_transition_tables')); SELECT
defs := replace(defs, 'update_command', replace(command, 'combined_transition_tables', '(select_old_table UNION ALL select_new_table) AS combined_transition_tables')); -1 AS count_diff, old_t::thing AS thing
defs := replace(defs, 'select_old_table', $$ FROM old_table AS old_t
UNION ALL
SELECT SELECT
-1 AS count_diff, old_table::thing AS thing FROM old_table $$); 1 AS count_diff, new_t::thing AS thing
defs := replace(defs, 'select_new_table', $$ FROM new_table AS new_t) AS combined_transition_tables $$);
SELECT -- `new_table` and `old_table` are made available as empty tables if they don't already exist
1 AS count_diff, new_table::thing AS thing FROM new_table $$); defs := replace(defs, 'delete_command', replace(command, 'new_table', $$ (
SELECT
*
FROM old_table
WHERE
FALSE) $$));
defs := replace(defs, 'insert_command', replace(command, 'old_table', $$ (
SELECT
*
FROM new_table
WHERE
FALSE) $$));
defs := replace(defs, 'update_command', command);
defs := replace(defs, 'thing', table_name); defs := replace(defs, 'thing', table_name);
EXECUTE defs; EXECUTE defs;
END END
@ -141,15 +154,18 @@ BEGIN
UPDATE UPDATE
thing_report thing_report
SET SET
resolved = TRUE, resolver_id = first_removal.mod_person_id, updated = first_removal.when_ FROM ( resolved = TRUE, resolver_id = first_removal.mod_person_id, updated = first_removal.when_ FROM ( SELECT DISTINCT
SELECT thing_id
thing_id, min(when_) AS when_ FROM new_removal FROM new_removal) AS removal_group, LATERAL (
WHERE SELECT
new_removal.removed GROUP BY thing_id) AS first_removal *
WHERE FROM new_removal
report.thing_id = first_removal.thing_id WHERE
AND NOT report.resolved new_removal.thing_id = removal_group.thing_id ORDER BY when_ ASC LIMIT 1) AS first_removal
AND COALESCE(report.updated < first_removal.when_, TRUE); WHERE
thing_report.thing_id = first_removal.thing_id
AND NOT thing_report.resolved
AND COALESCE(thing_report.updated < first_removal.when_, TRUE);
RETURN NULL; RETURN NULL;
END $$; END $$;
CREATE TRIGGER resolve_reports CREATE TRIGGER resolve_reports
@ -163,17 +179,17 @@ BEGIN
score = a.score + diff.upvotes - diff.downvotes, upvotes = a.upvotes + diff.upvotes, downvotes = a.downvotes + diff.downvotes, controversy_rank = controversy_rank ((a.upvotes + diff.upvotes)::numeric, (a.downvotes + diff.downvotes)::numeric) score = a.score + diff.upvotes - diff.downvotes, upvotes = a.upvotes + diff.upvotes, downvotes = a.downvotes + diff.downvotes, controversy_rank = controversy_rank ((a.upvotes + diff.upvotes)::numeric, (a.downvotes + diff.downvotes)::numeric)
FROM ( FROM (
SELECT SELECT
(thing_like).thing_id, sum(count_diff) FILTER (WHERE (thing_like).score = 1) AS upvotes, sum(count_diff) FILTER (WHERE (thing_like).score != 1) AS downvotes FROM combined_transition_tables GROUP BY (thing_like).thing_id) AS diff (thing_like).thing_id, coalesce(sum(count_diff) FILTER (WHERE (thing_like).score = 1), 0) AS upvotes, coalesce(sum(count_diff) FILTER (WHERE (thing_like).score != 1), 0) AS downvotes FROM combined_transition_tables GROUP BY (thing_like).thing_id) AS diff
WHERE WHERE
a.thing_id = diff.thing_id a.thing_id = diff.thing_id
RETURNING RETURNING
creator_id_from_thing_aggregates (a.*) AS creator_id, diff.upvotes - diff.downvotes AS score) r.creator_id_from_thing_aggregates (a.*) AS creator_id, diff.upvotes - diff.downvotes AS score)
UPDATE UPDATE
person_aggregates AS a person_aggregates AS a
SET SET
thing_score = a.thing_score + diff.score FROM ( thing_score = a.thing_score + diff.score FROM (
SELECT SELECT
creator_id, sum(score) AS score FROM target_diff GROUP BY creator_id) AS diff creator_id, sum(score) AS score FROM thing_diff GROUP BY creator_id) AS diff
WHERE WHERE
a.person_id = diff.creator_id $$); a.person_id = diff.creator_id $$);
$b$, $b$,
@ -192,7 +208,7 @@ CALL r.create_triggers ('comment', $$ WITH comment_group AS (
(comment).post_id, (comment).post_id,
(comment).creator_id, (comment).creator_id,
(comment).local, (comment).local,
sum(count_diff) AS comments FROM combined_transition_tables coalesce(sum(count_diff), 0) AS comments FROM combined_transition_tables
WHERE WHERE
NOT ((comment).deleted NOT ((comment).deleted
OR (comment).removed) OR (comment).removed)
@ -238,29 +254,28 @@ unused_person_aggregates_update_result AS (
AND a.published > (new_comment.published - '2 days'::interval) AND a.published > (new_comment.published - '2 days'::interval)
LIMIT 1)) LIMIT 1))
FROM FROM
comment_group, comment_group
LATERAL ( WHERE
a.post_id = comment_group.post_id
RETURNING
a.community_id,
comment_group.comments,
(
SELECT SELECT
* NOT (post.deleted
OR post.removed)
FROM FROM
post post
WHERE WHERE
a.post_id = post.id a.post_id = post.id
LIMIT 1) AS post LIMIT 1) AS include_in_community_aggregates)
WHERE
a.post_id = comment_group.post_id
RETURNING
a.community_id,
diff.comments,
NOT (post.deleted
OR post.removed) AS include_in_community_aggregates)
UPDATE UPDATE
community_aggregates AS a community_aggregates AS a
SET SET
comments = a.comments + diff.comments comments = a.comments + diff.comments
FROM ( FROM (
SELECT SELECT
community_id, sum(comments) community_id, sum(comments) AS comments
FROM FROM
post_diff post_diff
WHERE WHERE
@ -272,7 +287,7 @@ unused_person_aggregates_update_result AS (
CALL r.create_triggers ('post', $$ WITH post_group AS ( CALL r.create_triggers ('post', $$ WITH post_group AS (
SELECT SELECT
(post).community_id, (post).creator_id, (post).local, sum(count_diff) AS posts FROM combined_transition_tables (post).community_id, (post).creator_id, (post).local, coalesce(sum(count_diff), 0) AS posts FROM combined_transition_tables
WHERE WHERE
NOT ((post).deleted NOT ((post).deleted
OR (post).removed) OR (post).removed)
@ -301,7 +316,7 @@ CALL r.create_triggers ('community', $$ UPDATE
SET SET
communities = a.communities + diff.communities FROM ( communities = a.communities + diff.communities FROM (
SELECT SELECT
sum(count_diff) AS communities FROM combined_transition_tables coalesce(sum(count_diff), 0) AS communities FROM combined_transition_tables
WHERE (community).local WHERE (community).local
AND NOT ((community).deleted AND NOT ((community).deleted
OR (community).removed)) AS diff $$); OR (community).removed)) AS diff $$);
@ -311,7 +326,7 @@ CALL r.create_triggers ('person', $$ UPDATE
SET SET
users = a.users + diff.users FROM ( users = a.users + diff.users FROM (
SELECT SELECT
sum(count_diff) AS users FROM combined_transition_tables coalesce(sum(count_diff), 0) AS users FROM combined_transition_tables
WHERE (person).local) AS diff $$); WHERE (person).local) AS diff $$);
-- For community_aggregates.comments, don't include comments of deleted or removed posts -- For community_aggregates.comments, don't include comments of deleted or removed posts
@ -365,9 +380,9 @@ CREATE TRIGGER comment_count
CALL r.create_triggers ('community_follower', $$ UPDATE CALL r.create_triggers ('community_follower', $$ UPDATE
community_aggregates AS a community_aggregates AS a
SET SET
subscriber = a.subscribers + diff.subscribers FROM ( subscribers = a.subscribers + diff.subscribers FROM (
SELECT SELECT
(community_follower).community_id, sum(count_diff) AS subscribers FROM combined_transition_tables (community_follower).community_id, coalesce(sum(count_diff), 0) AS subscribers FROM combined_transition_tables
WHERE ( WHERE (
SELECT SELECT
local local