-- SaaS Spend Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/saas-spend-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. -- -- Money is integer cents. renews_on and ended_on are calendar dates with no time. -- Annualized cost, unused seat cost, notice deadline and reminder date are computed, never stored (section 5). -- A person on the team. Owners and admins are people. -- App rule (R16): at least one active admin must remain. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, -- unique ignoring case (index below) is_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE UNIQUE INDEX people_email_unique ON people (LOWER(email)); -- A department: the budget that pays for a tool. -- App rule (screen 10): a department with tools of any status can't be deleted. CREATE TABLE departments ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 2 AND 60) -- unique ignoring case (index below) ); CREATE UNIQUE INDEX departments_name_unique ON departments (LOWER(name)); -- One paid subscription or contract. -- App rule (R16): owner_id is an active person while the tool is active or cancelling. -- App rule (R18): every change increments version; a save with a stale version is refused. -- App rule (R19): cancelled tools are read-only until restored. CREATE TABLE tools ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (name = TRIM(name) AND LENGTH(name) BETWEEN 2 AND 100), -- trimmed; unique among current tools (index below) owner_id TEXT NOT NULL REFERENCES people(id), department_id TEXT NOT NULL REFERENCES departments(id), billing_cycle TEXT NOT NULL CHECK (billing_cycle IN ('monthly', 'quarterly', 'annual')), cycle_price_cents INTEGER NOT NULL CHECK (cycle_price_cents BETWEEN 0 AND 100000000), -- total per billing cycle for the whole subscription, never per seat renews_on DATE NOT NULL, -- end of the current term; for a cancelling tool, the day it ends auto_renew BOOLEAN NOT NULL DEFAULT TRUE, -- the vendor renews unless notice is given notice_days INTEGER NOT NULL DEFAULT 0 CHECK (notice_days BETWEEN 0 AND 365), -- notice the contract requires before renews_on seats_purchased INTEGER NOT NULL DEFAULT 0 CHECK (seats_purchased BETWEEN 0 AND 100000), -- 0 means not seat-based seats_used INTEGER NOT NULL DEFAULT 0 CHECK (seats_used >= 0), -- typed in by the owner status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'cancelling', 'cancelled')), status_reason TEXT CHECK (status_reason IS NULL OR LENGTH(status_reason) BETWEEN 3 AND 200), -- given when marked cancelling or cancelled ended_on DATE, -- set when the tool becomes cancelled link TEXT CHECK (link IS NULL OR (LENGTH(link) <= 500 AND (SUBSTR(link, 1, 7) = 'http://' OR SUBSTR(link, 1, 8) = 'https://'))), notes TEXT CHECK (notes IS NULL OR LENGTH(notes) <= 1000), version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CHECK (seats_used <= seats_purchased), -- R14 CHECK (status = 'active' OR status_reason IS NOT NULL), -- R19: a reason is required CHECK ((status = 'cancelled' AND ended_on IS NOT NULL) OR (status <> 'cancelled' AND ended_on IS NULL)) ); CREATE UNIQUE INDEX tools_current_name_unique ON tools (LOWER(name)) WHERE status IN ('active', 'cancelling'); -- R13: a cancelled tool's name can be reused CREATE INDEX tools_owner ON tools (owner_id); CREATE INDEX tools_department ON tools (department_id); CREATE INDEX tools_status_renews ON tools (status, renews_on); -- One change to a tool. Append-only, never updated or deleted (R17). CREATE TABLE history_events ( id TEXT PRIMARY KEY, tool_id TEXT NOT NULL REFERENCES tools(id), actor_id TEXT REFERENCES people(id), -- null means the daily job type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'renewed', 'auto_renewed', 'status_changed', 'reminder_queued')), source TEXT NOT NULL CHECK (source IN ('form', 'csv', 'job')), changes TEXT, -- JSON text: field -> {from, to}; a late cancellation also holds "late_notice": true reason TEXT, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX history_events_tool ON history_events (tool_id, created_at); -- One reminder or notice in the outbox, written before it is sent. -- App rule (section 11): after 5 failed attempts the status becomes failed. CREATE TABLE notifications ( id TEXT PRIMARY KEY, recipient_id TEXT NOT NULL REFERENCES people(id), kind TEXT NOT NULL CHECK (kind IN ('renewal_reminder', 'owner_assigned')), tool_id TEXT REFERENCES tools(id), -- null when one notice lists several tools dedupe_key TEXT UNIQUE, -- reminder:{tool_id}:{renews_on}:{notice_deadline}:{recipient_id} (R7) subject TEXT NOT NULL, body TEXT NOT NULL, -- the text as sent status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), -- logged: no email provider configured attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), last_error TEXT, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, sent_at TIMESTAMP ); CREATE INDEX notifications_recipient ON notifications (recipient_id, created_at); -- The single settings row. CREATE TABLE settings ( id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1), -- always 1, so there can only be one row currency_code TEXT NOT NULL DEFAULT 'USD', -- changes the symbol only; amounts are never converted time_zone TEXT NOT NULL DEFAULT 'UTC', -- IANA name; today is the date in this zone dev_clock_offset_days INTEGER NOT NULL DEFAULT 0 -- development only, ignored in production );