Creating Migrations
Migrations are version control for your database schema. This guide explains how to create migrations that track changes to your database structure.
What are Migrations?
Section titled “What are Migrations?”Migrations are files that define changes to your database schema:
- Creating tables
- Adding/removing columns
- Changing column types
- Adding indexes
- Modifying constraints
Think of migrations as Git commits for your database structure.
Why Use Migrations?
Section titled “Why Use Migrations?”Without Migrations
Section titled “Without Migrations”// Change your modelconst User = Model('user', { newField: { type: 'string', required: true, min: 2, max: 100, }, // Added this});Problem: Database doesn’t know about the new field!
With Migrations
Section titled “With Migrations”- Change your model
- Create migration
- Run migration
- Database updated ✓
Benefits:
- Track schema changes over time
- Share changes with team
- Apply changes consistently across environments
- Rollback changes if needed
When to Create Migrations
Section titled “When to Create Migrations”Create a migration when you:
- ✅ Create a new model
- ✅ Add/remove fields from models
- ✅ Change field types or constraints
- ✅ Add indexes or relationships
- ✅ Rename tables or columns
Don’t need migrations for:
- ❌ Changing controller logic
- ❌ Updating routes
- ❌ Modifying business logic
Creating Your First Migration
Section titled “Creating Your First Migration”Step 1: Create a Model
Section titled “Step 1: Create a Model”import { Model } from 'stratus-ts';
const User = Model('user', { name: { type: 'string', required: true, min: 2, max: 100, }, email: { type: 'email', unique: true, required: true, }, password: { type: 'string', required: true, select: false, },});
export { User };Step 2: Generate Migration
Section titled “Step 2: Generate Migration”Run the CLI command:
st create db:migrationor
stratus-ts create db:migrationif you want you can give also name
st create db:migration user-tableStep 3: Review the Migration
Section titled “Step 3: Review the Migration”Open the generated file:
generely find in .migrations/sql untill change in settings
change migration directly
Section titled “change migration directly”export default { PORT, APPS, DATABASE, TEMPLATE_DIR, MIGRATION: { TABLE: 'tests', PATH: '.migrations', },} satisfies SettingsTypeNext Steps
Section titled “Next Steps”Now that you’ve created migrations:
- Running Migrations - Apply migrations to database
Documentation Notice:
The majority of this documentation was created with assistance from Claude (Anthropic AI) and refined with modifications by Rahul Roy, the project owner. We believe in transparency about our development process.