Ir para o conteúdo

Migrations

Database schema management.

Workflow

# 1. Edit models
# 2. Generate migration
stride makemigrations --name add_posts

# 3. Review migration file
cat migrations/0001_add_posts.py

# 4. Apply migration
stride migrate

Commands

# Create migration from model changes
stride makemigrations --name description

# Apply all pending migrations
stride migrate

# Show migration status
core showmigrations

# Rollback last migration
core rollback

# Rollback to specific migration
core rollback 0002

# Preview without applying
stride migrate --dry-run

Migration File

Generated in migrations/:

# migrations/0001_add_posts.py
from strider.migrations import Migration, CreateTable, ColumnDef

class Migration(Migration):
    dependencies = []
    
    operations = [
        CreateTable(
            table_name="posts",
            columns=[
                ColumnDef(name="id", type="INTEGER", primary_key=True),
                ColumnDef(name="title", type="VARCHAR(200)", nullable=False),
                ColumnDef(name="content", type="TEXT", nullable=False),
                ColumnDef(name="published", type="BOOLEAN", default=False),
            ],
        ),
    ]

Operations

Operation Description
CreateTable Create new table
DropTable Drop table
AddColumn Add column
DropColumn Remove column
AlterColumn Modify column
RenameColumn Rename column
CreateIndex Create index
DropIndex Drop index

Manual Migration

# migrations/0002_custom.py
from strider.migrations import Migration, AddColumn, ColumnDef

class Migration(Migration):
    dependencies = ["0001_add_posts"]
    
    operations = [
        AddColumn(
            table_name="posts",
            column=ColumnDef(
                name="views",
                type="INTEGER",
                default=0,
            ),
        ),
    ]

Empty Migration

For custom SQL:

stride makemigrations --name custom_sql --empty
# migrations/0003_custom_sql.py
from strider.migrations import Migration, RunSQL

class Migration(Migration):
    dependencies = ["0002_custom"]
    
    operations = [
        RunSQL(
            forward="CREATE INDEX idx_posts_title ON posts(title)",
            backward="DROP INDEX idx_posts_title",
        ),
    ]

Best Practices

  1. One change per migration — easier to rollback
  2. Descriptive namesadd_posts, add_user_avatar, remove_legacy_field
  3. Review before applying — check generated SQL
  4. Test rollback — ensure backward migration works
  5. Don't edit applied migrations — create new ones instead

Troubleshooting

Migration not detecting changes

# Ensure models are imported in barrel file
# src/apps/models.py
from src.apps.posts.models import Post  # noqa

Column already exists

# Skip problematic migration
stride migrate --fake 0001

Reset migrations (dev only)

# Delete migration files
rm -rf migrations/*.py

# Reset database
core reset_db --yes

# Start fresh
stride makemigrations --name initial
stride migrate

Next

  • Models — Model definitions
  • CLI — All commands