-- Equipment Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/equipment-tracker (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. -- A person who can sign in and hold equipment. People are deactivated, never deleted. -- App rule (R3): a person who holds assigned items cannot be deactivated; nobody can deactivate the last active IT admin. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase is_it_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); -- 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), company_name TEXT NOT NULL, it_email TEXT NOT NULL, -- shown on the found-item page time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; "today" and every date use this zone email_domain TEXT, -- the company sign-in domain (section 3) warranty_notice_days INTEGER NOT NULL DEFAULT 30 CHECK (warranty_notice_days BETWEEN 1 AND 365), tag_prefix TEXT NOT NULL DEFAULT 'NW-' CHECK (length(tag_prefix) BETWEEN 1 AND 10), -- app rule: letters, digits, ".", "-" or "_" only next_tag_number INTEGER NOT NULL DEFAULT 1 CHECK (next_tag_number >= 1) -- read-only on Settings ); -- A kind of item (Laptop, Monitor...). Never deleted; an inactive type cannot be chosen for new or imported items. CREATE TABLE item_types ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 40), active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE UNIQUE INDEX item_types_name_unique ON item_types (lower(name)); -- unique ignoring case -- A piece of equipment with a permanent tag. -- App rule (R1): tag is tag_prefix plus next_tag_number zero-padded to 4 digits, assigned at creation, never reused or changed. -- App rule (section 6): status changes only through the lifecycle actions; retired is final. -- App rule (R11): every IT action checks version first and adds 1 to it. CREATE TABLE items ( id TEXT PRIMARY KEY, tag TEXT NOT NULL UNIQUE, -- e.g. NW-0042 type_id TEXT NOT NULL REFERENCES item_types(id), make TEXT NOT NULL CHECK (length(make) BETWEEN 1 AND 60), model TEXT NOT NULL CHECK (length(model) BETWEEN 1 AND 80), serial_number TEXT CHECK (length(serial_number) <= 80), purchase_date DATE, cost_cents INTEGER CHECK (cost_cents BETWEEN 0 AND 10000000), -- $0 to $100,000; NULL means cost unknown supplier TEXT CHECK (length(supplier) <= 80), warranty_end DATE, status TEXT NOT NULL DEFAULT 'in_stock' CHECK (status IN ('in_stock', 'assigned', 'in_repair', 'retired', 'lost')), holder_id TEXT REFERENCES people(id), -- set if and only if status is assigned (R2) location TEXT CHECK (length(location) <= 80), -- free text, e.g. "London office" notes TEXT CHECK (length(notes) <= 1000), retire_reason TEXT CHECK (length(retire_reason) BETWEEN 5 AND 200), created_at TIMESTAMP NOT NULL, version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1), CHECK ((status = 'assigned' AND holder_id IS NOT NULL) OR (status <> 'assigned' AND holder_id IS NULL)), CHECK ((status = 'retired' AND retire_reason IS NOT NULL) OR (status <> 'retired' AND retire_reason IS NULL)), CHECK (warranty_end IS NULL OR purchase_date IS NULL OR warranty_end >= purchase_date) ); -- R4: a serial is unique among items that are not retired, compared trimmed and ignoring case. CREATE UNIQUE INDEX items_serial_unique ON items (lower(trim(serial_number))) WHERE status <> 'retired' AND serial_number IS NOT NULL; CREATE INDEX items_holder ON items (holder_id); -- One period an item was held by one person. Open while checked_in_at is NULL. CREATE TABLE assignments ( id TEXT PRIMARY KEY, item_id TEXT NOT NULL REFERENCES items(id), person_id TEXT NOT NULL REFERENCES people(id), checked_out_at TIMESTAMP NOT NULL, checked_out_by TEXT NOT NULL REFERENCES people(id), condition_out TEXT CHECK (length(condition_out) <= 200), checked_in_at TIMESTAMP, checked_in_by TEXT REFERENCES people(id), condition_in TEXT CHECK (length(condition_in) <= 200), -- the repair note when sent to repair; "Lost" when marked lost CHECK ((checked_in_at IS NULL AND checked_in_by IS NULL) OR (checked_in_at IS NOT NULL AND checked_in_by IS NOT NULL)), CHECK (checked_in_at IS NULL OR checked_in_at >= checked_out_at) ); CREATE UNIQUE INDEX assignments_one_open ON assignments (item_id) WHERE checked_in_at IS NULL; -- at most one open assignment per item -- A round of asking every holder to confirm the items they have (R10). CREATE TABLE asset_checks ( id TEXT PRIMARY KEY, started_at TIMESTAMP NOT NULL, started_by TEXT NOT NULL REFERENCES people(id), due_date DATE NOT NULL, -- the start date plus 7 days in the team time zone; also the reminder day closed_at TIMESTAMP ); CREATE UNIQUE INDEX asset_checks_one_open ON asset_checks ((closed_at IS NULL)) WHERE closed_at IS NULL; -- only one open check at a time -- One item to confirm in a check, addressed to the person who held it when the check started. -- App rule (R10): answers are final. CREATE TABLE check_responses ( id TEXT PRIMARY KEY, check_id TEXT NOT NULL REFERENCES asset_checks(id), person_id TEXT NOT NULL REFERENCES people(id), item_id TEXT NOT NULL REFERENCES items(id), answer TEXT NOT NULL DEFAULT 'pending' CHECK (answer IN ('pending', 'have_it', 'dont_have_it')), note TEXT CHECK (length(note) <= 300), -- optional with have_it; 5 to 300 characters with dont_have_it answered_at TIMESTAMP, reminded_at TIMESTAMP, -- when the one reminder for this check went to this person UNIQUE (check_id, item_id), CHECK (answer <> 'dont_have_it' OR length(note) >= 5), CHECK ((answer = 'pending' AND answered_at IS NULL) OR (answer <> 'pending' AND answered_at IS NOT NULL)) ); -- One thing that happened, mostly to an item. Append-only (R13). CREATE TABLE events ( id TEXT PRIMARY KEY, item_id TEXT REFERENCES items(id), -- NULL for check_started and check_closed actor_id TEXT REFERENCES people(id), -- NULL for the system type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'checked_out', 'checked_in', 'sent_to_repair', 'back_from_repair', 'retired', 'marked_lost', 'found', 'imported', 'problem_reported', 'check_started', 'check_answered', 'check_closed', 'warranty_reminded')), data TEXT, -- JSON stored as text created_at TIMESTAMP NOT NULL ); CREATE INDEX events_item ON events (item_id, created_at); -- One email in the outbox. Written first, then sent; a failure never blocks the action. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, to_email TEXT NOT NULL, person_id TEXT REFERENCES people(id), item_id TEXT REFERENCES items(id), subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')), error TEXT, -- the last error of a failed send created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP, CHECK (status <> 'logged' OR sent_at IS NULL) -- a logged email has no sent_at ); CREATE INDEX notifications_status ON notifications (status);