-- Timesheets database schema -- Source: https://personalsoftwarefactory.com/blueprints/timesheets (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 logs time. manager_id is the approver of their weeks (R6). -- App rule: no manager loops. App rule (R12): nobody deactivates themselves or the last active admin. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80), email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase manager_id TEXT REFERENCES people(id) CHECK (manager_id <> id), -- never themselves is_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE, weekly_capacity_minutes INTEGER NOT NULL DEFAULT 2400 CHECK (weekly_capacity_minutes BETWEEN 0 AND 10080) -- 0 to 168 hours; only for the "under hours" note in reminders ); -- 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, email_domain TEXT NOT NULL, -- sign-in is limited to this domain time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; weeks run Monday to Sunday in this zone reminder_weekday TEXT NOT NULL DEFAULT 'Friday' CHECK (reminder_weekday IN ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')), reminder_time TEXT NOT NULL DEFAULT '15:00' -- 24-hour HH:MM in the team time zone ); -- A client that projects bill to. Archiving a client archives its projects (R5). CREATE TABLE clients ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE CHECK (length(name) BETWEEN 1 AND 80), archived BOOLEAN NOT NULL DEFAULT FALSE ); -- A project people log time to. Internal projects have no client. -- App rule: owner_id must be an active person. CREATE TABLE projects ( id TEXT PRIMARY KEY, client_id TEXT REFERENCES clients(id), -- NULL for internal projects name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80), owner_id TEXT NOT NULL REFERENCES people(id), -- sees all hours and gets budget warnings billable BOOLEAN NOT NULL, rate_cents INTEGER CHECK (rate_cents BETWEEN 0 AND 100000), -- per hour, $0.00 to $1,000.00; NULL for non-billable projects budget_minutes INTEGER CHECK (budget_minutes > 0 AND budget_minutes % 15 = 0), -- 0.25 h steps; NULL means no budget archived BOOLEAN NOT NULL DEFAULT FALSE, warned_80_at TIMESTAMP, -- when the 80% budget warning went (R10) warned_100_at TIMESTAMP, -- when the 100% budget warning went (R10) UNIQUE (client_id, name), -- unique within the client CHECK ((billable = TRUE AND rate_cents IS NOT NULL) OR (billable = FALSE AND rate_cents IS NULL)) ); CREATE UNIQUE INDEX projects_internal_name_unique ON projects (name) WHERE client_id IS NULL; -- unique among internal projects too -- A person assigned to a project. Only assigned people can log time to it (R2). CREATE TABLE assignments ( project_id TEXT NOT NULL REFERENCES projects(id), person_id TEXT NOT NULL REFERENCES people(id), PRIMARY KEY (project_id, person_id) ); -- The timesheet of one person for one week. A missing row means a draft week with no entries. -- App rule: week_start is always a Monday. App rule (R6): approver_id is set at submission and never the person. -- App rule: submitting a week with no entries needs empty_note. CREATE TABLE weeks ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), week_start DATE NOT NULL, -- the Monday status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'submitted', 'approved')), submitted_at TIMESTAMP, approved_at TIMESTAMP, approved_by TEXT REFERENCES people(id), approver_id TEXT REFERENCES people(id), -- the only person who can approve or reject this submission empty_note TEXT CHECK (length(empty_note) BETWEEN 5 AND 200), -- e.g. "On leave" reminded_on DATE, -- the date of the last reminder about this week (R11) version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1), -- R13: up by 1 on every change to the week or its entries UNIQUE (person_id, week_start), UNIQUE (id, person_id), -- lets entries check that their person owns the week CHECK (approver_id <> person_id), CHECK ((status = 'draft' AND submitted_at IS NULL AND approver_id IS NULL AND approved_at IS NULL AND approved_by IS NULL) OR (status = 'submitted' AND submitted_at IS NOT NULL AND approver_id IS NOT NULL AND approved_at IS NULL AND approved_by IS NULL) OR (status = 'approved' AND submitted_at IS NOT NULL AND approver_id IS NOT NULL AND approved_at IS NOT NULL AND approved_by IS NOT NULL)) ); -- Time on one project on one day. Locked while its week is submitted or approved (R3). -- App rule (R1): the total of a person for one date is at most 1,440 minutes, and dates cannot be after today. -- App rule (R4): date falls inside its week. CREATE TABLE entries ( id TEXT PRIMARY KEY, week_id TEXT NOT NULL, person_id TEXT NOT NULL, project_id TEXT NOT NULL REFERENCES projects(id), date DATE NOT NULL, minutes INTEGER NOT NULL CHECK (minutes BETWEEN 15 AND 1440 AND minutes % 15 = 0), note TEXT CHECK (length(note) <= 300), rate_cents INTEGER CHECK (rate_cents BETWEEN 0 AND 100000), -- copied from the project when the week is approved (R9); NULL before FOREIGN KEY (week_id, person_id) REFERENCES weeks(id, person_id) ); CREATE INDEX entries_project_date ON entries (project_id, date); -- One action, kept for the activity log. Append-only (R14). CREATE TABLE events ( id TEXT PRIMARY KEY, actor_id TEXT REFERENCES people(id), -- NULL for the system week_id TEXT REFERENCES weeks(id), project_id TEXT REFERENCES projects(id), type TEXT NOT NULL CHECK (type IN ('entry_added', 'entry_edited', 'entry_deleted', 'submitted', 'withdrawn', 'approved', 'rejected', 'approval_withdrawn', 'budget_warning', 'reminded', 'project_changed', 'person_changed', 'client_changed', 'settings_changed', 'approver_reassigned')), data TEXT NOT NULL DEFAULT '{}', -- JSON stored as text created_at TIMESTAMP NOT NULL ); -- 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), subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')), error TEXT, attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 3), -- up to 3 attempts in all created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP ); CREATE INDEX notifications_status ON notifications (status);