-- Capacity Planner database schema -- Source: https://personalsoftwarefactory.com/blueprints/capacity-planner (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. -- Every hour amount is integer minutes (1 hour = 60). Dates are calendar dates in the team time zone. -- There is no table of totals: capacity, booked and free hours, utilization and budget use are computed every time (R11). -- A person who can sign in, with a role and working hours per weekday. Deactivated, never deleted. -- App rule (R8): at least one active admin; deactivating or taking someone off the schedule is refused while they have bookings that run this week or later. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80), email TEXT NOT NULL UNIQUE, -- stored lower-case; any name@domain address title TEXT CHECK (length(title) <= 60), -- optional, such as "Designer" role TEXT NOT NULL DEFAULT 'member' CHECK (role IN ('admin', 'planner', 'member')), on_schedule BOOLEAN NOT NULL DEFAULT TRUE, -- FALSE for people who take no project work mon_minutes INTEGER NOT NULL DEFAULT 480 CHECK (mon_minutes BETWEEN 0 AND 1440 AND mon_minutes % 15 = 0), tue_minutes INTEGER NOT NULL DEFAULT 480 CHECK (tue_minutes BETWEEN 0 AND 1440 AND tue_minutes % 15 = 0), wed_minutes INTEGER NOT NULL DEFAULT 480 CHECK (wed_minutes BETWEEN 0 AND 1440 AND wed_minutes % 15 = 0), thu_minutes INTEGER NOT NULL DEFAULT 480 CHECK (thu_minutes BETWEEN 0 AND 1440 AND thu_minutes % 15 = 0), fri_minutes INTEGER NOT NULL DEFAULT 480 CHECK (fri_minutes BETWEEN 0 AND 1440 AND fri_minutes % 15 = 0), sat_minutes INTEGER NOT NULL DEFAULT 0 CHECK (sat_minutes BETWEEN 0 AND 1440 AND sat_minutes % 15 = 0), sun_minutes INTEGER NOT NULL DEFAULT 0 CHECK (sun_minutes BETWEEN 0 AND 1440 AND sun_minutes % 15 = 0), active BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMP NOT NULL, CHECK (email = lower(email)), -- R8: someone on the schedule needs at least one working day CHECK (on_schedule = FALSE OR mon_minutes + tue_minutes + wed_minutes + thu_minutes + fri_minutes + sat_minutes + sun_minutes > 0) ); -- A skill used to search Who has time. CREATE TABLE skills ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 40) ); -- Skill names are unique ignoring case. CREATE UNIQUE INDEX skills_name_unique ON skills (lower(name)); -- One skill a person has. Deleting a skill removes it from everyone (R9). CREATE TABLE person_skills ( person_id TEXT NOT NULL REFERENCES people(id), skill_id TEXT NOT NULL REFERENCES skills(id) ON DELETE CASCADE, PRIMARY KEY (person_id, skill_id) ); -- A company holiday that applies to everyone: capacity 0 that day. Added and deleted, never renamed. CREATE TABLE holidays ( id TEXT PRIMARY KEY, date DATE NOT NULL UNIQUE, -- one holiday per date name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 60) ); -- Recorded time off for one person, full days or a part day on each working day in the range. Never edited. -- App rule (R7): at most 366 days in total; a person's time off cannot overlap their other time off. CREATE TABLE time_off_entries ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), start_date DATE NOT NULL, end_date DATE NOT NULL, part_day_minutes INTEGER CHECK (part_day_minutes BETWEEN 15 AND 1440 AND part_day_minutes % 15 = 0), -- NULL means full days created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, CHECK (end_date >= start_date) ); -- A client or internal project. Closed and lost are final: nothing about the project, its phases or bookings changes after (R5). -- App rule (section 7): tentative > confirmed > closed, or tentative > lost. CREATE TABLE projects ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80), client TEXT CHECK (length(client) <= 80), -- optional status TEXT NOT NULL CHECK (status IN ('tentative', 'confirmed', 'closed', 'lost')), created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, status_changed_by TEXT REFERENCES people(id), -- set by Confirm, Mark lost and Close status_changed_at TIMESTAMP, CHECK ((status_changed_by IS NULL) = (status_changed_at IS NULL)), CHECK (status <> 'tentative' OR status_changed_at IS NULL), CHECK (status NOT IN ('closed', 'lost') OR status_changed_at IS NOT NULL) ); -- Project names are unique ignoring case. CREATE UNIQUE INDEX projects_name_unique ON projects (lower(name)); -- A phase of a project. Every booking belongs to one phase, so every booked hour counts against exactly one budget. -- App rule (R10): a project keeps at least one phase; a phase with bookings cannot be deleted. CREATE TABLE phases ( id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 60), budget_minutes INTEGER CHECK (budget_minutes BETWEEN 60 AND 6000000 AND budget_minutes % 60 = 0), -- NULL means no budget; whole hours 1 to 100,000 position INTEGER NOT NULL, -- display order within the project created_at TIMESTAMP NOT NULL ); -- Phase names are unique within a project ignoring case. CREATE UNIQUE INDEX phases_name_unique ON phases (project_id, lower(name)); -- One person booked on one phase for a run of whole weeks, at so many minutes a day or a week (Float calls this an allocation). -- App rule (R2): first_week and last_week are Mondays and cover at most 52 weeks, both ends counted. -- App rule (R4, R5): only active people on the schedule; on a tentative project every booking is tentative; closed and lost projects' bookings never change. -- App rule (R12): a save carries the updated_at it loaded and is refused if the row changed since. CREATE TABLE bookings ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), phase_id TEXT NOT NULL REFERENCES phases(id), -- the booking's project is its phase's project first_week DATE NOT NULL, -- a Monday last_week DATE NOT NULL, -- a Monday, on or after first_week unit TEXT NOT NULL CHECK (unit IN ('per_day', 'per_week')), minutes INTEGER NOT NULL, -- per_day: 15 to 1440; per_week: 15 to 10080; multiples of 15 tentative BOOLEAN NOT NULL, note TEXT CHECK (length(note) <= 200), created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, updated_by TEXT NOT NULL REFERENCES people(id), updated_at TIMESTAMP NOT NULL, CHECK (last_week >= first_week), CHECK (minutes % 15 = 0 AND ((unit = 'per_day' AND minutes BETWEEN 15 AND 1440) OR (unit = 'per_week' AND minutes BETWEEN 15 AND 10080))) ); CREATE INDEX bookings_person_weeks ON bookings (person_id, first_week, last_week); CREATE INDEX bookings_phase ON bookings (phase_id); -- A notification in the outbox, written in the same transaction as the change and sent after it commits. CREATE TABLE notifications ( id TEXT PRIMARY KEY, recipient_id TEXT NOT NULL REFERENCES people(id), kind TEXT NOT NULL CHECK (kind IN ('over_capacity', 'booking_added', 'booking_changed', 'booking_removed', 'project_confirmed', 'time_off_added', 'time_off_removed')), subject TEXT NOT NULL, body TEXT NOT NULL, -- plain text status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), -- logged when no provider is configured attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), -- each send attempt adds 1; logging counts as one error TEXT, created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP );