Database Migrations
The CoW Protocol BFF uses TypeORM for database management through thelibs/repositories library. Migrations serve as version control for your database schema, enabling teams to track changes, apply them consistently across environments, and rollback when necessary.
Core Commands
Manual Migration Creation
Generate an empty file and write SQL directly:up() and down() methods:
Auto-Generated Migrations
Define TypeORM entities first, then generate migration files based on schema differences:Running Migrations
Reverting Migrations
Common Patterns
Adding a Table
Adding a Column
Renaming a Column
Data Migration
Best Practices
- Always implement complete
down()methods for rollback capability - Test migrations bidirectionally before production deployment
- Never modify migrations after production deployment - create new migrations instead
- Use descriptive naming conventions (e.g.,
add-user-email-index) - Back up databases before production migrations
- Make changes backward-compatible through staged deployments
Production Deployment
The recommended deployment sequence:- Back up the database
- Run migrations before deploying application code:
- Deploy the new application version
- Monitor for issues post-deployment
- Rollback if needed:
Naming Conventions
Use clear, action-oriented file names:add-user-tableadd-order-status-columncreate-notification-indexrename-user-name-to-display-namemigrate-order-status-data