Framework
defineMigration()
Function: defineMigration()
function defineMigration(definition): RegisteredMigrationDefine a migration script.
Migrations are used to apply breaking schema changes that cannot be auto-applied on startup. Each migration should be:
- Idempotent: Safe to run multiple times
- Atomic: Either fully applies or fully rolls back
- Testable: Can be verified in a test environment
Parameters
| Parameter | Type | Description |
|---|---|---|
definition | MigrationDefinition | Migration definition with name, up, and optional down |
Returns
Registered migration with computed checksum
Example
// migrations/001_add_role_field.ts
import { defineMigration } from 'robo.js/flashcore'
export default defineMigration({
name: 'add_role_field',
async up(ctx) {
// Add default value for existing records
await ctx.model('user').updateMany({
where: { role: { equals: undefined } },
data: { role: 'user' }
})
},
async down(ctx) {
// Remove the field (optional rollback)
ctx.progress('Rollback not supported for this migration')
}
})