This project uses a custom raw SQL migration script to manage database schema changes reliably.
src/db/migrations/: Directory where all.sqlmigration files are stored.src/db/migrate.ts: The script that executes pending migrations against the database.
To add a new migration, create a new .sql file in src/db/migrations/.
Naming Convention:
Use a sequential prefix followed by a descriptive name: XXX_description.sql (e.g., 003_add_user_status.sql).
All .sql files are sorted alphabetically when applied, so the prefix ensures they run in the correct order. The migration script uses a schema_version table to track which files have already been applied based on their filename.
Migrations rely on the DATABASE_URL environment variable.
- Ensure your
.envfile has a validDATABASE_URL:DATABASE_URL="postgres://user:password@localhost:5432/revora"
- Run the migration script via npm:
npm run migrate
This command will:
- Compile the TypeScript code (
tsc). - Connect to the database specified by
DATABASE_URL. - Create the
schema_versiontable if it doesn't already exist. - Apply any
.sqlfile insrc/db/migrations/that hasn't been recorded inschema_version, within a transaction. - Record the applied filename in
schema_version.
If a migration fails mid-execution, the transaction will rollback, leaving your database safely unmodified.