# SaaS management system database schema

Source: https://personalsoftwarefactory.com/schemas/saas-spend-tracker
Updated: 2026-12-04

> Six tables, with tools carrying the design: one row per subscription with its cycle price for the whole subscription, billing cycle, renewal date, notice days, seats and status. Annualized cost, unused seat cost, notice deadline and reminder date are computed, never stored, and a unique dedupe key on notifications makes the daily reminder job safe to run twice.

- 6 tables, 8 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-04
- 27 seed rows from the blueprint
- The blueprint's test build passed 31 of 31 acceptance checks

Files: [saas-spend-tracker.sql](https://personalsoftwarefactory.com/schemas/saas-spend-tracker.sql) · [saas-spend-tracker-seed.sql](https://personalsoftwarefactory.com/schemas/saas-spend-tracker-seed.sql) · [the SaaS Spend Tracker blueprint](https://personalsoftwarefactory.com/blueprints/saas-spend-tracker)

## Relationships

- tools.owner_id references people.id: each tools row points to exactly one people row, and one people row can have many tools rows pointing to it.
- tools.department_id references departments.id: each tools row points to exactly one departments row, and one departments row can have many tools rows pointing to it.
- history_events.tool_id references tools.id: each history_events row points to exactly one tools row, and one tools row can have many history_events rows pointing to it.
- history_events.actor_id references people.id: each history_events row points to at most one people row, and one people row can have many history_events rows pointing to it.
- notifications.recipient_id references people.id: each notifications row points to exactly one people row, and one people row can have many notifications rows pointing to it.
- notifications.tool_id references tools.id: each notifications row points to at most one tools row, and one tools row can have many notifications rows pointing to it.

## Tables

Also described as: software subscription tracker database design, SaaS license management database tables, vendor contract renewal SQL schema.

### people

A team member, who may own tools and may be a finance admin. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  |  | unique ignoring case (index below) |
| `is_admin` | BOOLEAN | no | `FALSE` |  |  |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- Unique index `people_email_unique` on (LOWER(email))
- App rule (R16): at least one active admin must remain.

### departments

The budget that pays for a tool. Implements: Department.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | unique ignoring case (index below) Check: `LENGTH(name) BETWEEN 2 AND 60` |

- Unique index `departments_name_unique` on (LOWER(name))
- App rule (screen 10): a department with tools of any status can't be deleted.

### tools

One paid subscription or contract, with its price per billing cycle, renewal date, notice period, seats and status. Implements: Tool.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | trimmed; unique among current tools (index below) Check: `name = TRIM(name) AND LENGTH(name) BETWEEN 2 AND 100` |
| `owner_id` | TEXT | no |  | References people.id |  |
| `department_id` | TEXT | no |  | References departments.id |  |
| `billing_cycle` | TEXT | no |  |  | Check: `billing_cycle IN ('monthly', 'quarterly', 'annual')` |
| `cycle_price_cents` | INTEGER | no |  |  | total per billing cycle for the whole subscription, never per seat Check: `cycle_price_cents BETWEEN 0 AND 100000000` |
| `renews_on` | DATE | no |  |  | end of the current term; for a cancelling tool, the day it ends |
| `auto_renew` | BOOLEAN | no | `TRUE` |  | the vendor renews unless notice is given |
| `notice_days` | INTEGER | no | `0` |  | notice the contract requires before renews_on Check: `notice_days BETWEEN 0 AND 365` |
| `seats_purchased` | INTEGER | no | `0` |  | 0 means not seat-based Check: `seats_purchased BETWEEN 0 AND 100000` |
| `seats_used` | INTEGER | no | `0` |  | typed in by the owner Check: `seats_used >= 0` |
| `status` | TEXT | no | `'active'` |  | Check: `status IN ('active', 'cancelling', 'cancelled')` |
| `status_reason` | TEXT | yes |  |  | given when marked cancelling or cancelled Check: `status_reason IS NULL OR LENGTH(status_reason) BETWEEN 3 AND 200` |
| `ended_on` | DATE | yes |  |  | set when the tool becomes cancelled |
| `link` | TEXT | yes |  |  | Check: `link IS NULL OR (LENGTH(link) <= 500 AND (SUBSTR(link, 1, 7) = 'http://' OR SUBSTR(link, 1, 8) = 'https://'))` |
| `notes` | TEXT | yes |  |  | Check: `notes IS NULL OR LENGTH(notes) <= 1000` |
| `version` | INTEGER | no | `1` |  | Check: `version >= 1` |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `updated_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Table check: `seats_used <= seats_purchased`
- Table check: `status = 'active' OR status_reason IS NOT NULL`
- Table check: `(status = 'cancelled' AND ended_on IS NOT NULL) OR (status <> 'cancelled' AND ended_on IS NULL)`
- Unique index `tools_current_name_unique` on (LOWER(name)) where `status IN ('active', 'cancelling')`
- Index `tools_owner` on (owner_id)
- Index `tools_department` on (department_id)
- Index `tools_status_renews` on (status, renews_on)
- App rule (R16): owner_id is an active person while the tool is active or cancelling.
- App rule (R18): every change increments version; a save with a stale version is refused.
- App rule (R19): cancelled tools are read-only until restored.

### history_events

One append-only change to a tool, by a person, a CSV import or the daily job. Implements: HistoryEvent.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `tool_id` | TEXT | no |  | References tools.id |  |
| `actor_id` | TEXT | yes |  | References people.id | null means the daily job |
| `type` | TEXT | no |  |  | Check: `type IN ('created', 'edited', 'renewed', 'auto_renewed', 'status_changed', 'reminder_queued')` |
| `source` | TEXT | no |  |  | Check: `source IN ('form', 'csv', 'job')` |
| `changes` | TEXT | yes |  |  | JSON text: field -> {from, to}; a late cancellation also holds "late_notice": true |
| `reason` | TEXT | yes |  |  |  |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Index `history_events_tool` on (tool_id, created_at)

### notifications

One renewal reminder or owner notice in the outbox, with its dedupe key and send status. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `recipient_id` | TEXT | no |  | References people.id |  |
| `kind` | TEXT | no |  |  | Check: `kind IN ('renewal_reminder', 'owner_assigned')` |
| `tool_id` | TEXT | yes |  | References tools.id | null when one notice lists several tools |
| `dedupe_key` | TEXT | yes |  | Unique | reminder:{tool_id}:{renews_on}:{notice_deadline}:{recipient_id} (R7) |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  | the text as sent |
| `status` | TEXT | no | `'queued'` |  | logged: no email provider configured Check: `status IN ('queued', 'sent', 'logged', 'failed')` |
| `attempts` | INTEGER | no | `0` |  | Check: `attempts >= 0` |
| `last_error` | TEXT | yes |  |  |  |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |

- Index `notifications_recipient` on (recipient_id, created_at)
- App rule (section 11): after 5 failed attempts the status becomes failed.

### settings

The single row with the currency, time zone and development clock offset. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no | `1` | Primary key | always 1, so there can only be one row Check: `id = 1` |
| `currency_code` | TEXT | no | `'USD'` |  | changes the symbol only; amounts are never converted |
| `time_zone` | TEXT | no | `'UTC'` |  | IANA name; today is the date in this zone |
| `dev_clock_offset_days` | INTEGER | no | `0` |  | development only, ignored in production |

## The SQL

```sql
-- SaaS Spend Tracker database schema
-- Source: https://personalsoftwarefactory.com/blueprints/saas-spend-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.
--
-- Money is integer cents. renews_on and ended_on are calendar dates with no time.
-- Annualized cost, unused seat cost, notice deadline and reminder date are computed, never stored (section 5).

-- A person on the team. Owners and admins are people.
-- App rule (R16): at least one active admin must remain.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL, -- unique ignoring case (index below)
  is_admin BOOLEAN NOT NULL DEFAULT FALSE,
  active BOOLEAN NOT NULL DEFAULT TRUE
);

