Files
lincodex/backend/db/migrations/00018_create_account_deletion_requests.sql
T

38 lines
1.5 KiB
SQL

-- A C1 revocation creates a durable request for the account-deletion workflow.
-- The API records the request and its outbox intent atomically; processing and
-- deleting the account are separate operations.
-- +goose Up
CREATE TABLE account_deletion_requests (
id uuid NOT NULL,
subject_id uuid,
status text NOT NULL,
requested_at timestamptz NOT NULL,
completed_at timestamptz,
CONSTRAINT account_deletion_requests_pkey PRIMARY KEY (id),
CONSTRAINT account_deletion_requests_subject_id_fkey FOREIGN KEY (subject_id)
REFERENCES users (id) ON DELETE SET NULL,
CONSTRAINT account_deletion_requests_status_known CHECK (
status IN ('requested', 'processing', 'completed')
),
CONSTRAINT account_deletion_requests_state_shape CHECK (
(status = 'completed') = (completed_at IS NOT NULL)
),
CONSTRAINT account_deletion_requests_subject_retained_until_complete CHECK (
subject_id IS NOT NULL OR status = 'completed'
),
CONSTRAINT account_deletion_requests_completed_after_request CHECK (
completed_at IS NULL OR completed_at >= requested_at
)
);
CREATE UNIQUE INDEX account_deletion_requests_one_active_per_subject
ON account_deletion_requests (subject_id)
WHERE subject_id IS NOT NULL AND status IN ('requested', 'processing');
CREATE INDEX account_deletion_requests_processing_idx
ON account_deletion_requests (requested_at, id)
WHERE status IN ('requested', 'processing');
-- +goose Down
DROP TABLE account_deletion_requests;