-- OKR Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/okr-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. -- Key result values are integer hundredths (8.5% is 850, $40,000 is 4000000). INTEGER is 4 bytes in PostgreSQL, -- so a value must stay within about +/- 21.4 million units (2147483647 hundredths). -- Company settings. Exactly one row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), -- keeps this table to one row company_name TEXT NOT NULL, email_domain TEXT NOT NULL, -- sign-in is limited to this domain currency TEXT NOT NULL DEFAULT 'USD', -- used by every currency key result time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; days and job times are reckoned here reminder_day TEXT NOT NULL DEFAULT 'Monday' CHECK (reminder_day IN ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')), reminder_time TEXT NOT NULL DEFAULT '09:00', -- "HH:MM" company time summary_day TEXT NOT NULL DEFAULT 'Friday' CHECK (summary_day IN ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')), summary_time TEXT NOT NULL DEFAULT '16:00' -- "HH:MM" company time ); -- A team. At most one lead, who need not be a member; a team may have no lead. -- App rule (section 4): deleted only when it has no people and no objectives. CREATE TABLE teams ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) <= 100), lead_id TEXT -- a person id; no foreign key because people.team_id points back here (create order) ); -- Team names are unique ignoring case. CREATE UNIQUE INDEX teams_name_unique ON teams (lower(name)); -- A person who can sign in. Team leads are found through teams.lead_id. -- App rule (section 4): an admin cannot deactivate themselves; the last active admin cannot be demoted or deactivated. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) <= 100), email TEXT NOT NULL UNIQUE, -- trimmed and lowercase role TEXT NOT NULL CHECK (role IN ('admin', 'member')), team_id TEXT REFERENCES teams(id), -- optional active BOOLEAN NOT NULL DEFAULT TRUE, CHECK (email = lower(email)) ); -- A goal-setting period, usually a calendar quarter. Cannot be edited or deleted in v1; closed cycles are read-only. -- App rule (section 7): cycle dates must not overlap another cycle (touching dates are fine). CREATE TABLE cycles ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 50), -- such as "Q4 2026" starts_on DATE NOT NULL, ends_on DATE NOT NULL, status TEXT NOT NULL DEFAULT 'planning' CHECK (status IN ('planning', 'active', 'closed')), closed_at TIMESTAMP, closed_by TEXT REFERENCES people(id), CHECK (ends_on > starts_on), CHECK (status <> 'closed' OR (closed_at IS NOT NULL AND closed_by IS NOT NULL)), CHECK (status <> 'planning' OR (closed_at IS NULL AND closed_by IS NULL)) ); -- Cycle names are unique ignoring case. CREATE UNIQUE INDEX cycles_name_unique ON cycles (lower(name)); -- Only one active cycle at a time. CREATE UNIQUE INDEX cycles_one_active ON cycles (status) WHERE status = 'active'; -- An objective in a cycle, for the company or for one team. -- App rule (R1): 1 to 5 key results; at most 5 objectives per team, and 5 for the company, per cycle. -- App rule (R6): aligned_to_id is a company objective in the same cycle. CREATE TABLE objectives ( id TEXT PRIMARY KEY, cycle_id TEXT NOT NULL REFERENCES cycles(id), level TEXT NOT NULL CHECK (level IN ('company', 'team')), -- cannot change after creation team_id TEXT REFERENCES teams(id), -- team level only; cannot change after creation title TEXT NOT NULL CHECK (length(title) <= 200), description TEXT CHECK (length(description) <= 2000), owner_id TEXT NOT NULL REFERENCES people(id), aligned_to_id TEXT REFERENCES objectives(id), -- optional; no cascade, so a company objective with aligned team objectives cannot be deleted (R6) position INTEGER NOT NULL, -- order within its company or team list final_score_bp INTEGER CHECK (final_score_bp BETWEEN 0 AND 10000), -- set at close, basis points: 6875 shows as 0.7 carried_from_id TEXT REFERENCES objectives(id), -- the objective this one was copied from at a close (R7) CHECK ((level = 'company' AND team_id IS NULL) OR (level = 'team' AND team_id IS NOT NULL)), CHECK (aligned_to_id IS NULL OR level = 'team') ); -- A measurable key result of an objective. Progress is computed, not stored (section 6), except final_progress_bp at close. -- App rule (R2): the owner is active. App rule (section 13): the metric cannot change once check-ins exist. CREATE TABLE key_results ( id TEXT PRIMARY KEY, objective_id TEXT NOT NULL REFERENCES objectives(id) ON DELETE CASCADE, title TEXT NOT NULL CHECK (length(title) <= 200), -- free text; does not change when a target changes owner_id TEXT NOT NULL REFERENCES people(id), metric TEXT NOT NULL CHECK (metric IN ('number', 'percent', 'currency', 'milestone')), unit TEXT CHECK (length(unit) <= 30), -- such as "hours"; optional for numbers, empty for percent, currency and milestones direction TEXT NOT NULL CHECK (direction IN ('increase', 'decrease')), start_x100 INTEGER NOT NULL, -- hundredths; milestones store 0 target_x100 INTEGER NOT NULL, -- hundredths; milestones store 100 current_x100 INTEGER NOT NULL, -- hundredths; milestones store 0 or 100; a new key result starts at start_x100 confidence TEXT CHECK (confidence IN ('on_track', 'at_risk', 'off_track')), -- from the latest check-in; NULL until the first last_check_in_at TIMESTAMP, final_progress_bp INTEGER CHECK (final_progress_bp BETWEEN 0 AND 10000), -- set at close, basis points position INTEGER NOT NULL, -- order within its objective carried_from_id TEXT REFERENCES key_results(id), -- the key result it was carried forward from (R7) CHECK (unit IS NULL OR metric = 'number'), CHECK (metric <> 'milestone' OR (direction = 'increase' AND start_x100 = 0 AND target_x100 = 100 AND current_x100 IN (0, 100))), CHECK (metric <> 'percent' OR (start_x100 BETWEEN 0 AND 10000 AND target_x100 BETWEEN 0 AND 10000 AND current_x100 BETWEEN 0 AND 10000)), -- R2 and section 13: the target differs from the start, in the key result's direction CHECK ((direction = 'increase' AND target_x100 > start_x100) OR (direction = 'decrease' AND target_x100 < start_x100)), CHECK ((confidence IS NULL) = (last_check_in_at IS NULL)) ); -- One check-in on a key result. Append-only (R4): sets the key result's current value, confidence and last_check_in_at. -- App rule (section 7): refused unless the cycle is active. CREATE TABLE check_ins ( id TEXT PRIMARY KEY, key_result_id TEXT NOT NULL, -- no foreign key: check-ins stay when their key result is deleted (R1) at TIMESTAMP NOT NULL, by_id TEXT NOT NULL REFERENCES people(id), value_x100 INTEGER NOT NULL, -- hundredths; percent check-ins are 0 to 10000 (app rule, depends on the key result's metric) confidence TEXT NOT NULL CHECK (confidence IN ('on_track', 'at_risk', 'off_track')), comment TEXT NOT NULL CHECK (length(comment) BETWEEN 3 AND 1000) -- trimmed before it is stored ); CREATE INDEX check_ins_key_result ON check_ins (key_result_id, at); -- A change to a key result's start or target once its cycle is active (R5). Append-only. CREATE TABLE target_changes ( id TEXT PRIMARY KEY, key_result_id TEXT NOT NULL, -- no foreign key: target changes stay when their key result is deleted (R1) at TIMESTAMP NOT NULL, by_id TEXT NOT NULL REFERENCES people(id), field TEXT NOT NULL CHECK (field IN ('start', 'target')), old_x100 INTEGER NOT NULL, new_x100 INTEGER NOT NULL, reason TEXT NOT NULL CHECK (length(trim(reason)) BETWEEN 5 AND 300) ); -- Audit log entry. Append-only (R10). A cycle reopen stores its reason here. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT REFERENCES people(id), -- NULL when the hourly job acted type TEXT NOT NULL, data TEXT -- details as JSON text ); -- An email in the outbox. Written here first, then sent. Failed emails stay failed in v1. CREATE TABLE notifications ( id TEXT PRIMARY KEY, created_at TIMESTAMP NOT NULL, kind TEXT NOT NULL, -- which email it is: one of the triggers in section 10 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 provider is configured sent_at TIMESTAMP, error TEXT );