CREATE UNIQUE INDEX people_email_unique ON people (LOWER(email));

-- A department: the budget that pays for a tool.
-- App rule (screen 10): a department with tools of any status can't be deleted.
CREATE TABLE departments (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (LENGTH(name) BETWEEN 2 AND 60) -- unique ignoring case (index below)
);

CREATE UNIQUE INDEX departments_name_unique ON departments (LOWER(name));

-- One paid subscription or contract.
-- App rule (R16): owner_id is an active person while the tool is active or cancelling.
-- App rule (R18): every change increments version; a save with a stale version is refused.
-- App rule (R19): cancelled tools are read-only until restored.
CREATE TABLE tools (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (name = TRIM(name) AND LENGTH(name) BETWEEN 2 AND 100), -- trimmed; unique among current tools (index below)
  owner_id TEXT NOT NULL REFERENCES people(id),
  department_id TEXT NOT NULL REFERENCES departments(id),
  billing_cycle TEXT NOT NULL CHECK (billing_cycle IN ('monthly', 'quarterly', 'annual')),
  cycle_price_cents INTEGER NOT NULL CHECK (cycle_price_cents BETWEEN 0 AND 100000000), -- total per billing cycle for the whole subscription, never per seat
  renews_on DATE NOT NULL, -- end of the current term; for a cancelling tool, the day it ends
  auto_renew BOOLEAN NOT NULL DEFAULT TRUE, -- the vendor renews unless notice is given
  notice_days INTEGER NOT NULL DEFAULT 0 CHECK (notice_days BETWEEN 0 AND 365), -- notice the contract requires before renews_on
  seats_purchased INTEGER NOT NULL DEFAULT 0 CHECK (seats_purchased BETWEEN 0 AND 100000), -- 0 means not seat-based
  seats_used INTEGER NOT NULL DEFAULT 0 CHECK (seats_used >= 0), -- typed in by the owner
  status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'cancelling', 'cancelled')),
  status_reason TEXT CHECK (status_reason IS NULL OR LENGTH(status_reason) BETWEEN 3 AND 200), -- given when marked cancelling or cancelled
  ended_on DATE, -- set when the tool becomes cancelled
  link TEXT CHECK (link IS NULL OR (LENGTH(link) <= 500 AND (SUBSTR(link, 1, 7) = 'http://' OR SUBSTR(link, 1, 8) = 'https://'))),
  notes TEXT CHECK (notes IS NULL OR LENGTH(notes) <= 1000),
  version INTEGER NOT NULL DEFAULT 1 CHECK (version >= 1),
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CHECK (seats_used <= seats_purchased), -- R14
  CHECK (status = 'active' OR status_reason IS NOT NULL), -- R19: a reason is required
  CHECK ((status = 'cancelled' AND ended_on IS NOT NULL) OR (status <> 'cancelled' AND ended_on IS NULL))
);

