SQLAlchemy database migrations for Flask applications using Alembic.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for inspecting migration history, current state, and repository status. These commands provide visibility into the migration repository and database state without making changes.
Show detailed information about a specific revision.
def show(directory=None, revision='head'):
"""
Show the revision denoted by the given symbol.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- revision: Revision to show (default: 'head')
Raises:
- CommandError: If revision cannot be found or displayed
- RuntimeError: If Flask application context is not available
"""List all changeset scripts in chronological order, providing an overview of the migration timeline.
def history(directory=None, rev_range=None, verbose=False,
indicate_current=False):
"""
List changeset scripts in chronological order.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- rev_range: Revision range filter in format [start]:[end] (optional)
- verbose: Use more verbose output (default: False)
- indicate_current: Indicate current version - requires Alembic 0.9.9+ (default: False)
Raises:
- CommandError: If history cannot be retrieved
- RuntimeError: If Flask application context is not available
"""Show current available heads in the script directory, useful for identifying branch points.
def heads(directory=None, verbose=False, resolve_dependencies=False):
"""
Show current available heads in the script directory.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- verbose: Use more verbose output (default: False)
- resolve_dependencies: Treat dependency versions as down revisions (default: False)
Raises:
- CommandError: If heads cannot be retrieved
- RuntimeError: If Flask application context is not available
"""Show current branch points in the migration history.
def branches(directory=None, verbose=False):
"""
Show current branch points.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- verbose: Use more verbose output (default: False)
Raises:
- CommandError: If branches cannot be retrieved
- RuntimeError: If Flask application context is not available
"""Display the current revision for each database, showing the actual state of the database.
def current(directory=None, verbose=False):
"""
Display the current revision for each database.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
- verbose: Use more verbose output (default: False)
Raises:
- CommandError: If current revision cannot be determined
- RuntimeError: If Flask application context is not available
"""Check if there are any new operations to migrate by comparing the current database state with model definitions.
def check(directory=None):
"""
Check if there are any new operations to migrate.
Parameters:
- directory: Migration directory path (optional, uses app extension config if None)
Raises:
- CommandError: If check fails or if there are pending migrations
- RuntimeError: If Flask application context is not available
"""List all available migration templates that can be used for repository initialization.
def list_templates():
"""
List available templates.
Raises:
- CommandError: If templates cannot be listed
"""from flask_migrate import show, current, history
# Show information about the head revision
show()
# Show specific revision
show(revision="abc123def456")
# Display current database revision
current()
# Show migration history
history()from flask_migrate import history, heads, branches
# Show verbose history
history(verbose=True)
# Show history for specific range
history(rev_range="abc123:def456")
# Show history with current revision indicated (Alembic 0.9.9+)
history(indicate_current=True)
# Show all head revisions
heads(verbose=True)
# Show branch points
branches(verbose=True)from flask_migrate import check, current, heads
# Check if migrations are needed
try:
check()
print("No pending migrations")
except CommandError:
print("Migrations needed")
# Get comprehensive status
current()
heads()from flask_migrate import list_templates
# List all available templates
list_templates()from flask_migrate import history, heads, branches, current
def migration_status_report():
"""Generate comprehensive migration status report."""
print("=== Current Database State ===")
current(verbose=True)
print("\n=== Available Heads ===")
heads(verbose=True, resolve_dependencies=True)
print("\n=== Branch Points ===")
branches(verbose=True)
print("\n=== Recent History ===")
history(verbose=True, rev_range="-5:")
# Run status report
migration_status_report()Install with Tessl CLI
npx tessl i tessl/pypi-flask-migrate