-- Maintenance Requests database schema -- Source: https://personalsoftwarefactory.com/blueprints/maintenance-requests (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. -- The single settings row. id is fixed at 1 so the table can never hold a second row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), org_name TEXT NOT NULL, time_zone TEXT NOT NULL DEFAULT 'America/Chicago', -- IANA name; today, due dates and reports use this zone due_days_high INTEGER NOT NULL DEFAULT 0 CHECK (due_days_high >= 0), -- days from today to due_on for a high priority work order (0 = due today) due_days_medium INTEGER NOT NULL DEFAULT 3 CHECK (due_days_medium >= 0), due_days_low INTEGER NOT NULL DEFAULT 7 CHECK (due_days_low >= 0), pm_lead_days INTEGER NOT NULL DEFAULT 7 CHECK (pm_lead_days >= 0) -- a schedule creates its work order this many days before next_due_on ); -- A staff member with an account. Requesters have no account and are not people. -- App rule (section 13): deactivating a technician unassigns their open work orders and their schedules. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase role TEXT NOT NULL CHECK (role IN ('admin', 'technician')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- A building or site, such as one restaurant. The public request form is /r/. CREATE TABLE locations ( id TEXT PRIMARY KEY, name TEXT NOT NULL, address TEXT ); -- A piece of equipment at a location, with a QR code that opens the request form for it. -- App rule (section 13): deactivating an asset pauses its schedules. CREATE TABLE assets ( id TEXT PRIMARY KEY, location_id TEXT NOT NULL REFERENCES locations(id), name TEXT NOT NULL, category TEXT, make_model TEXT, -- free text, may be blank serial_number TEXT, -- the spec field serial; renamed because SERIAL is a PostgreSQL type keyword. Free text, may be blank qr_code TEXT NOT NULL UNIQUE, -- random token; the QR label opens /a/ active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE INDEX assets_location ON assets (location_id); -- A named checklist that schedules and work orders copy their items from. CREATE TABLE checklist_templates ( id TEXT PRIMARY KEY, name TEXT NOT NULL ); -- One item of a checklist template, in order. Stores the template items list. CREATE TABLE checklist_template_items ( template_id TEXT NOT NULL REFERENCES checklist_templates(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position >= 1), label TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('check', 'number', 'pass_fail')), unit TEXT, -- number items only, e.g. degrees F min NUMERIC, -- number items only: a reading below this is flagged max NUMERIC, -- number items only: a reading above this is flagged PRIMARY KEY (template_id, position), CHECK (kind = 'number' OR (unit IS NULL AND min IS NULL AND max IS NULL)), CHECK (min IS NULL OR max IS NULL OR min <= max) ); -- A preventive maintenance schedule that creates work orders ahead of each due date. -- App rule (section 7): the daily job creates the next work order pm_lead_days before next_due_on; next_due_on moves only when the work order due on it is completed. -- App rule (section 7): fixed schedules advance from the previous due date, after_completion schedules from the completion date; months are calendar months. CREATE TABLE schedules ( id TEXT PRIMARY KEY, title TEXT NOT NULL, asset_id TEXT REFERENCES assets(id), location_id TEXT NOT NULL REFERENCES locations(id), checklist_template_id TEXT REFERENCES checklist_templates(id), -- optional; copied onto each work order every_n INTEGER NOT NULL CHECK (every_n >= 1), every_unit TEXT NOT NULL CHECK (every_unit IN ('days', 'weeks', 'months')), mode TEXT NOT NULL CHECK (mode IN ('fixed', 'after_completion')), next_due_on DATE NOT NULL, assignee_id TEXT REFERENCES people(id), -- NULL when unassigned priority TEXT NOT NULL CHECK (priority IN ('high', 'medium', 'low')), active BOOLEAN NOT NULL DEFAULT TRUE -- FALSE when paused ); -- A problem reported through the public form, waiting for triage or already handled. -- App rule (R1): requester_email must be a valid email; a request from a QR code gets the asset and its location. -- App rule (R9): numbers are sequential (REQ-0001, REQ-0002...) and never reused. CREATE TABLE requests ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 4) = 'REQ-'), -- e.g. REQ-0032 location_id TEXT NOT NULL REFERENCES locations(id), asset_id TEXT REFERENCES assets(id), title TEXT NOT NULL CHECK (length(title) BETWEEN 5 AND 120), description TEXT, requester_name TEXT NOT NULL, requester_email TEXT NOT NULL, status_token TEXT NOT NULL UNIQUE, -- random; the status page is /s/ status TEXT NOT NULL DEFAULT 'new' CHECK (status IN ('new', 'converted', 'declined')), decline_reason TEXT, work_order_id TEXT, -- the work order it became, or the one it was linked to as a duplicate. No foreign key: work_orders.request_id points back here (a cycle). created_at TIMESTAMP NOT NULL, CHECK ((status = 'converted') = (work_order_id IS NOT NULL)), CHECK ((status = 'declined') = (decline_reason IS NOT NULL)) ); CREATE INDEX requests_status ON requests (status); -- One photo attached to a request. Stores the request photos list. -- App rule (section 13): images only, at most 10 MB each. CREATE TABLE request_photos ( request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), -- at most 5 photos per request file_name TEXT NOT NULL UNIQUE, -- the random name the image is stored under PRIMARY KEY (request_id, position) ); -- A job to do: converted from a request, created by a schedule, or entered by staff. -- App rule (section 6): status moves only by Start, Hold, Resume, Complete and Reopen; Complete needs a completion note and every checklist item done or not applicable. -- App rule (R2): a done work order is read-only except an admin reopening it within 7 calendar days of the completion date. -- App rule (R3): due_on is today plus the priority due days from settings unless set by hand. -- App rule (R9): numbers are sequential (WO-0001, WO-0002...) and never reused. CREATE TABLE work_orders ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 3) = 'WO-'), -- e.g. WO-0105 title TEXT NOT NULL, description TEXT, location_id TEXT NOT NULL REFERENCES locations(id), asset_id TEXT REFERENCES assets(id), priority TEXT NOT NULL CHECK (priority IN ('high', 'medium', 'low')), assignee_id TEXT REFERENCES people(id), -- NULL when unassigned due_on DATE NOT NULL, -- overdue when not done and today is after this date status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'in_progress', 'on_hold', 'done')), hold_reason TEXT, -- e.g. waiting for part completion_note TEXT, -- required by the Complete action completed_at TIMESTAMP, request_id TEXT UNIQUE REFERENCES requests(id), -- the request it was converted from schedule_id TEXT REFERENCES schedules(id), -- the schedule that created it created_by TEXT REFERENCES people(id), -- NULL when the daily job created it from a schedule created_at TIMESTAMP NOT NULL, CHECK (status <> 'on_hold' OR hold_reason IS NOT NULL), CHECK (status <> 'done' OR completed_at IS NOT NULL) ); -- Section 7: a schedule never has two work orders that are not done. CREATE UNIQUE INDEX work_orders_one_open_per_schedule ON work_orders (schedule_id) WHERE status <> 'done' AND schedule_id IS NOT NULL; CREATE INDEX work_orders_assignee ON work_orders (assignee_id, status); CREATE INDEX work_orders_due ON work_orders (status, due_on); -- One checklist item on one work order, copied from a template or added to the work order. -- App rule (R4): a number reading outside min to max sets flagged and offers a follow-up work order. CREATE TABLE checklist_items ( work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position >= 1), label TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('check', 'number', 'pass_fail')), unit TEXT, min NUMERIC, max NUMERIC, value TEXT, -- the reading: a decimal number for number items, pass or fail for pass_fail items done BOOLEAN NOT NULL DEFAULT FALSE, flagged BOOLEAN NOT NULL DEFAULT FALSE, na_reason TEXT, -- set when the item is marked not applicable PRIMARY KEY (work_order_id, position), CHECK (kind = 'number' OR (unit IS NULL AND min IS NULL AND max IS NULL)), CHECK (min IS NULL OR max IS NULL OR min <= max), CHECK (kind <> 'pass_fail' OR value IS NULL OR value IN ('pass', 'fail')), CHECK (kind = 'number' OR flagged = FALSE) -- only number items are flagged ); -- Minutes one person spent on one work order. CREATE TABLE time_entries ( id TEXT PRIMARY KEY, work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE, person_id TEXT NOT NULL REFERENCES people(id), minutes INTEGER NOT NULL CHECK (minutes > 0), -- whole minutes note TEXT ); CREATE INDEX time_entries_work_order ON time_entries (work_order_id); -- A part used on one work order. Cost is quantity times unit_cost_cents. CREATE TABLE parts_used ( id TEXT PRIMARY KEY, work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE, name TEXT NOT NULL, quantity INTEGER NOT NULL CHECK (quantity > 0), unit_cost_cents INTEGER NOT NULL CHECK (unit_cost_cents >= 0) -- may be zero ); CREATE INDEX parts_used_work_order ON parts_used (work_order_id); -- A comment on a work order. Only public comments appear on the requester status page. CREATE TABLE comments ( id TEXT PRIMARY KEY, work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE, author_id TEXT NOT NULL REFERENCES people(id), body TEXT NOT NULL, at TIMESTAMP NOT NULL, public BOOLEAN NOT NULL DEFAULT FALSE -- TRUE when the author ticked Show to the requester ); CREATE INDEX comments_work_order ON comments (work_order_id); -- One photo attached to a comment. Stores the comment photos list. -- App rule (section 13): images only, at most 10 MB each. CREATE TABLE comment_photos ( comment_id TEXT NOT NULL REFERENCES comments(id) ON DELETE CASCADE, position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), -- at most 5 photos per comment file_name TEXT NOT NULL UNIQUE, -- the random name the image is stored under PRIMARY KEY (comment_id, position) ); -- One change, written by every action. Append-only. -- App rule (R10): events are never edited or deleted. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT NOT NULL, -- who made the change: a person id, the requester, or the daily job type TEXT NOT NULL, -- e.g. request_converted, work_order_completed data TEXT -- details of the change as JSON text ); -- One email in the outbox. Every email is written here first, then sent. -- App rule (R8): a failed email never blocks the action that caused it. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, -- which trigger in section 10, e.g. request_received, assigned, overdue_digest to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged when no email provider is configured error TEXT );