migrate command applies pending database schema migrations. Migrations are embedded in the oForum binary and run automatically on server startup.
Usage
You typically don’t need to run this command manually. The
serve command automatically runs migrations on startup. Use migrate when you want to apply migrations without starting the server.How It Works
- Load migrations - Reads embedded SQL files from the binary
- Connect to database - Establishes PostgreSQL connection
- Check status - Determines which migrations have already been applied
- Apply pending - Runs new migrations in order
- Update schema - Records applied migrations in
schema_migrationstable
Environment Variables
PostgreSQL connection string.
Output
Success (New Migrations)
Success (Already Up to Date)
Error (Database Unreachable)
Error (Migration Failed)
Embedded Migrations
oForum uses embedded migrations compiled into the binary. The migration files are located in themigrations/ directory of the source repository:
Migration Naming
Migrations follow the pattern:version- Sequential number (e.g.,000001,000002)description- Brief name (e.g.,initial,add_roles)direction- Eitherupordown
Migrations are embedded at build time. You cannot add new migrations without rebuilding the binary.
Migration Status
Migration state is tracked in theschema_migrations table:
version- Current migration versiondirty- Whether the last migration failed mid-execution
Troubleshooting
Migration Failed Mid-Execution
If a migration fails partway through, the database may be in a “dirty” state:Migration Version Conflict
If you downgrade to an older binary with fewer migrations:Cannot Connect to Database
DATABASE_URL is correct:
Rollback
To roll back a migration:-
Manually run the down migration:
-
Update schema_migrations table:
Migration Safety
Migrations are designed to be safe for production:- Idempotent - Safe to run multiple times
- Atomic - Each migration runs in a transaction
- Ordered - Applied sequentially by version number
- Tracked - State stored in
schema_migrations
Some operations (like
DROP COLUMN) cannot be rolled back. Always test migrations on a staging database first.