CREATE UNIQUE INDEX tools_current_name_unique ON tools (LOWER(name)) WHERE status IN ('active', 'cancelling'); -- R13: a cancelled tool's name can be reused
CREATE INDEX tools_owner ON tools (owner_id);
CREATE INDEX tools_department ON tools (department_id);
CREATE INDEX tools_status_renews ON tools (status, renews_on);

-- One change to a tool. Append-only, never updated or deleted (R17).
CREATE TABLE history_events (
  id TEXT PRIMARY KEY,
  tool_id TEXT NOT NULL REFERENCES tools(id),
  actor_id TEXT REFERENCES people(id), -- null means the daily job
  type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'renewed', 'auto_renewed', 'status_changed', 'reminder_queued')),
  source TEXT NOT NULL CHECK (source IN ('form', 'csv', 'job')),
  changes TEXT, -- JSON text: field -> {from, to}; a late cancellation also holds "late_notice": true
  reason TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX history_events_tool ON history_events (tool_id, created_at);

-- One reminder or notice in the outbox, written before it is sent.
-- App rule (section 11): after 5 failed attempts the status becomes failed.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  recipient_id TEXT NOT NULL REFERENCES people(id),
  kind TEXT NOT NULL CHECK (kind IN ('renewal_reminder', 'owner_assigned')),
  tool_id TEXT REFERENCES tools(id), -- null when one notice lists several tools
  dedupe_key TEXT UNIQUE, -- reminder:{tool_id}:{renews_on}:{notice_deadline}:{recipient_id} (R7)
  subject TEXT NOT NULL,
  body TEXT NOT NULL, -- the text as sent
  status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')), -- logged: no email provider configured
  attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
  last_error TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  sent_at TIMESTAMP
);

