migration
Write database migrations. Use when creating schema changes, adding tables, columns, indexes, or modifying database structure.
Installation
Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.
Voir la source ↗GuidesInstructions
Database Migrations
Before Writing a Migration
Always read the current schema first:
cat drizzle/schema.sql
This file contains a pg_dump of the current database schema. Review it to understand existing tables, columns, constraints, and indexes before making changes.
Writing Migrations
Migrations are written as raw SQL files in the drizzle/ folder. We do NOT use drizzle-kit generate.
File Naming
Use an incrementing numeric prefix followed by a descriptive name:
0035_add_user_preferences.sql
Check existing migrations to find the next available number:
ls drizzle/*.sql | tail -5
SQL Format
Separate statements with --> statement-breakpoint:
-- Description of what this migration does
CREATE TABLE example (
id uuid PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
--> statement-breakpoint
CREATE INDEX idx_example_user ON example(user_id);
PostgreSQL Conventions
-
IDs: Use
uuidwith UUIDv7 (time-ordered) -
Timestamps: Always use
timestamptz, nevertimestamp -
Foreign keys: Include
ON DELETE CASCADEfor user-owned data -
Case-insensitive text: Use
citextextension when needed
Registering Migrations
Migrations won't run unless registered in the journal.
Edit drizzle/meta/_journal.json and add an entry:
{
"idx": 35,
"version": "7",
"when": 1767500000000,
"tag": "0035_add_user_preferences",
"breakpoints": true
}
-
idx: Next sequential index -
when: Unix timestamp in milliseconds (use current time) -
tag: Filename without.sqlextension -
breakpoints: Alwaystrue
Enum Changes
Enum additions MUST be in their own migration file.
PostgreSQL doesn't allow using new enum values in the same transaction they were added. If you need to add an enum value and use it:
- Migration 1: Add the enum value
- Migration 2: Use the new enum value
Running Migrations
# Run migrations on development database
pnpm db:migrate
# Run migrations on test database
pnpm db:migrate:test
Verifying Migrations
When Docker/Postgres is available: Always run the integration tests after writing a migration:
pnpm test:integration
This runs the migrations against a real Postgres instance and updates drizzle/schema.sql with the current database state.
In cloud environments without Docker: If you cannot run Docker, manually update drizzle/schema.sql as best you can to reflect your migration changes. The schema dump will be corrected automatically by a future run of the integration tests.
Exemples
Consultez les exemples du dépôt source.
Voir la source ↗Historique source
- Date d’indexation
- 2026-01-20
- Mise à jour de l’index
- 2026-01-26
- Branch
- main
Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!