mirror of
https://github.com/block/buzz.git
synced 2026-10-27 04:30:52 +00:00
29 lines
1.4 KiB
PL/PgSQL
29 lines
1.4 KiB
PL/PgSQL
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
|
|
$$;
|