Skip to content

Creating Migrations

Migrations are version control for your database schema. This guide explains how to create migrations that track changes to your database structure.

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.

src/users/models.ts
// Change your model
const User = Model('user', {
newField: {
type: 'string',
required: true,
min: 2,
max: 100,
}, // Added this
});

Problem: Database doesn’t know about the new field!

  1. Change your model
  2. Create migration
  3. Run migration
  4. Database updated ✓

Benefits:

  • Track schema changes over time
  • Share changes with team
  • Apply changes consistently across environments
  • Rollback changes if needed

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
src/users/models.ts
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 };

Run the CLI command:

terminal
st create db:migration

or

terminal
stratus-ts create db:migration

if you want you can give also name

terminal
st create db:migration user-table

Open the generated file:

generely find in .migrations/sql untill change in settings

src/settings.ts
export default {
PORT,
APPS,
DATABASE,
TEMPLATE_DIR,
MIGRATION: {
TABLE: 'tests',
PATH: '.migrations',
},
} satisfies SettingsType

Now that you’ve created migrations: