makemigrations Command

Generate database migration files using the Akron CLI for schema changes across SQLite, MySQL, PostgreSQL, and MongoDB.

Overview

The makemigrations command generates migration files that describe changes to your database schema. These files can be version controlled and applied across different environments to keep database schemas in sync.

Basic Syntax

akron makemigrations --db <database_url> [options]

Parameters

--db

Type: str (required)

Database connection URL to generate migrations for.

--name

Type: str (optional)

Custom name for the migration file. Auto-generated if not provided.

--output-dir

Type: str (optional)

Directory to save migration files. Default: ./migrations/

--dry-run

Type: flag (optional)

Show what migrations would be generated without creating files.

Examples

Generate Initial Migration

1# Create initial migration from current schema
2akron makemigrations --db "sqlite:///myapp.db" --name "initial_schema"
Expected Output
✓ Generated migration: migrations/001_initial_schema.py
✓ Migration includes 3 tables: users, posts, comments
✓ File saved to: ./migrations/001_initial_schema.py

Auto-Generated Migration

1# Let Akron auto-name the migration
2akron makemigrations --db "mysql://user:pass@host:3306/store"
Expected Output
✓ Detected schema changes since last migration
✓ Generated migration: migrations/002_add_products_table.py
✓ Changes: CREATE TABLE products, ADD COLUMN category_id to users

Custom Output Directory

1# Save migrations to custom directory
2akron makemigrations --db "postgres://user:pass@host:5432/app" --output-dir "./db/migrations"
Expected Output
✓ Created directory: ./db/migrations/
✓ Generated migration: db/migrations/003_update_user_schema.py
✓ Changes: ALTER TABLE users ADD COLUMN last_login

Dry Run Preview

1# Preview migration without creating files
2akron makemigrations --db "mongodb://localhost:27017/social" --dry-run
Expected Output
Migration Preview:
==================
File: migrations/004_add_user_preferences.py

Operations:
- ADD COLLECTION: user_preferences
- UPDATE SCHEMA: users (add field: preferences)
- CREATE INDEX: users.email (unique)

Run without --dry-run to generate files.

Migration File Structure

SQLite Migration Example

1# migrations/001_initial_schema.py
2"""
3Initial database schema
4Generated: 2024-01-15 10:30:00
5Database: SQLite
6"""
7
8def upgrade(db):
9 """Apply migration changes"""
10 db.execute("""
11 CREATE TABLE users (
12 id INTEGER PRIMARY KEY AUTOINCREMENT,
13 username TEXT NOT NULL UNIQUE,
14 email TEXT NOT NULL,
15 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
16 )
17 """)
18
19 db.execute("""
20 CREATE TABLE posts (
21 id INTEGER PRIMARY KEY AUTOINCREMENT,
22 user_id INTEGER NOT NULL,
23 title TEXT NOT NULL,
24 content TEXT,
25 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
26 FOREIGN KEY (user_id) REFERENCES users (id)
27 )
28 """)
29
30def downgrade(db):
31 """Rollback migration changes"""
32 db.execute("DROP TABLE IF EXISTS posts")
33 db.execute("DROP TABLE IF EXISTS users")

PostgreSQL Migration Example

1# migrations/002_add_user_profiles.py
2"""
3Add user profiles table
4Generated: 2024-01-16 14:20:00
5Database: PostgreSQL
6"""
7
8def upgrade(db):
9 """Apply migration changes"""
10 db.execute("""
11 CREATE TABLE user_profiles (
12 id SERIAL PRIMARY KEY,
13 user_id INTEGER NOT NULL REFERENCES users(id),
14 first_name VARCHAR(100),
15 last_name VARCHAR(100),
16 bio TEXT,
17 avatar_url VARCHAR(255),
18 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
19 )
20 """)
21
22 db.execute("""
23 CREATE INDEX idx_user_profiles_user_id ON user_profiles(user_id)
24 """)
25
26def downgrade(db):
27 """Rollback migration changes"""
28 db.execute("DROP INDEX IF EXISTS idx_user_profiles_user_id")
29 db.execute("DROP TABLE IF EXISTS user_profiles")

Migration Workflow

Development Workflow

1# 1. Make schema changes to your database
2akron create-table new_feature --db "sqlite:///dev.db" --schema '{"id": "int", "name": "str"}'
3
4# 2. Generate migration for the changes
5akron makemigrations --db "sqlite:///dev.db" --name "add_new_feature"
6
7# 3. Review the generated migration file
8cat migrations/003_add_new_feature.py
9
10# 4. Apply migration to other environments
11akron migrate --db "sqlite:///test.db"
12akron migrate --db "postgres://prod"

Team Collaboration

  • • Migration files are version controlled with your code
  • • Each developer can apply the same migrations
  • • Conflicts are resolved through file merging
  • • Production deployments include migration application

Advanced Features

Schema Comparison

Akron compares current database schema with the last migration to detect changes automatically.

Data Migrations

Include data transformation logic in migration files for complex schema changes.

Rollback Support

Every migration includes upgrade and downgrade functions for safe rollbacks.

Error Handling

Common Errors

  • No changes detected: Database schema matches last migration
  • Migration directory not found: Create migrations/ directory first
  • Invalid schema: Check database connection and permissions
  • Conflicting migrations: Resolve merge conflicts in migration files

Best Practices

  • • Review generated migrations before committing
  • • Use descriptive migration names
  • • Test migrations on development data first
  • • Keep migrations small and focused
  • • Never edit applied migrations

Related Commands