Accounts receivable collections database schema
An invoice reminder tool needs thirteen tables, and four carry the design: invoices, steps, reminders_sent and daily_runs. A unique key on reminders_sent (invoice_id, step_id) stops any invoice from getting the same reminder step twice, and a unique run_date on daily_runs keeps the daily run to once a day. Money is integer cents, and check constraints keep each balance between zero and the invoice amount.
- 13 tables, 7 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
- 17 seed rows from the blueprint
- The blueprint's test build passed 18 of 18 acceptance checks
Invoice reminders entity-relationship diagram
Relationships
- customers.account_owner_id references people.id: each customers row points to at most one people row, and one people row can have many customers rows pointing to it.
- invoices.customer_id references customers.id: each invoices row points to exactly one customers row, and one customers row can have many invoices rows pointing to it.
- payments.invoice_id references invoices.id: each payments row points to exactly one invoices row, and one invoices row can have many payments rows pointing to it.
- payments.recorded_by references people.id: each payments row points to exactly one people row, and one people row can have many payments rows pointing to it.
- steps.template_id references templates.id: each steps row points to exactly one templates row, and one templates row can have many steps rows pointing to it.
- reminders_sent.invoice_id references invoices.id: each reminders_sent row points to exactly one invoices row, and one invoices row can have many reminders_sent rows pointing to it.
- reminders_sent.step_id references steps.id: each reminders_sent row points to exactly one steps row, and one steps row can have many reminders_sent rows pointing to it.
- reminders_sent.notification_id references notifications.id: each reminders_sent row points to exactly one notifications row, and one notifications row can have many reminders_sent rows pointing to it.
- notes.invoice_id references invoices.id: each notes row points to at most one invoices row, and one invoices row can have many notes rows pointing to it.
- notes.customer_id references customers.id: each notes row points to at most one customers row, and one customers row can have many notes rows pointing to it.
- notes.by_id references people.id: each notes row points to at most one people row, and one people row can have many notes rows pointing to it.
- import_runs.by_id references people.id: each import_runs row points to exactly one people row, and one people row can have many import_runs rows pointing to it.
- daily_runs.run_by references people.id: each daily_runs row points to at most one people row, and one people row can have many daily_runs rows pointing to it.
- events.actor references people.id: each events row points to at most one people row, and one people row can have many events rows pointing to it.
Invoice reminders tables
The schema has 13 tables. Every column below is read from the SQL file itself. Also described as: invoice reminder system database design, dunning schedule database tables, overdue invoice tracker data model.
| Table | Implements | What a row is |
|---|---|---|
settings | Settings | The single settings row: company, sign-in domain, sender, time zone, send time and the sending switch. |
people | Person | One row per person who signs in, with their role and whether they are active. |
customers | Customer | One row per customer, with contact and cc addresses, account owner and pause state. |
invoices | Invoice | One row per invoice, with amount and balance in cents, status, dispute reason and promise date. |
payments | Payment | One row per payment recorded in the app against an invoice. |
templates | Template | One row per email template, with a subject and a plain-text body that use merge fields. |
steps | Step | One row per step of the reminder schedule, with its offset from the due date and its template. |
notifications | Notification | One row per email in the outbox, including reminders held while sending is off. |
reminders_sent | ReminderSent | One row per invoice per step sent, linked to the email that carried it. |
notes | Note | One row per entry on an invoice's or a customer's append-only notes timeline. |
import_runs | ImportRun | One row per CSV import, with its five result counts. |
daily_runs | DailyRun | One row per business day's reminder run, with a snapshot of what was sent and skipped. |
events | Event | One row per change, in an append-only audit log. |
settings
The single settings row for the one company, sender and time zone.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | exactly one rowCheck: id = 'settings' | |
company_name | TEXT | no | |||
company_domain | TEXT | no | the sign-in domain, for example northwind.example | ||
from_name | TEXT | no | used by the {from_name} merge field | ||
from_email | TEXT | no | the shared address reminders are sent from | ||
reply_to | TEXT | yes | |||
time_zone | TEXT | no | 'America/New_York' | IANA name; business dates are calendar dates in this zone | |
send_time | TEXT | no | '09:00' | HH:MM, 24-hour, company timeCheck: send_time LIKE '__:__' AND send_time BETWEEN '00:00' AND '23:59' AND substr(send_time, 4, 2) < '60' | |
sending_enabled | BOOLEAN | no | FALSE | off: every customer reminder is written to the outbox as held | |
account_owner_cc_on_final | BOOLEAN | no | TRUE | copy the active account owner on the Final notice |
- App rule (R8): turning sending_enabled on is admin-only and needs a confirmation the server checks.
people
One row per person who signs in.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
email | TEXT | no | Unique | must be on settings.company_domain to sign in | |
role | TEXT | no | Check: role IN ('admin', 'member') | ||
active | BOOLEAN | no | TRUE |
- App rule (section 4): an admin cannot deactivate or demote themselves, so there is always an active admin.
customers
One row per customer who owes money.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | unique, compared trimmed and ignoring case (index below) | ||
contact_email | TEXT | yes | may be empty; the run then skips the customer's invoices (No contact email) | ||
cc_emails | TEXT | no | '' | comma-separated list of valid addresses, copied on every reminder | |
account_owner_id | TEXT | yes | References people.id | may be empty for customers created by import | |
paused | BOOLEAN | no | FALSE | paused customers get no reminders (R3) | |
paused_reason | TEXT | yes | |||
notes | TEXT | yes |
- Table check:
paused = FALSE OR paused_reason IS NOT NULL - Unique index
customers_name_idxon(lower(name)). Customer names are unique ignoring case (section 5). - App rule (section 7): an import row finds its customer by name, trimmed and ignoring case, and its contact_email is used only when the customer is created.
invoices
One row per invoice, imported from the accounting software.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
number | TEXT | no | Unique | the accounting software's invoice number, for example INV-1032 | |
customer_id | TEXT | no | References customers.id | ||
issue_date | DATE | no | |||
due_date | DATE | no | |||
amount_cents | INTEGER | no | Check: amount_cents >= 0 | ||
balance_cents | INTEGER | no | never below zero (R1), never above the amount (section 7)Check: balance_cents >= 0 | ||
status | TEXT | no | 'open' | Check: status IN ('open', 'paid', 'disputed', 'written_off') | |
pay_link | TEXT | yes | optional URL, listed by the {pay_links} merge field | ||
dispute_reason | TEXT | yes | required while disputed | ||
promise_date | DATE | yes | no reminders while this is today or later (R3) | ||
not_in_latest_import | BOOLEAN | no | FALSE | open invoice missing from the latest file; never marks it paid | |
last_imported_at | TIMESTAMP | yes |
- Table check:
due_date >= issue_date - Table check:
balance_cents <= amount_cents - Table check:
status NOT IN ('paid', 'written_off') OR balance_cents = 0 - Table check:
status <> 'disputed' OR dispute_reason IS NOT NULL - Index
invoices_customer_idxon(customer_id). The invoice list, the aging report and the daily run read open invoices by due date. - Index
invoices_due_idxon(status, due_date) - App rule (R4): a promise date is from today to today + 30 days, on an open invoice with a balance; a broken promise is cleared and noted.
- App rule (R5): only open invoices can be disputed; resolving reopens the invoice and clears dispute_reason (the notes keep it).
- App rule (R6): a write-off sets the balance to zero and the status to written_off; amount_cents keeps the original amount.
payments
One row per payment recorded in the app. Payments are never edited or deleted.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
invoice_id | TEXT | no | References invoices.id | ||
date | DATE | no | business date of the payment | ||
amount_cents | INTEGER | no | Check: amount_cents > 0 | ||
note | TEXT | yes | |||
recorded_by | TEXT | no | References people.id |
- Index
payments_invoice_idxon(invoice_id) - App rule (R1): a payment is not more than the balance, dated today or earlier, on an open or disputed invoice; one equal to the balance makes the invoice paid.
templates
One row per email template used by a reminder step.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | Unique | ||
subject | TEXT | no | one line | ||
body | TEXT | no | plain text |
- App rule (R9): subject and body may use only {customer_name}, {invoice_table}, {total_due}, {oldest_days_overdue}, {pay_links} and {from_name}, case-sensitive.
steps
One row per step of the reminder schedule. Steps are turned off, never deleted, because reminders_sent refers to them.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | Unique | ||
offset_days | INTEGER | no | Unique | days from the due date; negative is before itCheck: offset_days BETWEEN -365 AND 365 | |
template_id | TEXT | no | References templates.id | ||
position | INTEGER | no | display order, following offset_days | ||
active | BOOLEAN | no | TRUE | ||
is_final | BOOLEAN | no | FALSE | the Final notice: copies the account owner and starts the Needs a call clock |
- Unique index
steps_one_final_idxon(is_final)whereis_final = TRUE. At most one step is the Final notice. - App rule (section 5): exactly one step is the Final notice; the index below allows at most one.
notifications
The email outbox: one row per email, written before it is sent.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
at | TIMESTAMP | no | when it was written | ||
sent_at | TIMESTAMP | yes | |||
kind | TEXT | no | Check: kind IN ('reminder', 'final_notice', 'promise_broken', 'import_finished') | ||
to_email | TEXT | no | |||
cc | TEXT | yes | comma-separated addresses, duplicates removed | ||
subject | TEXT | no | |||
body | TEXT | no | plain text | ||
status | TEXT | no | held: written while sending was off; never sent laterCheck: status IN ('queued', 'sent', 'failed', 'logged', 'held') | ||
error | TEXT | yes | failed emails are not retried automatically |
reminders_sent
One row per invoice per step sent, so no invoice gets the same step twice (R2). One email can cover several invoices.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
invoice_id | TEXT | no | References invoices.id | ||
step_id | TEXT | no | References steps.id | ||
notification_id | TEXT | no | References notifications.id | the customer's email that listed this invoice | |
sent_on | DATE | no | business date of the run; a held email counts as sent |
- Unique together:
invoice_id, step_id
notes
The append-only notes timeline. A note belongs to exactly one invoice or one customer.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
invoice_id | TEXT | yes | References invoices.id | ||
customer_id | TEXT | yes | References customers.id | ||
at | TIMESTAMP | no | |||
by_id | TEXT | yes | References people.id | empty for notes the job writes | |
kind | TEXT | no | Check: kind IN ('note', 'call', 'promise', 'promise_broken', 'dispute', 'dispute_resolved', 'pause', 'resume', 'payment', 'write_off', 'import') | ||
text | TEXT | yes | may be empty only where the spec makes the note optional, such as resuming a customer |
- Table check:
(invoice_id IS NULL AND customer_id IS NOT NULL) OR (invoice_id IS NOT NULL AND customer_id IS NULL) - Index
notes_invoice_idxon(invoice_id, at) - Index
notes_customer_idxon(customer_id, at) - App rule (R10): notes are never edited or deleted.
import_runs
One row per CSV import.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
at | TIMESTAMP | no | |||
by_id | TEXT | no | References people.id | ||
file_name | TEXT | no | |||
new_count | INTEGER | no | Check: new_count >= 0 | ||
updated_count | INTEGER | no | Check: updated_count >= 0 | ||
unchanged_count | INTEGER | no | Check: unchanged_count >= 0 | ||
error_count | INTEGER | no | Check: error_count >= 0 | ||
missing_count | INTEGER | no | open invoices in the app but not in the fileCheck: missing_count >= 0 |
daily_runs
One row per business day's reminder run, written in the same transaction as its emails, which keeps the run to once a day.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
run_date | DATE | no | Unique | ||
at | TIMESTAMP | no | |||
run_by | TEXT | yes | References people.id | the admin who pressed Run now; empty when the job ran it | |
snapshot | TEXT | yes | JSON as text: what was sent and skipped, with reasons |
events
The append-only audit log: one row per change.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
at | TIMESTAMP | no | |||
actor | TEXT | yes | References people.id | empty for the job | |
type | TEXT | no | |||
data | TEXT | yes | JSON as text |
- App rule (R10): events are never edited or deleted.
The SQL
The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download invoice-reminders.sql or copy it from here.
-- Invoice Reminders database schema
-- Source: https://personalsoftwarefactory.com/blueprints/invoice-reminders (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.
-- The single settings row for the one company, sender and time zone.
-- App rule (R8): turning sending_enabled on is admin-only and needs a confirmation the server checks.
CREATE TABLE settings (
id TEXT PRIMARY KEY CHECK (id = 'settings'), -- exactly one row
company_name TEXT NOT NULL,
company_domain TEXT NOT NULL, -- the sign-in domain, for example northwind.example
from_name TEXT NOT NULL, -- used by the {from_name} merge field
from_email TEXT NOT NULL, -- the shared address reminders are sent from
reply_to TEXT,
time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; business dates are calendar dates in this zone
send_time TEXT NOT NULL DEFAULT '09:00' CHECK (send_time LIKE '__:__' AND send_time BETWEEN '00:00' AND '23:59' AND substr(send_time, 4, 2) < '60'), -- HH:MM, 24-hour, company time
sending_enabled BOOLEAN NOT NULL DEFAULT FALSE, -- off: every customer reminder is written to the outbox as held
account_owner_cc_on_final BOOLEAN NOT NULL DEFAULT TRUE -- copy the active account owner on the Final notice
);
-- One row per person who signs in.
-- App rule (section 4): an admin cannot deactivate or demote themselves, so there is always an active admin.
CREATE TABLE people (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE, -- must be on settings.company_domain to sign in
role TEXT NOT NULL CHECK (role IN ('admin', 'member')),
active BOOLEAN NOT NULL DEFAULT TRUE
);
-- One row per customer who owes money.
-- App rule (section 7): an import row finds its customer by name, trimmed and ignoring case, and its contact_email is used only when the customer is created.
CREATE TABLE customers (
id TEXT PRIMARY KEY,
name TEXT NOT NULL, -- unique, compared trimmed and ignoring case (index below)
contact_email TEXT, -- may be empty; the run then skips the customer's invoices (No contact email)
cc_emails TEXT NOT NULL DEFAULT '', -- comma-separated list of valid addresses, copied on every reminder
account_owner_id TEXT REFERENCES people(id), -- may be empty for customers created by import
paused BOOLEAN NOT NULL DEFAULT FALSE, -- paused customers get no reminders (R3)
paused_reason TEXT,
notes TEXT,
CHECK (paused = FALSE OR paused_reason IS NOT NULL)
);
-- One row per invoice, imported from the accounting software.
-- App rule (R4): a promise date is from today to today + 30 days, on an open invoice with a balance; a broken promise is cleared and noted.
-- App rule (R5): only open invoices can be disputed; resolving reopens the invoice and clears dispute_reason (the notes keep it).
-- App rule (R6): a write-off sets the balance to zero and the status to written_off; amount_cents keeps the original amount.
CREATE TABLE invoices (
id TEXT PRIMARY KEY,
number TEXT NOT NULL UNIQUE, -- the accounting software's invoice number, for example INV-1032
customer_id TEXT NOT NULL REFERENCES customers(id),
issue_date DATE NOT NULL,
due_date DATE NOT NULL,
amount_cents INTEGER NOT NULL CHECK (amount_cents >= 0),
balance_cents INTEGER NOT NULL CHECK (balance_cents >= 0), -- never below zero (R1), never above the amount (section 7)
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'paid', 'disputed', 'written_off')),
pay_link TEXT, -- optional URL, listed by the {pay_links} merge field
dispute_reason TEXT, -- required while disputed
promise_date DATE, -- no reminders while this is today or later (R3)
not_in_latest_import BOOLEAN NOT NULL DEFAULT FALSE, -- open invoice missing from the latest file; never marks it paid
last_imported_at TIMESTAMP,
CHECK (due_date >= issue_date),
CHECK (balance_cents <= amount_cents),
CHECK (status NOT IN ('paid', 'written_off') OR balance_cents = 0),
CHECK (status <> 'disputed' OR dispute_reason IS NOT NULL)
);
-- One row per payment recorded in the app. Payments are never edited or deleted.
-- App rule (R1): a payment is not more than the balance, dated today or earlier, on an open or disputed invoice; one equal to the balance makes the invoice paid.
CREATE TABLE payments (
id TEXT PRIMARY KEY,
invoice_id TEXT NOT NULL REFERENCES invoices(id),
date DATE NOT NULL, -- business date of the payment
amount_cents INTEGER NOT NULL CHECK (amount_cents > 0),
note TEXT,
recorded_by TEXT NOT NULL REFERENCES people(id)
);
-- One row per email template used by a reminder step.
-- App rule (R9): subject and body may use only {customer_name}, {invoice_table}, {total_due}, {oldest_days_overdue}, {pay_links} and {from_name}, case-sensitive.
CREATE TABLE templates (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
subject TEXT NOT NULL, -- one line
body TEXT NOT NULL -- plain text
);
-- One row per step of the reminder schedule. Steps are turned off, never deleted, because reminders_sent refers to them.
-- App rule (section 5): exactly one step is the Final notice; the index below allows at most one.
CREATE TABLE steps (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
offset_days INTEGER NOT NULL UNIQUE CHECK (offset_days BETWEEN -365 AND 365), -- days from the due date; negative is before it
template_id TEXT NOT NULL REFERENCES templates(id),
position INTEGER NOT NULL, -- display order, following offset_days
active BOOLEAN NOT NULL DEFAULT TRUE,
is_final BOOLEAN NOT NULL DEFAULT FALSE -- the Final notice: copies the account owner and starts the Needs a call clock
);
-- The email outbox: one row per email, written before it is sent.
CREATE TABLE notifications (
id TEXT PRIMARY KEY,
at TIMESTAMP NOT NULL, -- when it was written
sent_at TIMESTAMP,
kind TEXT NOT NULL CHECK (kind IN ('reminder', 'final_notice', 'promise_broken', 'import_finished')),
to_email TEXT NOT NULL,
cc TEXT, -- comma-separated addresses, duplicates removed
subject TEXT NOT NULL,
body TEXT NOT NULL, -- plain text
status TEXT NOT NULL CHECK (status IN ('queued', 'sent', 'failed', 'logged', 'held')), -- held: written while sending was off; never sent later
error TEXT -- failed emails are not retried automatically
);
-- One row per invoice per step sent, so no invoice gets the same step twice (R2). One email can cover several invoices.
CREATE TABLE reminders_sent (
id TEXT PRIMARY KEY,
invoice_id TEXT NOT NULL REFERENCES invoices(id),
step_id TEXT NOT NULL REFERENCES steps(id),
notification_id TEXT NOT NULL REFERENCES notifications(id), -- the customer's email that listed this invoice
sent_on DATE NOT NULL, -- business date of the run; a held email counts as sent
UNIQUE (invoice_id, step_id)
);
-- The append-only notes timeline. A note belongs to exactly one invoice or one customer.
-- App rule (R10): notes are never edited or deleted.
CREATE TABLE notes (
id TEXT PRIMARY KEY,
invoice_id TEXT REFERENCES invoices(id),
customer_id TEXT REFERENCES customers(id),
at TIMESTAMP NOT NULL,
by_id TEXT REFERENCES people(id), -- empty for notes the job writes
kind TEXT NOT NULL CHECK (kind IN ('note', 'call', 'promise', 'promise_broken', 'dispute', 'dispute_resolved', 'pause', 'resume', 'payment', 'write_off', 'import')),
text TEXT, -- may be empty only where the spec makes the note optional, such as resuming a customer
CHECK ((invoice_id IS NULL AND customer_id IS NOT NULL) OR (invoice_id IS NOT NULL AND customer_id IS NULL))
);
-- One row per CSV import.
CREATE TABLE import_runs (
id TEXT PRIMARY KEY,
at TIMESTAMP NOT NULL,
by_id TEXT NOT NULL REFERENCES people(id),
file_name TEXT NOT NULL,
new_count INTEGER NOT NULL CHECK (new_count >= 0),
updated_count INTEGER NOT NULL CHECK (updated_count >= 0),
unchanged_count INTEGER NOT NULL CHECK (unchanged_count >= 0),
error_count INTEGER NOT NULL CHECK (error_count >= 0),
missing_count INTEGER NOT NULL CHECK (missing_count >= 0) -- open invoices in the app but not in the file
);
-- One row per business day's reminder run, written in the same transaction as its emails, which keeps the run to once a day.
CREATE TABLE daily_runs (
id TEXT PRIMARY KEY,
run_date DATE NOT NULL UNIQUE,
at TIMESTAMP NOT NULL,
run_by TEXT REFERENCES people(id), -- the admin who pressed Run now; empty when the job ran it
snapshot TEXT -- JSON as text: what was sent and skipped, with reasons
);
-- The append-only audit log: one row per change.
-- App rule (R10): events are never edited or deleted.
CREATE TABLE events (
id TEXT PRIMARY KEY,
at TIMESTAMP NOT NULL,
actor TEXT REFERENCES people(id), -- empty for the job
type TEXT NOT NULL,
data TEXT -- JSON as text
);
-- Customer names are unique ignoring case (section 5).
CREATE UNIQUE INDEX customers_name_idx ON customers (lower(name));
-- At most one step is the Final notice.
CREATE UNIQUE INDEX steps_one_final_idx ON steps (is_final) WHERE is_final = TRUE;
-- The invoice list, the aging report and the daily run read open invoices by due date.
CREATE INDEX invoices_customer_idx ON invoices (customer_id);
CREATE INDEX invoices_due_idx ON invoices (status, due_date);
CREATE INDEX payments_invoice_idx ON payments (invoice_id);
CREATE INDEX notes_invoice_idx ON notes (invoice_id, at);
CREATE INDEX notes_customer_idx ON notes (customer_id, at);Seed data
Loads the settings row, 3 people, 6 customers and 7 invoices from section 14, with dates fixed to Thursday, October 15, 2026, the day the spec's examples use; issue dates and amounts follow the spec's rule (amount equals balance, issued 30 days before due). Left out: the 5 default templates and their steps, because the spec only describes the template text and each step needs one; the earlier reminders and their sent emails, which need those steps and email bodies the spec does not give; and Theo's promise and pause notes, whose text the spec does not give. Payments, imports, daily runs and events start empty.
Download invoice-reminders-seed.sql (17 rows). It loads with foreign keys enforced on both engines.
Load it
SQLite, into a new file:
curl -s https://personalsoftwarefactory.com/schemas/invoice-reminders.sql https://personalsoftwarefactory.com/schemas/invoice-reminders-seed.sql | sqlite3 invoice_reminders.dbPostgreSQL, into a new database:
createdb invoice_reminders && curl -s https://personalsoftwarefactory.com/schemas/invoice-reminders.sql https://personalsoftwarefactory.com/schemas/invoice-reminders-seed.sql | psql -v ON_ERROR_STOP=1 invoice_remindersSQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.
Design decisions
Balances stay between zero and the invoice amount
Rule R1 says balances never go below zero, and section 7 rejects an import row whose balance is more than its amount. Two checks hold both, and a third makes paid and written-off invoices carry a zero balance, since a payment equal to the balance marks an invoice paid and R6 writes off by setting the balance to zero. amount_cents keeps the original amount after a write-off.
Each invoice gets each step at most once
Rule R2 says each invoice gets each step at most once, enforced as a unique ReminderSent. The unique key on reminders_sent (invoice_id, step_id) makes a second send of the same step fail, which is what stops a retried or doubled run from chasing a customer twice. One email can list several invoices, so each row points at the email that carried it.
The daily run is a row with a unique date
From the build test · field report
Section 5 writes one DailyRun per date in the same transaction as the run's emails and ReminderSent rows, and that is what keeps the run to once a day. The build test found that nothing recorded that the day's run had happened, which safe to run twice depends on (gap 39); the unique run_date is the fix. Two people pressing Run now at once then get one run, as section 13 requires.
Steps have unique offsets and one Final notice
From the build test · field report
The build test found that steps can be renamed and added, so the Final notice couldn't be identified by name, and that two steps sharing an offset made the current step ambiguous (gaps 22 and 23). The spec now gives Step an is_final flag on exactly one step and makes offsets unique, from -365 to 365 days. A partial unique index allows at most one final step; the app keeps it at exactly one.
A note belongs to exactly one invoice or one customer
From the build test · field report
The build test found that invoice_id or customer_id on Note could mean either or both (gap 58); the spec now says exactly one. A table check refuses a note with both or neither. A customer's page then shows its own notes plus the notes on its invoices, newest first.
A broken promise is a note, not a status
From the build test · field report
Rule R4 records a promise as promise_date on the invoice and, when it passes unpaid, writes a promise_broken note and clears the date. The build test found that R4 marked a promise broken but no field held that (gap 44). Keeping the history in notes leaves the invoice with one current promise, which is all the run needs to skip it.
Held emails are a status in the outbox
Rule R8 holds every customer reminder in the outbox while sending_enabled is off, so a new install can never email customers by accident. Held is one of the notification statuses, and held emails stay held when sending is turned on later. A held reminder still writes its reminders_sent row, because section 6 counts it as sent.
Example queries
The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.
Aging report bucket totals
SELECT SUM(CASE WHEN i.due_date >= '2026-10-15' THEN i.balance_cents ELSE 0 END) AS not_yet_due, SUM(CASE WHEN i.due_date BETWEEN '2026-09-15' AND '2026-10-14' THEN i.balance_cents ELSE 0 END) AS days_1_30, SUM(CASE WHEN i.due_date BETWEEN '2026-08-16' AND '2026-09-14' THEN i.balance_cents ELSE 0 END) AS days_31_60, SUM(CASE WHEN i.due_date BETWEEN '2026-07-17' AND '2026-08-15' THEN i.balance_cents ELSE 0 END) AS days_61_90, SUM(CASE WHEN i.due_date <= '2026-07-16' THEN i.balance_cents ELSE 0 END) AS over_90_days, SUM(i.balance_cents) AS total FROM invoices i WHERE i.status IN ('open', 'disputed') AND i.balance_cents > 0;On the seed data with today 2026-10-15: 1 row, matching section 14. Not yet due 1130000 cents, 1 to 30 days 1960000, 31 to 60 days 90000, nothing older, total 3180000 ($31,800).
Aging report by customer
SELECT c.name AS customer, SUM(CASE WHEN i.due_date >= '2026-10-15' THEN i.balance_cents ELSE 0 END) AS not_yet_due, SUM(CASE WHEN i.due_date BETWEEN '2026-09-15' AND '2026-10-14' THEN i.balance_cents ELSE 0 END) AS days_1_30, SUM(CASE WHEN i.due_date BETWEEN '2026-08-16' AND '2026-09-14' THEN i.balance_cents ELSE 0 END) AS days_31_60, SUM(CASE WHEN i.due_date BETWEEN '2026-07-17' AND '2026-08-15' THEN i.balance_cents ELSE 0 END) AS days_61_90, SUM(CASE WHEN i.due_date <= '2026-07-16' THEN i.balance_cents ELSE 0 END) AS over_90_days, SUM(i.balance_cents) AS total FROM invoices i JOIN customers c ON c.id = i.customer_id WHERE i.status IN ('open', 'disputed') AND i.balance_cents > 0 GROUP BY c.name ORDER BY c.name;On the seed data with today 2026-10-15: 6 rows, one per customer by name. Acme Corp has 480000 cents not yet due and 250000 at 1 to 30 days, 730000 in total.
Open invoices, overdue first
SELECT i.number, c.name AS customer, i.due_date, i.balance_cents, i.status, i.promise_date, CASE WHEN i.due_date < '2026-10-15' THEN 'overdue' ELSE 'not yet due' END AS due_state FROM invoices i JOIN customers c ON c.id = i.customer_id WHERE i.status IN ('open', 'disputed') AND i.balance_cents > 0 ORDER BY i.due_date, i.number;On the seed data with today 2026-10-15: 7 rows. INV-1025 for Olsen & Co (due 2026-08-31) is first; five are overdue and two are not yet due, 2050000 cents overdue in all.
Invoices the run must not chase
SELECT i.number, c.name AS customer, CASE WHEN c.paused = TRUE THEN 'Customer paused: ' || c.paused_reason WHEN i.status = 'disputed' THEN 'Disputed: ' || i.dispute_reason WHEN i.promise_date >= '2026-10-15' THEN 'Promised to pay' WHEN c.contact_email IS NULL THEN 'No contact email' END AS skip_reason, i.promise_date FROM invoices i JOIN customers c ON c.id = i.customer_id WHERE i.status IN ('open', 'disputed') AND i.balance_cents > 0 AND (c.paused = TRUE OR i.status = 'disputed' OR i.promise_date >= '2026-10-15' OR c.contact_email IS NULL) ORDER BY i.number;On the seed data with today 2026-10-15: 2 rows, each with the first reason in section 6's skip order. INV-1035 (Kestrel Schools, promised to pay, promise date 2026-10-20) and INV-1037 (Harbor Clinic, customer paused: disputing the September hours).
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 comes from
Tables are the easy half. This schema is section 5 of the Invoice Reminders blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 18 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 18 of 18 acceptance checks in a browser.
- Read the Invoice Reminders blueprint: rules, screens, build prompts and acceptance checks
- Read the build-test field report: what the test build got wrong and what was not tested
- All database schemas
- How we test blueprints
Follow new blueprints
Every new blueprint and build test appears in the feed and in the public GitHub repository. No account, no email list.