mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-01-23 10:25:56 +00:00
Prevent incorrectly using delete instead of uplete (#5331)
* implement check and test it by modifying PostLike::remove * revert change in PostLike::remove
This commit is contained in:
parent
edb063f288
commit
6f05254aae
2 changed files with 42 additions and 1 deletions
|
@ -889,3 +889,41 @@ CALL r.create_inbox_combined_trigger ('person_post_mention');
|
|||
|
||||
CALL r.create_inbox_combined_trigger ('private_message');
|
||||
|
||||
-- Prevent using delete instead of uplete on action tables
|
||||
CREATE FUNCTION r.require_uplete ()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF pg_trigger_depth() = 1 AND NOT starts_with (current_query(), '/**/') THEN
|
||||
RAISE 'using delete instead of uplete is not allowed for this table';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END
|
||||
$$;
|
||||
|
||||
CREATE TRIGGER require_uplete
|
||||
BEFORE DELETE ON comment_actions
|
||||
FOR EACH STATEMENT
|
||||
EXECUTE FUNCTION r.require_uplete ();
|
||||
|
||||
CREATE TRIGGER require_uplete
|
||||
BEFORE DELETE ON community_actions
|
||||
FOR EACH STATEMENT
|
||||
EXECUTE FUNCTION r.require_uplete ();
|
||||
|
||||
CREATE TRIGGER require_uplete
|
||||
BEFORE DELETE ON instance_actions
|
||||
FOR EACH STATEMENT
|
||||
EXECUTE FUNCTION r.require_uplete ();
|
||||
|
||||
CREATE TRIGGER require_uplete
|
||||
BEFORE DELETE ON person_actions
|
||||
FOR EACH STATEMENT
|
||||
EXECUTE FUNCTION r.require_uplete ();
|
||||
|
||||
CREATE TRIGGER require_uplete
|
||||
BEFORE DELETE ON post_actions
|
||||
FOR EACH STATEMENT
|
||||
EXECUTE FUNCTION r.require_uplete ();
|
||||
|
||||
|
|
|
@ -121,6 +121,9 @@ impl QueryFragment<Pg> for UpleteQuery {
|
|||
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> Result<(), Error> {
|
||||
assert_ne!(self.set_null_columns.len(), 0, "`set_null` was not called");
|
||||
|
||||
// This is checked by require_uplete triggers
|
||||
out.push_sql("/**/");
|
||||
|
||||
// Declare `update_keys` and `delete_keys` CTEs, which select primary keys
|
||||
for (prefix, subquery) in [
|
||||
("WITH update_keys", &self.update_subquery),
|
||||
|
@ -357,7 +360,7 @@ mod tests {
|
|||
let update_count = "SELECT count(*) FROM update_result";
|
||||
let delete_count = "SELECT count(*) FROM delete_result";
|
||||
|
||||
format!(r#"WITH {with_queries} SELECT ({update_count}), ({delete_count}) -- binds: []"#)
|
||||
format!(r#"/**/WITH {with_queries} SELECT ({update_count}), ({delete_count}) -- binds: []"#)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue