Database, migrations, pagination and indexes
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Default engine is PostgreSQL; choose another only with a written reason. Portability means any provider of the same engine by settings, not any engine.
- From class L: numbered hand-written migrations, never edited after applying; destructive commands need an explicit marker and the owner’s approval.
- Multilingual fixed content goes in a row-based translation table, never
name_tr,name_encolumns.
Which class?
This topic has items from class S. The blocks below are cumulative: read up to your own class.
- S: 1 items
- M: 7 items
- L: 8 items
- XL: 2 items
Audit checklist
Every project
- A privacy statement matches what the site really does
- What: If the site has a form, analytics or cookies, a privacy statement says what is collected, why, and for how long; if nothing is collected the site does not claim otherwise.
- How to check: List forms, cookies and external requests in the browser’s network panel; compare with the privacy text.
- Evidence: Network panel list; privacy page.
+ If it has users, personal data or API keys (M)
- PostgreSQL by default, through an ORM or query builder
- What: The default database is PostgreSQL (another choice has a written reason); queries go through an ORM or query builder; raw SQL lives in one module; connection settings come from configuration.
- How to check: Read the database config and decision notes; search for raw SQL.
- Evidence: Decision note; list of raw SQL locations.
- Database connections use TLS with certificate checks
- What: The database connection uses TLS and certificate validation is never turned off.
- How to check: Read the connection string and driver options for SSL mode.
- Evidence: Connection settings.
- Deleting a record has a deliberate rule for related records
- What: For every link between records the code states what happens on delete (cascade only with a reason,
SET NULLfor “created in” links). - How to check: Read foreign key definitions; look at the
ON DELETErule of each. - Evidence: Schema file lines with a comment explaining each non-default rule.
- What: For every link between records the code states what happens on delete (cascade only with a reason,
- Fixed multilingual content uses a translation table
- What: Fixed texts (category names, notification templates) live in a row-based translation table (record id, language code, text); no
name_tr,name_encolumns; a new language is a new row. User-written text is stored in its own language and marked, not machine-translated. - How to check: Look for language-suffixed columns in the schema.
- Evidence: Schema with translation table.
- What: Fixed texts (category names, notification templates) live in a row-based translation table (record id, language code, text); no
- Automatic backups with a tested restore
- What: The database is backed up automatically, retention is written down, and a restore has actually been tried.
- How to check: Find the backup job and its schedule; find the record of the last restore test.
- Evidence: Backup schedule, retention setting and dated restore-test record.
- Personal data fields are documented
- What: For every personal data field the purpose, retention time and legal basis (KVKK, GDPR) are written down.
- How to check: List the database columns and form fields with personal data; find the matching entries in the data note.
- Evidence: Data note listing each field with purpose, period and basis.
- One deletion schedule covers all data
- What: Every kind of data has a deletion period, collected in one deletion schedule document, and the code implements it.
- How to check: Find the document; for each row find the job or code that deletes the data.
- Evidence: Schedule document and the matching job names.
+ If it has an admin panel, file uploads, many users or a store app (L)
- Schema changes use a migration ledger
- What: Schema changes are numbered, hand-written files; applied files are never edited or deleted; automatic schema sync is never run on a database with data.
- How to check: Look at the migrations folder history; search scripts for automatic sync commands (for example
drizzle-kit push). - Evidence: Migration folder; absence of sync commands in deploy scripts.
- Data-destroying commands need explicit approval
- What: Dropping tables or columns, TRUNCATE, DELETE and type changes carry an explicit approval marker and reason in the file, and the owner’s approval is cited in the commit.
- How to check: Search migrations for destructive statements and check for the marker and commit reference.
- Evidence: Migration lines with marker; commit message.
- Lists use cursor pagination and indexes are checked
- What: Every list is paginated by cursor (no OFFSET, no unbounded queries); new queries are checked with
EXPLAIN; indexes are added to both schema and migration. - How to check: Search for
OFFSETand queries withoutLIMIT; look for EXPLAIN notes; compare schema and migration indexes. - Evidence: Endpoint list with cursor parameters; EXPLAIN output for new queries.
- What: Every list is paginated by cursor (no OFFSET, no unbounded queries); new queries are checked with
- Bulk deletes are batched and ordered
- What: Mass deletions run in batches, oldest first, so no rows wait forever; every deletion path respects legal holds.
- How to check: Read the cleanup jobs.
- Evidence: Job code with batch size and ordering; hold check.
- Deletion paths respect legal holds
- What: If a legal hold may be required, every deletion path checks it, and no two different paths delete the same data.
- How to check: List the deletion paths and look for the hold check.
- Evidence: Code lines; path list.
- A skipped cleanup is an alarm
- What: If a cleanup job cannot get its lock or is skipped, it exits with a distinct code and raises an alarm; changed text and deleted files are kept for a short fixed period (for example 7 days) when needed.
- How to check: Block the lock and run the job.
- Evidence: Exit code; alarm.
- An empty database is set up with one command
- What: Running the migrations in order and loading base data builds a working database from nothing with one command.
- How to check: Run the command against an empty database.
- Evidence: Run log.
- No “check then write” races
- What: State changes use conditional updates; counters are incremented in the database, not read and rewritten in code.
- How to check: Search for read-then-update patterns on balances, counters and status fields.
- Evidence: Code showing conditional updates; concurrency test.
+ If it targets millions of users (XL)
- Small frequent writes are batched
- What: Frequent small writes (views, progress, likes) are collected and sent as one request every few seconds.
- How to check: Watch the network calls of a screen over a minute.
- Evidence: Request count measurement.
- Region-aware design and Standard lines
- What: Every user has a data region and personal data, files and backups stay in it; moving a user or country between regions is possible with a verifiable, resumable tool; each code commit has a
Standard:line. - How to check: Read the user model and storage configuration; read recent commits.
- Evidence: Region field; commit examples.
- What: Every user has a data region and personal data, files and backups stay in it; moving a user or country between regions is possible with a verifiable, resumable tool; each code commit has a
Common mistakes
- Running automatic schema sync against a database with data.
- Editing a migration that was already applied.
OFFSETpagination on a large table.- Adding an index to the schema but not to a migration.
ON DELETE CASCADEchosen by default, silently deleting related records.- Bulk deletes that run unordered and leave some rows forever.
- Database traffic without TLS or with certificate checks off.
- Columns such as
title_trandtitle_eninstead of a translation table.
Sources
- PostgreSQL: PostgreSQL documentation — PostgreSQL Global Development Group
- PostgreSQL: Constraints — PostgreSQL Global Development Group
- PostgreSQL: Indexes — PostgreSQL Global Development Group
- PostgreSQL: EXPLAIN — PostgreSQL Global Development Group
- PostgreSQL: Using EXPLAIN — PostgreSQL Global Development Group
- PostgreSQL: LIMIT and OFFSET — PostgreSQL Global Development Group
- PostgreSQL: Secure TCP/IP Connections with SSL — PostgreSQL Global Development Group
- PostgreSQL: Privileges — PostgreSQL Global Development Group
- PostgreSQL: Explicit Locking — PostgreSQL Global Development Group
- PostgreSQL: Transaction Isolation — PostgreSQL Global Development Group
- SQL Injection Prevention Cheat Sheet — OWASP Cheat Sheet Series
- The Twelve-Factor App: III. Config — 12factor.net
- Pro Build Guide v1.2: compiled team practices — Pro Build Guide