Skip to main content
Kit provides a complete set of commands for managing database migrations, inspired by Laravel’s migration system.

make:migration

Generate a new database migration file.
kit make:migration <name>

Examples

kit make:migration create_users_table
kit make:migration add_email_to_users
kit make:migration create_posts_table

Generated File

// migrations/YYYYMMDDHHMMSS_create_users_table.rs
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Users::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Users::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Users::CreatedAt).timestamp().not_null())
                    .col(ColumnDef::new(Users::UpdatedAt).timestamp().not_null())
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Users::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Users {
    Table,
    Id,
    CreatedAt,
    UpdatedAt,
}

migrate

Run all pending migrations.
kit migrate

Output

Running migrations...
Applying migration: 20240101120000_create_users_table
Applying migration: 20240101120001_create_posts_table
All migrations completed successfully!

migrate:status

Show the status of all migrations.
kit migrate:status

Output

Migration Status
----------------
[✓] 20240101120000_create_users_table (applied)
[✓] 20240101120001_create_posts_table (applied)
[ ] 20240101120002_add_email_to_users (pending)

migrate:rollback

Rollback the last migration(s).
kit migrate:rollback [options]

Options

OptionDefaultDescription
--step <N>1Number of migrations to rollback

Examples

# Rollback the last migration
kit migrate:rollback

# Rollback the last 3 migrations
kit migrate:rollback --step 3

Output

Rolling back migrations...
Rolling back: 20240101120002_add_email_to_users
Rollback completed successfully!

migrate:fresh

Drop all tables and re-run all migrations from scratch.
kit migrate:fresh
This command will delete all data in your database. Use with caution, especially in production!

Output

Dropping all tables...
Tables dropped.
Running migrations...
Applying migration: 20240101120000_create_users_table
Applying migration: 20240101120001_create_posts_table
Applying migration: 20240101120002_add_email_to_users
Fresh migration completed successfully!

Use Cases

  • Resetting development database
  • Testing migrations from scratch
  • Clearing test data

db:sync

Sync database schema to entity files. This runs migrations and regenerates SeaORM entity files.
kit db:sync [options]

Options

OptionDescription
--skip-migrationsSkip running migrations before sync

Examples

# Run migrations and sync entities
kit db:sync

# Only sync entities (skip migrations)
kit db:sync --skip-migrations

What It Does

  1. Runs all pending migrations (unless --skip-migrations)
  2. Introspects the database schema
  3. Generates/updates SeaORM entity files in src/models/

Migration Workflow

A typical migration workflow:
# 1. Create a new migration
kit make:migration create_users_table

# 2. Edit the migration file to define your schema
# migrations/YYYYMMDDHHMMSS_create_users_table.rs

# 3. Run the migration
kit migrate

# 4. Sync entity files
kit db:sync

# 5. Use the generated model in your code
# src/models/users.rs is now available

Writing Migrations

Creating Tables

async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
    manager
        .create_table(
            Table::create()
                .table(Posts::Table)
                .if_not_exists()
                .col(
                    ColumnDef::new(Posts::Id)
                        .integer()
                        .not_null()
                        .auto_increment()
                        .primary_key(),
                )
                .col(ColumnDef::new(Posts::Title).string().not_null())
                .col(ColumnDef::new(Posts::Content).text().not_null())
                .col(ColumnDef::new(Posts::UserId).integer().not_null())
                .foreign_key(
                    ForeignKey::create()
                        .from(Posts::Table, Posts::UserId)
                        .to(Users::Table, Users::Id)
                        .on_delete(ForeignKeyAction::Cascade),
                )
                .col(ColumnDef::new(Posts::CreatedAt).timestamp().not_null())
                .col(ColumnDef::new(Posts::UpdatedAt).timestamp().not_null())
                .to_owned(),
        )
        .await
}

Adding Columns

async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
    manager
        .alter_table(
            Table::alter()
                .table(Users::Table)
                .add_column(ColumnDef::new(Users::Email).string().not_null())
                .to_owned(),
        )
        .await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
    manager
        .alter_table(
            Table::alter()
                .table(Users::Table)
                .drop_column(Users::Email)
                .to_owned(),
        )
        .await
}

Creating Indexes

async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
    manager
        .create_index(
            Index::create()
                .name("idx_users_email")
                .table(Users::Table)
                .col(Users::Email)
                .unique()
                .to_owned(),
        )
        .await
}

Summary

CommandDescription
make:migration <name>Create a new migration file
migrateRun all pending migrations
migrate:statusShow migration status
migrate:rollbackRollback last migration(s)
migrate:freshDrop all tables and re-run migrations
db:syncSync schema to entity files