mirror of
https://github.com/block/buzz.git
synced 2026-10-27 12:40:30 +00:00
refactor(schema): fan desired-state schema out to one file per table (#8131)
This commit is contained in:
28
schema/functions/enforce_community_tombstone.sql
Normal file
28
schema/functions/enforce_community_tombstone.sql
Normal file
@@ -0,0 +1,28 @@
|
||||
CREATE FUNCTION enforce_community_tombstone() RETURNS TRIGGER
|
||||
LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
executor_community TEXT := current_setting('buzz.deletion_executor_community', true);
|
||||
executor_generation TEXT := current_setting('buzz.deletion_fence_generation', true);
|
||||
expected_generation BIGINT;
|
||||
BEGIN
|
||||
IF TG_OP = 'DELETE' THEN
|
||||
IF OLD.deletion_state <> 'active' OR OLD.deleted_at IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'community tombstones are permanent'
|
||||
USING ERRCODE = 'object_not_in_prerequisite_state';
|
||||
END IF;
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
expected_generation := CASE WHEN NEW.deletion_fence_generation > OLD.deletion_fence_generation
|
||||
THEN NEW.deletion_fence_generation ELSE OLD.deletion_fence_generation END;
|
||||
IF executor_community = OLD.id::text AND executor_generation ~ '^[0-9]+$'
|
||||
AND executor_generation::BIGINT = expected_generation THEN RETURN NEW; END IF;
|
||||
IF OLD.deletion_state <> 'active' OR NEW.deletion_state <> OLD.deletion_state
|
||||
OR NEW.deletion_fence_generation <> OLD.deletion_fence_generation
|
||||
OR NEW.deleted_at IS DISTINCT FROM OLD.deleted_at THEN
|
||||
RAISE EXCEPTION 'community tombstone mutation rejected: community % generation %',
|
||||
OLD.id, OLD.deletion_fence_generation
|
||||
USING ERRCODE = 'object_not_in_prerequisite_state';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END
|
||||
$$;
|
||||
Reference in New Issue
Block a user