SQLAlchemy database migrations for Flask applications using Alembic.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Essential migration commands for creating, applying, and managing database schema changes. These functions form the core workflow of database migration management.
Create a new migration repository with the necessary directory structure and configuration files.
def init(directory=None, multidb=False, template=None, package=False):
"""
Creates a new migration repository.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- multidb: Support multiple databases (default: False)
- template: Repository template name (optional, defaults to 'flask' or 'flask-multidb')
- package: Write empty __init__.py files to environment and version locations (default: False)
Raises:
- CommandError: If repository initialization fails
- RuntimeError: If Flask application context is not available
"""Create a new revision file manually, optionally with auto-generated migration operations based on model changes.
def revision(directory=None, message=None, autogenerate=False, sql=False,
head='head', splice=False, branch_label=None, version_path=None,
rev_id=None):
"""
Create a new revision file.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- message: Revision message (optional)
- autogenerate: Populate revision with candidate migration operations (default: False)
- sql: Generate SQL output instead of executing (default: False)
- head: Head revision or <branchname>@head to base new revision on (default: 'head')
- splice: Allow non-head revision as the "head" to splice onto (default: False)
- branch_label: Branch label to apply to the new revision (optional)
- version_path: Specific path from config for version file (optional)
- rev_id: Hardcoded revision id instead of generating one (optional)
Raises:
- CommandError: If revision creation fails
- RuntimeError: If Flask application context is not available
"""Generate a migration automatically by comparing the current database state with the model definitions.
def migrate(directory=None, message=None, sql=False, head='head', splice=False,
branch_label=None, version_path=None, rev_id=None, x_arg=None):
"""
Autogenerate a new revision file (Alias for 'revision --autogenerate').
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- message: Revision message (optional)
- sql: Generate SQL output instead of executing (default: False)
- head: Head revision or <branchname>@head to base new revision on (default: 'head')
- splice: Allow non-head revision as the "head" to splice onto (default: False)
- branch_label: Branch label to apply to the new revision (optional)
- version_path: Specific path from config for version file (optional)
- rev_id: Hardcoded revision id instead of generating one (optional)
- x_arg: Additional arguments consumed by custom env.py scripts (list, optional)
Raises:
- CommandError: If migration generation fails
- RuntimeError: If Flask application context is not available
"""Apply migrations to upgrade the database to a later version.
def upgrade(directory=None, revision='head', sql=False, tag=None, x_arg=None):
"""
Upgrade to a later version.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- revision: Target revision (default: 'head')
- sql: Generate SQL output instead of executing (default: False)
- tag: Arbitrary tag name for custom env.py scripts (optional)
- x_arg: Additional arguments consumed by custom env.py scripts (list, optional)
Raises:
- CommandError: If upgrade fails
- RuntimeError: If Flask application context is not available
"""Revert the database to a previous version by applying downgrade operations.
def downgrade(directory=None, revision='-1', sql=False, tag=None, x_arg=None):
"""
Revert to a previous version.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- revision: Target revision (default: '-1' for one step back)
- sql: Generate SQL output instead of executing (default: False)
- tag: Arbitrary tag name for custom env.py scripts (optional)
- x_arg: Additional arguments consumed by custom env.py scripts (list, optional)
Raises:
- CommandError: If downgrade fails
- RuntimeError: If Flask application context is not available
"""from flask_migrate import init, migrate, upgrade
# Initialize migration repository
init()
# Generate migration after model changes
migrate(message="Add user table")
# Apply migration to database
upgrade()from flask_migrate import revision, upgrade, downgrade
# Create manual revision
revision(message="Custom migration", autogenerate=False)
# Upgrade to specific revision
upgrade(revision="abc123")
# Downgrade two steps
downgrade(revision="-2")
# Generate SQL without executing
upgrade(sql=True)from flask_migrate import init, migrate
# Initialize with multi-database support
init(multidb=True)
# Generate migration with custom env.py arguments
migrate(message="Multi-db changes", x_arg=['--database', 'users'])from flask_migrate import init
# Use custom template
init(template="custom-template")
# Use async Flask template
init(template="aioflask")Install with Tessl CLI
npx tessl i tessl/pypi-flask-migrate