CREATE INDEX notifications_recipient ON notifications (recipient_id, created_at);

-- The single settings row.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1), -- always 1, so there can only be one row
  currency_code TEXT NOT NULL DEFAULT 'USD', -- changes the symbol only; amounts are never converted
  time_zone TEXT NOT NULL DEFAULT 'UTC', -- IANA name; today is the date in this zone
  dev_clock_offset_days INTEGER NOT NULL DEFAULT 0 -- development only, ignored in production
);
```

## Seed data

Loads section 14 with T fixed to Tue 13 Oct 2026, the date the spec's examples use: 7 people, 5 departments, the 6 tools with their prices, seats, renewal dates, notice periods and statuses, 8 history events (created by Ana Lima for each tool, Dana's change of Monday.com to cancelling and Maya's change of Loom to cancelled), and the settings row. The spec gives no times for the seed history, so the events are stamped 09:00 and 09:05 on T; stamped on T, Dana's change comes after Monday.com's Oct 11 notice deadline, so its changes record late_notice as section 8 requires. The outbox is empty, as the spec says; the first daily job run queues the four reminders.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/saas-spend-tracker.sql https://personalsoftwarefactory.com/schemas/saas-spend-tracker-seed.sql | sqlite3 saas_spend_tracker.db
# PostgreSQL
createdb saas_spend_tracker && curl -s https://personalsoftwarefactory.com/schemas/saas-spend-tracker.sql https://personalsoftwarefactory.com/schemas/saas-spend-tracker-seed.sql | psql -v ON_ERROR_STOP=1 saas_spend_tracker
```

## Design decisions

### The price is per billing cycle for the whole subscription

tools.cycle_price_cents is the total charged each cycle, never a per-seat price (section 5, R1). Annualized cost is the cycle price times 12, 4 or 1, so a $1,000-a-month plan with 100 seats is $12,000 a year. Storing a per-seat price invites multiplying by seats and reporting $1,200,000.

### Money and dates that follow from the row are never stored

Annualized cost, unused seat cost, the notice deadline (renews_on minus notice_days) and the reminder date are computed in one shared module (section 5), so a changed price or notice period can never leave a stale total. Date arithmetic differs between SQLite and PostgreSQL, so the deadline belongs in the app, not in a portable query.

### Names are unique among current tools only

R13 makes names unique ignoring case among active and cancelling tools, and lets a cancelled tool's name be reused. A partial unique index on LOWER(name) WHERE status IN ('active', 'cancelling') enforces exactly that; restoring a cancelled tool while a current one has its name fails on the same index.

### Status, reason and end date must agree

CHECK constraints require a reason for cancelling and cancelled tools (R19), an ended_on exactly when the tool is cancelled (section 8), and seats_used no higher than seats_purchased (R14). Tools are never deleted; a mistake is marked cancelled with that reason, so the history stays complete.

### A unique dedupe key makes reminders idempotent

notifications.dedupe_key is reminder:{tool_id}:{renews_on}:{notice_deadline}:{recipient_id} and is UNIQUE (R7). Two overlapping job runs cannot queue the same reminder; the second insert fails and is treated as already queued. A new renewal date, notice period or owner makes a new key, so the next reminder is queued when it is due.

### Late notice lives in the history event's changes

