Create a database migration with schema and type updates. Use when modifying the database schema, adding tables, columns, or indexes.
Step-by-step process for making database schema changes.
pnpm migrate:create {description}
Example: pnpm migrate:create add-foo-column
This creates a timestamped file in migrations/.
Open migrations/TIMESTAMP_{description}.js and add your SQL:
exports.up = async (db) => {
await db.query(`
ALTER TABLE your_table
ADD COLUMN new_column TEXT
`)
}
exports.down = async (db) => {
await db.query(`
ALTER TABLE your_table
DROP COLUMN new_column
`)
}
Edit src/lib/schema.sql to match the new schema state. This file is used for creating fresh test databases and must always reflect the current schema.
If the change affects types, update src/lib/db-types.ts to match.
If needed, update functions in src/lib/db.ts to use the new schema.
pnpm migrate
schema.sqldb-types.ts match the schemapnpm test| File | Purpose |
|---|---|
migrations/*.js | Production migration files |
src/lib/schema.sql | Full schema for fresh test DBs |
src/lib/db-types.ts | TypeScript types for DB entities |
src/lib/db.ts | Query functions |
See doc/arch/data-layer.md for the full data layer architecture.