-- Content Calendar database schema -- Source: https://personalsoftwarefactory.com/blueprints/content-calendar (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 writer or editor on the content team. -- App rule (section 11): the last active editor and the default reviewer can't be demoted or deactivated; owners of unpublished pieces hand them over first. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, role TEXT NOT NULL CHECK (role IN ('writer', 'editor')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- The single settings row. Comes after people because it names the default reviewer. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), -- one row only time_zone TEXT NOT NULL, -- IANA name; publish dates and "today" are in this zone default_reviewer_id TEXT NOT NULL REFERENCES people(id), -- an editor; gets review requests by default (R3) last_digest_on DATE -- the Monday the weekly digest last went out ); -- A place content is published: Blog, Newsletter, LinkedIn and so on. -- App rule (section 13): the last active channel can't be deactivated. CREATE TABLE channels ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 30), color TEXT NOT NULL CHECK (color LIKE '#______'), -- hex, "#rrggbb" active BOOLEAN NOT NULL -- inactive channels keep their pieces but are hidden from pickers ); CREATE UNIQUE INDEX channels_name_uniq ON channels (LOWER(name)); -- unique ignoring case -- A named date range that groups pieces. CREATE TABLE campaigns ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 1 AND 80), starts_on DATE NOT NULL, ends_on DATE NOT NULL, goal TEXT CHECK (LENGTH(goal) <= 200), -- optional created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, CHECK (ends_on >= starts_on) ); CREATE UNIQUE INDEX campaigns_name_uniq ON campaigns (LOWER(name)); -- unique ignoring case -- One piece of content moving from idea to published. Overdue is computed, never stored (R12). -- App rule (R1, R2): only the transitions in section 6; the only way into scheduled is an editor's approval from in_review. -- App rule (R3): nobody approves or requests changes on a piece they own at that moment. -- App rule (R4, R6, R7): sending for review and approving need a publish date of today or later; moves to past dates are refused. -- App rule (R14): only ideas can be deleted. CREATE TABLE pieces ( id TEXT PRIMARY KEY, title TEXT NOT NULL CHECK (LENGTH(title) BETWEEN 1 AND 120), channel_id TEXT NOT NULL REFERENCES channels(id), owner_id TEXT NOT NULL REFERENCES people(id), -- an active person campaign_id TEXT REFERENCES campaigns(id), status TEXT NOT NULL CHECK (status IN ('idea', 'writing', 'in_review', 'scheduled', 'published')), publish_on DATE, -- empty means unscheduled (the backlog) draft_url TEXT CHECK (draft_url LIKE 'http://%' OR draft_url LIKE 'https://%'), notes TEXT CHECK (LENGTH(notes) <= 2000), reviewer_id TEXT REFERENCES people(id), -- the editor asked to review, set on submit submitted_at TIMESTAMP, approved_by TEXT REFERENCES people(id), approved_at TIMESTAMP, published_at TIMESTAMP, reminded_for DATE, -- the publish_on value the due-soon reminder was sent for (R11); cleared on a move overdue_notified_for DATE, -- the publish_on value the overdue notice was sent for (R12); cleared on a move created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, CHECK (status IN ('idea', 'writing') OR publish_on IS NOT NULL), -- R8: in review, scheduled and published pieces keep a date CHECK (status IN ('idea', 'writing') OR draft_url IS NOT NULL), -- R4 and R7: a draft link to submit, unchangeable after CHECK (status <> 'in_review' OR (reviewer_id IS NOT NULL AND submitted_at IS NOT NULL)), CHECK ((status IN ('scheduled', 'published') AND approved_by IS NOT NULL AND approved_at IS NOT NULL) OR (status NOT IN ('scheduled', 'published') AND approved_by IS NULL AND approved_at IS NULL)), -- reopen and request changes clear the approval CHECK ((status = 'published' AND published_at IS NOT NULL) OR (status <> 'published' AND published_at IS NULL)) ); CREATE INDEX pieces_publish_on_idx ON pieces (publish_on); CREATE INDEX pieces_owner_idx ON pieces (owner_id, status); CREATE INDEX pieces_campaign_idx ON pieces (campaign_id); -- One append-only log entry for a piece. Never edited or deleted (R17). CREATE TABLE activities ( id TEXT PRIMARY KEY, piece_id TEXT NOT NULL, -- not a foreign key: the deleted entry outlives its piece (section 6, Delete) actor_id TEXT REFERENCES people(id), -- empty for the scheduled job type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'moved', 'started_writing', 'parked', 'submitted', 'withdrawn', 'approved', 'changes_requested', 'reopened', 'published', 'commented', 'reminded', 'overdue', 'deleted')), comment TEXT, data TEXT, -- JSON text, e.g. the from and to dates of a move, or the title of a deleted idea created_at TIMESTAMP NOT NULL ); CREATE INDEX activities_piece_idx ON activities (piece_id, created_at); -- One in-app notification, also emailed. CREATE TABLE notifications ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), kind TEXT NOT NULL CHECK (kind IN ('review_requested', 'approved', 'changes_requested', 'assigned', 'commented', 'due_soon', 'overdue', 'digest')), piece_id TEXT REFERENCES pieces(id) ON DELETE SET NULL, -- empty for digests, and cleared if the idea is deleted text TEXT NOT NULL, created_at TIMESTAMP NOT NULL, read BOOLEAN NOT NULL DEFAULT FALSE, emailed_at TIMESTAMP, error TEXT -- a failed email is retried on the next daily job ); CREATE INDEX notifications_person_idx ON notifications (person_id, read, created_at);