Our build test found that cancelling after the notice deadline was supposed to record late_notice, but the history model had no place for it. Section 5 now puts "late_notice": true in the changes JSON of that status_changed event, which history_events.changes holds as text. (This point comes from a gap the blueprint's build test found.)

### Every edit carries a version

tools.version starts at 1 and goes up on every change (R18). A save sends the version the form was opened with, and the update is refused when the row has moved on, so a person and the daily job can't overwrite each other's changes.

## Example queries

### Company tiles: run rate, seats and unused seat cost

```sql
SELECT
  COUNT(*) AS active_tools,
  SUM(CASE billing_cycle WHEN 'monthly' THEN cycle_price_cents * 12 WHEN 'quarterly' THEN cycle_price_cents * 4 ELSE cycle_price_cents END) AS run_rate_cents,
  SUM(seats_used) AS seats_used,
  SUM(seats_purchased) AS seats_purchased,
  SUM(CASE WHEN seats_purchased = 0 THEN 0 ELSE ((CASE billing_cycle WHEN 'monthly' THEN cycle_price_cents * 12 WHEN 'quarterly' THEN cycle_price_cents * 4 ELSE cycle_price_cents END) * (seats_purchased - seats_used) * 2 + seats_purchased) / (2 * seats_purchased) END) AS unused_seat_cents
FROM tools
WHERE status = 'active';
```

On the seed data: 1 row. 4 active tools, run rate 3132000 cents ($31,320), 141 of 190 seats used, unused seat cost 894000 cents ($8,940), as section 6 says. On PostgreSQL, very large contracts can overflow 32-bit INTEGER in the unused-seat product; R3 asks for 64-bit arithmetic.

### Totals by department, with ending tools

```sql
SELECT
  d.name,
  SUM(CASE WHEN t.status = 'active' THEN 1 ELSE 0 END) AS active_tools,
  SUM(CASE WHEN t.status = 'active' THEN (CASE t.billing_cycle WHEN 'monthly' THEN t.cycle_price_cents * 12 WHEN 'quarterly' THEN t.cycle_price_cents * 4 ELSE t.cycle_price_cents END) ELSE 0 END) AS run_rate_cents,
  SUM(CASE WHEN t.status = 'cancelling' THEN 1 ELSE 0 END) AS ending_tools,
  SUM(CASE WHEN t.status = 'cancelling' THEN (CASE t.billing_cycle WHEN 'monthly' THEN t.cycle_price_cents * 12 WHEN 'quarterly' THEN t.cycle_price_cents * 4 ELSE t.cycle_price_cents END) ELSE 0 END) AS ending_cents
FROM departments d
LEFT JOIN tools t ON t.department_id = d.id
GROUP BY d.name
ORDER BY d.name;
```

On the seed data: 5 rows, matching the section 6 department table. Ops has 1 active tool at 1200000 cents ($12,000) and 1 ending tool at 240000 cents; Finance has none.

### Unused seat cost per active tool

```sql
SELECT
  name,
  seats_used,
  seats_purchased,
  CASE WHEN seats_purchased = 0 THEN 0 ELSE ((CASE billing_cycle WHEN 'monthly' THEN cycle_price_cents * 12 WHEN 'quarterly' THEN cycle_price_cents * 4 ELSE cycle_price_cents END) * (seats_purchased - seats_used) * 2 + seats_purchased) / (2 * seats_purchased) END AS unused_seat_cents
FROM tools
WHERE status = 'active'
ORDER BY unused_seat_cents DESC, name;
```

On the seed data: 4 rows. Notion 336000 cents ($3,360), Figma 288000, Zoom 270000 and GitHub 0. The integer formula is R3's round-half-up.

### Upcoming renewals with their notice periods

```sql
SELECT t.name, p.name AS owner, t.renews_on, t.auto_renew, t.notice_days, t.status
FROM tools t
JOIN people p ON p.id = t.owner_id
WHERE t.status IN ('active', 'cancelling')
ORDER BY t.renews_on, t.name;
```

On the seed data: 5 rows, starting with Notion (2026-10-20, auto-renew, 0 days' notice) and Monday.com (cancelling, ends 2026-10-25). The notice deadline, renews_on minus notice_days, is computed in the app (R5).

### One tool's history, newest first

```sql
SELECT h.type, h.source, COALESCE(p.name, 'Daily job') AS actor, h.reason, h.created_at
FROM history_events h
JOIN tools t ON t.id = h.tool_id
LEFT JOIN people p ON p.id = h.actor_id
WHERE t.name = 'Monday.com'
ORDER BY h.created_at DESC, h.id DESC;
```

On the seed data: 2 rows for Monday.com, the status change by Dana Ruiz ("Nobody has used it since we moved to Notion") and the created event by Ana Lima.

## What was not tested

- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.

## The blueprint

This schema is section 5 of the [SaaS Spend Tracker blueprint](https://personalsoftwarefactory.com/blueprints/saas-spend-tracker), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/saas-spend-tracker/blueprint.md
