The migration system detects field-level schema differences between your local type definitions and the previous snapshot, then generates migration files to safely apply those changes with data transformations.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tailor-platform/sdk/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Key Features:- Local snapshot-based diff detection between current types and previous migration snapshot
- Transaction-wrapped data migrations for atomicity - all changes commit or rollback together
- Automatic execution during apply - pending migrations run as part of
tailor-sdk apply - TypeScript migration scripts - type-safe data transformations using Kysely
Quick Start
1. Configure Migrations
Enable migrations in yourtailor.config.ts:
tailor.config.ts
2. Modify Your Schema
tailordb/user.ts
3. Generate Migration
4. Edit Migration Script
The generated migration script provides a template for data transformations:migrations/0001/migrate.ts
The
db.ts file contains Kysely Transaction types that reflect the schema state before the migration runs. This ensures type-safe data transformations based on the actual database structure at that point in time.5. Apply Migration
CLI Commands
Generate Migration
Check Migration Status
Set Migration Checkpoint
Migration Directory Structure
Migrations use a directory-based structure with 4-digit sequential numbering:0000- Initial schema snapshot (always starts at 0)0001- First schema change0002- Second schema change, etc.
Supported Schema Changes
| Change Type | Breaking? | Migration Script | Notes |
|---|---|---|---|
| Add optional field | No | No | Schema change only |
| Add required field | Yes | Yes | Script populates default values |
| Remove field | No | No | Schema change only - data is preserved |
| Change optional→required | Yes | Yes | Script sets defaults for null values |
| Change required→optional | No | No | Schema change only |
| Add index | No | No | Schema change only |
| Remove index | No | No | Schema change only |
| Add unique constraint | Yes | Yes | Script must resolve duplicate values |
| Remove unique constraint | No | No | Schema change only |
| Add enum value | No | No | Schema change only |
| Remove enum value | Yes | Yes | Script migrates records with removed values |
| Add type | No | No | Schema change only |
| Remove type | No | No | Schema change only - data is preserved |
| Change field type | - | - | Not supported - requires 3-step migration |
| Change array to single value | - | - | Not supported - requires 3-step migration |
| Change single value to array | - | - | Not supported - requires 3-step migration |
| Change foreign key relationship | Yes | Yes | Script updates existing references |
Unsupported Schema Changes
The following changes require a 3-step migration process:Field Type Change
Field type changes (e.g.,string → integer) are not directly supported. Use this migration strategy:
- Migration 1: Add a new field with the desired type (e.g.,
fieldName_new) and migrate data - Migration 2: Remove the old field
- Migration 3: Add the field with the original name and new type, migrate data from temporary field, then remove temporary field
Array Property Change
Changing between single value and array (e.g.,array: false → array: true) is not directly supported. Use the same 3-step migration strategy as field type changes.
Migration Script Examples
Populate Required Field
migrations/0003/migrate.ts
Populate Multiple Fields
migrations/0005/migrate.ts
Resolve Duplicate Values
migrations/0006/migrate.ts
Automatic Migration Execution
When runningtailor-sdk apply, pending migration scripts are automatically executed as part of the deployment process.
How It Works
- Pending Migration Detection: Identifies migration scripts that haven’t been executed yet
- Two-Stage Type Update: For breaking changes (required fields, type changes):
- Pre-Migration: New fields are added as
optionalfirst - Script Execution: Migration scripts run to populate data
- Post-Migration: Fields are changed to
required, deletions are applied
- Pre-Migration: New fields are added as
Execution Flow
Requirements for Automatic Migration
- Migrations configured:
migration.directoryset in db config - Auth configured: Auth service with machine users
- Kysely generator/plugin:
@tailor-platform/kysely-typeconfigured
Machine User Selection
When executing migration scripts, the system selects a machine user in the following priority:- Explicit configuration:
migration.machineUserin db config - Auto-selection: First machine user from
auth.machineUsers
Schema Verification
By default,tailor-sdk apply performs two schema verifications:
- Local schema check: Ensures local type definitions match the migration snapshot
- Remote schema check: Ensures remote schema matches the expected state based on migration history
Skipping Schema Check
To skip both local and remote schema verification (not recommended for production):Editor Integration
If theEDITOR environment variable is set, the generated script file will automatically open in your preferred editor:
Troubleshooting
Remote schema drift detected
Cause: The remote schema doesn’t match the expected state based on migration history. This can happen when:- Another developer applied different migrations
- Schema was changed manually outside of migrations
- Migration history is out of sync
- Check migration status: Run
tailor-sdk tailordb migration statusto see current state - Sync with team: Ensure all team members have the same migration files
- Reset if needed: Use
tailor-sdk tailordb migration set <number>to reset migration checkpoint - Force apply (use with caution): Use
--no-schema-checkto skip verification
”Machine user not found”
Cause: Specified machine user doesn’t exist in auth configuration. Solution:- Check available users: The error message lists available machine users
- Add machine user to your auth config:
tailor.config.ts
- Run
tailor-sdk applyto deploy the auth config - Retry migration
Migration script execution fails
Cause: Runtime error in your data migration logic. Solution:- Check the error message for the failing query/operation
- Test your script logic locally if possible
- Fix the script
- Apply again:
tailor-sdk apply
Best Practices
Always Review Generated Scripts
Don’t blindly apply migrations. Review and test migration scripts before deploying to production.
Use Transactions
All operations in migration scripts use the transaction object (
trx) to ensure atomicity.Test Migrations Locally
Test migration scripts in a development environment before applying to production.
Version Control
Commit migration files to version control and ensure team members are in sync.
Next Steps
- Learn about TailorDB for database schema definition
- Explore Generators for code generation from your schema
- Understand Testing strategies for your application