-- Simple CRM database schema -- Source: https://personalsoftwarefactory.com/blueprints/simple-crm (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. -- One row per person who uses the CRM. -- App rule (R19): only admins add or deactivate people, an admin cannot deactivate themselves, and deactivating reassigns their records. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, -- unique ignoring case (index below); store it trimmed is_admin BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); -- One row per company (an organization the team sells to). -- App rule (R9): owner_id must point at an active person. -- App rule (R13): a contact form or import row naming an unknown company creates it, owned by the contact's owner. CREATE TABLE companies ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 120), -- unique ignoring case (index below); store it trimmed domain TEXT UNIQUE CHECK (domain = lower(domain) AND domain NOT LIKE '%/%' AND domain NOT LIKE 'www.%'), -- normalised, for example acme.example; empty when unknown owner_id TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); -- One row per contact (a person at a customer or prospect). -- App rule (R4): a follow-up date entered or changed by hand must be today or later. -- App rule (R5): logging a call, email, meeting or note sets next_follow_up to tomorrow or later in the same transaction. -- App rule (R9): owner_id must point at an active person. -- App rule (R14): archiving is refused while the contact has an open deal; unarchiving sets next_follow_up to today if it is empty or past. CREATE TABLE contacts ( id TEXT PRIMARY KEY, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 120), email TEXT, -- optional; unique ignoring case across all contacts, archived included (index below) phone TEXT, -- free text title TEXT, -- job title company_id TEXT REFERENCES companies(id), owner_id TEXT NOT NULL REFERENCES people(id), next_follow_up DATE, -- required while the contact is not archived (R1); overdue, due today and upcoming are computed from it, never stored archived BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, last_activity_on DATE, -- derived: latest happened_on of this contact's call, email, meeting and note activities; empty if none. Kept in sync by the app, or drop it and compute it CHECK (archived = TRUE OR next_follow_up IS NOT NULL) ); -- One row per deal in the single pipeline. -- App rule (R15): a deal is created in an open stage, for a contact who is not archived; a new or changed expected_close on an open deal must be today or later. -- App rule (R17): won and lost set closed_on to today; reopening clears closed_on and lost_reason. Every stage change writes a system activity. -- App rule (R9): while the deal is open, owner_id must point at an active person. Closed deals keep their original owner (R19). CREATE TABLE deals ( id TEXT PRIMARY KEY, title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 120), contact_id TEXT NOT NULL REFERENCES contacts(id), company_id TEXT REFERENCES companies(id), -- copied from the contact's company whenever the deal's contact is set; not updated when the contact later changes company owner_id TEXT NOT NULL REFERENCES people(id), value_cents INTEGER NOT NULL CHECK (value_cents >= 0), -- integer cents in the team currency (TEAM_CURRENCY) stage TEXT NOT NULL CHECK (stage IN ('lead', 'demo', 'proposal', 'negotiation', 'won', 'lost')), -- probabilities 10, 30, 60, 80, 100 and 0 percent (section 7) expected_close DATE NOT NULL, closed_on DATE, -- set exactly when the stage is won or lost lost_reason TEXT, -- set exactly when the stage is lost, at least 3 characters created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CHECK ((stage IN ('won', 'lost') AND closed_on IS NOT NULL) OR (stage NOT IN ('won', 'lost') AND closed_on IS NULL)), CHECK ((stage = 'lost' AND lost_reason IS NOT NULL AND length(lost_reason) >= 3) OR (stage <> 'lost' AND lost_reason IS NULL)) ); -- One row per entry on a contact's timeline. -- App rule (R6): happened_on must be today or earlier. -- App rule (R7): archived contacts cannot get new activities. -- App rule (R8): the author or an admin can delete a call, email, meeting or note; system activities are never deleted, and deleting never changes the follow-up date. -- App rule (R11): changing a contact's owner or follow-up date, including by logging an activity, writes a system activity. CREATE TABLE activities ( id TEXT PRIMARY KEY, contact_id TEXT NOT NULL REFERENCES contacts(id), deal_id TEXT REFERENCES deals(id), -- set when the activity is about a deal, including every stage change type TEXT NOT NULL CHECK (type IN ('call', 'email', 'meeting', 'note', 'system')), -- system rows are written by the app only happened_on DATE NOT NULL, note TEXT CHECK (note IS NULL OR length(note) <= 2000), by_id TEXT NOT NULL REFERENCES people(id), -- the person who logged it, or whose action wrote the system row created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CHECK (type NOT IN ('note', 'system') OR note IS NOT NULL), CHECK (type <> 'note' OR length(note) >= 3) ); -- One row per daily digest email, written before sending so the job never sends twice. -- App rule (section 10): a failed send keeps its row with the error and is retried on the next run. CREATE TABLE digest_logs ( id TEXT PRIMARY KEY, person_id TEXT NOT NULL REFERENCES people(id), date DATE NOT NULL, -- the day the digest covers, in the team time zone overdue_count INTEGER NOT NULL CHECK (overdue_count >= 0), due_today_count INTEGER NOT NULL CHECK (due_today_count >= 0), deals_count INTEGER NOT NULL CHECK (deals_count >= 0), -- open deals past close date or closing within 7 days sent_at TIMESTAMP, -- empty until the email is sent error TEXT, -- the last send error, if any UNIQUE (person_id, date) ); -- Case-insensitive uniqueness (R12, R13, section 5). CREATE UNIQUE INDEX people_email_idx ON people (lower(email)); CREATE UNIQUE INDEX companies_name_idx ON companies (lower(name)); CREATE UNIQUE INDEX contacts_email_idx ON contacts (lower(email)); -- The contact list and Today screen: follow-up order per owner (R3). CREATE INDEX contacts_owner_follow_up_idx ON contacts (owner_id, next_follow_up); CREATE INDEX contacts_company_idx ON contacts (company_id); CREATE INDEX deals_contact_idx ON deals (contact_id); CREATE INDEX deals_stage_idx ON deals (stage, expected_close); CREATE INDEX activities_contact_idx ON activities (contact_id, happened_on);