-- Approval Workflow database schema -- Source: https://personalsoftwarefactory.com/blueprints/approval-workflow (data model, section 5 of the blueprint) -- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC. -- License: MIT. Free to copy and adapt. -- One row per person who can request, approve or administer. -- App rule (R11): reporting lines cannot loop; reject a manager change that makes someone their own manager, directly or indirectly. -- App rule (R8): inactive people cannot be assigned a step. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, manager_id TEXT REFERENCES people(id), -- who approves this person's requests first; empty for the top of the company is_admin BOOLEAN NOT NULL DEFAULT FALSE, -- manages people and rules; gives no approval power slack_user_id TEXT, -- filled from the email by a Slack lookup; empty means not linked out_of_office_until TIMESTAMP, -- while this is in the future, new steps go to the delegate delegate_id TEXT REFERENCES people(id), -- who acts for this person while they are out of office active BOOLEAN NOT NULL DEFAULT TRUE ); -- One row per routing rule in the approval matrix. A matching rule adds one step to the chain. -- App rule (R9): changing a rule affects only future submissions, never chains already in progress. CREATE TABLE rules ( id TEXT PRIMARY KEY, name TEXT NOT NULL, -- becomes the step name, for example Finance approver_id TEXT NOT NULL REFERENCES people(id), min_amount_cents INTEGER NOT NULL CHECK (min_amount_cents >= 0), -- the rule applies at or above this amount; 0 means any amount category TEXT CHECK (category IN ('Hardware', 'Software', 'Services', 'Travel', 'Other')), -- empty means every category position INTEGER NOT NULL, -- rules are evaluated in ascending position active BOOLEAN NOT NULL ); -- One row per spend request. -- App rule (R4): a request can be edited only in draft or changes_requested. -- App rule (R7): numbers are sequential and never reused. -- App rule (section 7): approved, rejected and cancelled are final; only comments are allowed after that. CREATE TABLE requests ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE, -- human-friendly and sequential: 201, 202, ... requester_id TEXT NOT NULL REFERENCES people(id), title TEXT NOT NULL CHECK (length(title) <= 120), category TEXT NOT NULL CHECK (category IN ('Hardware', 'Software', 'Services', 'Travel', 'Other')), amount_cents INTEGER NOT NULL CHECK (amount_cents > 0), -- integer cents in the one currency the admin set (R6) vendor TEXT, justification TEXT NOT NULL CHECK (length(justification) >= 10), needed_by DATE, link TEXT, -- URL of a quote, invoice or product page; v1 takes a link instead of a file upload status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'pending', 'changes_requested', 'approved', 'rejected', 'cancelled')), current_step_id TEXT, -- the active step in steps. No foreign key: requests and steps point at each other, and steps is created later created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, submitted_at TIMESTAMP, decided_at TIMESTAMP -- set on final approval, rejection or cancellation ); -- One row per approval step. The chain is rebuilt on every submission, so a resubmitted request gets new rows in a new round. -- App rule (R1): only the assignee of the active step can approve, reject or send back. -- App rule (R2): the assignee is never the requester; routing reassigns such a step and the action checks it again. -- App rule (R10): nobody approves the same request twice in one round; a later step for the same person is skipped. CREATE TABLE steps ( id TEXT PRIMARY KEY, request_id TEXT NOT NULL REFERENCES requests(id), round INTEGER NOT NULL DEFAULT 1 CHECK (round >= 1), -- starts at 1 and goes up by one each time the request is resubmitted position INTEGER NOT NULL CHECK (position >= 1), -- order inside the round: 1, 2, 3 name TEXT NOT NULL, -- Manager, or the name of the rule that added the step assignee_id TEXT NOT NULL REFERENCES people(id), -- who must act now original_assignee_id TEXT NOT NULL REFERENCES people(id), -- who the rules picked, before delegation, escalation or reassignment status TEXT NOT NULL DEFAULT 'waiting' CHECK (status IN ('waiting', 'active', 'approved', 'rejected', 'changes_requested', 'skipped', 'cancelled')), skip_reason TEXT, -- for example: Same approver as step 1 activated_at TIMESTAMP, -- the reminder and escalation clocks start here (R12 resets it) decided_at TIMESTAMP, last_reminded_at TIMESTAMP, escalated_at TIMESTAMP, UNIQUE (request_id, round, position) ); -- The audit log: one row per action. -- App rule (R5): every action writes exactly one row, and rows are never updated or deleted. Give the app no code path that does either. CREATE TABLE events ( id TEXT PRIMARY KEY, request_id TEXT NOT NULL REFERENCES requests(id), step_id TEXT REFERENCES steps(id), -- empty for actions on the whole request actor_id TEXT REFERENCES people(id), -- empty for system actions such as reminders and escalation type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'submitted', 'step_activated', 'approved', 'rejected', 'changes_requested', 'resubmitted', 'cancelled', 'commented', 'delegated', 'reminded', 'escalated', 'reassigned', 'skipped', 'completed')), comment TEXT, -- App rule (R3): at least 3 characters on rejected and changes_requested data TEXT, -- JSON as text, for example old and new amount on an edit, or from and to assignee created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); -- The notification outbox: one row per message to one person on one channel. Written first, then sent. -- App rule (section 10): a failed send keeps its row and its error and is retried on the next scheduled run; it never blocks an approval. CREATE TABLE notifications ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), -- the recipient channel TEXT NOT NULL CHECK (channel IN ('email', 'slack', 'in_app')), kind TEXT NOT NULL, -- which trigger in section 10 produced it, for example step_activated or reminder request_id TEXT NOT NULL REFERENCES requests(id), payload TEXT, -- JSON as text: what the message needs to render created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, sent_at TIMESTAMP, -- empty until the send succeeds error TEXT -- the last send error, if any ); -- Lists and filters behind the main screens. CREATE INDEX requests_requester_idx ON requests (requester_id); CREATE INDEX requests_status_idx ON requests (status); CREATE INDEX steps_request_idx ON steps (request_id, round, position); CREATE INDEX events_request_idx ON events (request_id, created_at); -- The chain is sequential: at most one step per request is active at a time. CREATE UNIQUE INDEX steps_one_active_idx ON steps (request_id) WHERE status = 'active'; -- The approvals inbox and the hourly reminder and escalation job read active steps only. CREATE INDEX steps_active_assignee_idx ON steps (assignee_id) WHERE status = 'active'; -- The retry job reads unsent notifications only. CREATE INDEX notifications_unsent_idx ON notifications (created_at) WHERE sent_at IS NULL;