-- Contract Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/contract-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. -- 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, time_zone TEXT NOT NULL DEFAULT 'America/Chicago', -- IANA name; today, the daily job and every date use this zone notice_reminder_days TEXT NOT NULL DEFAULT '60,30,7', -- comma-separated days before a notice deadline; admins are copied on the smallest expiry_reminder_days TEXT NOT NULL DEFAULT '90,30' -- comma-separated days before the end date of a contract that does not auto-renew ); -- A person who signs in. Owner is not a role: a person owns the contracts whose owner_id is theirs. -- App rule (R8): a person cannot be deactivated while they own an active or ending contract. 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', 'member')), active BOOLEAN NOT NULL DEFAULT TRUE, calendar_token TEXT NOT NULL UNIQUE -- long random secret; the calendar feed is /calendar/.ics ); -- The other party to one or more contracts. Only admins create counterparties. CREATE TABLE counterparties ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, kind TEXT NOT NULL CHECK (kind IN ('customer', 'supplier', 'landlord', 'insurer', 'other')), contact_email TEXT ); -- One contract and its current term. Only admins create contracts and change owner_id or confidential. -- App rule (section 6): the notice deadline is end_date minus notice_days for auto-renewing contracts; it is computed, never stored. -- App rule (R3): the roll-over starts a new term the day after end_date unless the decision is cancel, and clears decision, decision_note, notice_sent_on and late_notice. -- App rule (section 7): once renew or cancel is recorded only an admin Undo clears it; an ended contract takes no decision. -- App rule (R5): confidential contracts are visible only to their owner and admins, everywhere. -- App rule: number is the next unused C- number and is never reused. CREATE TABLE contracts ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 2) = 'C-'), -- e.g. C-001 title TEXT NOT NULL, counterparty_id TEXT NOT NULL REFERENCES counterparties(id), type TEXT NOT NULL, -- free text, such as Customer agreement, Services, Lease, NDA, Insurance, Software owner_id TEXT NOT NULL REFERENCES people(id), confidential BOOLEAN NOT NULL DEFAULT FALSE, tags TEXT, -- comma-separated free-text tags; the tag filter matches one whole tag, ignoring case annual_value_cents INTEGER NOT NULL DEFAULT 0 CHECK (annual_value_cents >= 0), -- 0 for no value start_date DATE NOT NULL, -- start of the current term end_date DATE, -- end of the current term; NULL for evergreen contracts auto_renew BOOLEAN NOT NULL DEFAULT FALSE, renewal_term_months INTEGER CHECK (renewal_term_months BETWEEN 1 AND 60), notice_days INTEGER CHECK (notice_days BETWEEN 0 AND 365), status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'ending', 'ended')), decision TEXT NOT NULL DEFAULT 'none' CHECK (decision IN ('none', 'renew', 'renegotiate', 'cancel')), decision_note TEXT, notice_sent_on DATE, -- the day notice was sent to the counterparty; required to record cancel late_notice BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE when cancel was confirmed with notice after the notice deadline CHECK (end_date IS NULL OR end_date > start_date), CHECK (auto_renew = FALSE OR (end_date IS NOT NULL AND renewal_term_months IS NOT NULL)), CHECK (status <> 'ending' OR decision = 'cancel'), CHECK (decision <> 'cancel' OR (status IN ('ending', 'ended') AND notice_sent_on IS NOT NULL AND decision_note IS NOT NULL)), CHECK (decision <> 'renegotiate' OR decision_note IS NOT NULL), CHECK (decision <> 'renew' OR auto_renew = TRUE), CHECK (late_notice = FALSE OR (decision = 'cancel' AND auto_renew = TRUE)) ); CREATE INDEX contracts_owner ON contracts (owner_id); CREATE INDEX contracts_counterparty ON contracts (counterparty_id); CREATE INDEX contracts_end_date ON contracts (status, end_date); -- One uploaded PDF on a contract: the original, an amendment or a renewal. -- App rule (R6): the declared type is application/pdf, the file starts with %PDF- and the name ends in .pdf; only the owner or an admin uploads. CREATE TABLE documents ( id TEXT PRIMARY KEY, contract_id TEXT NOT NULL REFERENCES contracts(id) ON DELETE CASCADE, label TEXT NOT NULL, -- e.g. Original, Amendment 1, Renewal 2027 storage_key TEXT NOT NULL UNIQUE, -- where the file lives in private storage original_filename TEXT NOT NULL, size_bytes INTEGER NOT NULL CHECK (size_bytes BETWEEN 0 AND 26214400), -- at most 25 MB (25 x 1024 x 1024) uploaded_at TIMESTAMP NOT NULL, uploaded_by TEXT NOT NULL REFERENCES people(id) ); CREATE INDEX documents_contract ON documents (contract_id); -- One reminder recorded for one key date, so each is sent once. -- App rule (section 6): when several reminders for one key date are due on one run, only the nearest is sent and the others are recorded here as covered. CREATE TABLE reminders_sent ( contract_id TEXT NOT NULL REFERENCES contracts(id) ON DELETE CASCADE, kind TEXT NOT NULL CHECK (kind IN ('notice', 'expiry')), days_before INTEGER NOT NULL CHECK (days_before >= 0), -- one of the settings reminder days for_date DATE NOT NULL, -- the key date: the notice deadline or the end date sent_on DATE NOT NULL, -- the company date of the daily run that recorded it PRIMARY KEY (contract_id, kind, days_before, for_date) ); -- One term of a contract. Append-only history. -- App rule (R10): terms are never edited or deleted. CREATE TABLE terms ( id TEXT PRIMARY KEY, contract_id TEXT NOT NULL REFERENCES contracts(id) ON DELETE CASCADE, start_date DATE NOT NULL, end_date DATE, -- NULL for an evergreen contract how TEXT NOT NULL CHECK (how IN ('signed', 'auto_renewed', 'extended')), -- extended: an edit moved the end date later, start = old end + 1 day CHECK (end_date IS NULL OR end_date >= start_date) ); CREATE INDEX terms_contract ON terms (contract_id, start_date); -- 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, -- a person id, or the daily job type TEXT NOT NULL, -- e.g. decision_recorded, rolled_over contract_id TEXT REFERENCES contracts(id), -- NULL for events that are not about one contract data TEXT -- details of the change as JSON text ); CREATE INDEX events_contract ON events (contract_id, at); -- One email to one address in the outbox. An email to several people is one row per address. -- App rule (section 10): a failed email never blocks the action that caused it. CREATE TABLE notifications ( id TEXT PRIMARY KEY, created_at TIMESTAMP NOT NULL, -- the Outbox lists newest first kind TEXT NOT NULL, -- which trigger in section 10, e.g. notice_reminder, renewed, late_notice 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 );