API¶
Anything documented here is part of the public API that Flask-Alembic provides, unless otherwise indicated. Anything not documented here is considered internal or private and may change at any time.
- class flask_alembic.Alembic(app=None, *, run_mkdir=True, command_name='db', metadatas=None, engines=None)¶
Provide an Alembic environment and migration API.
If instantiated without an app instance,
init_app()is used to register an app at a later time.Configures basic logging to
stderrfor thesqlalchemyandalembicloggers if they do not already have handlers.- Parameters:
app (Flask | None) – Call
init_appon this app.command_name (str) – Register a Click command with this name during
init_app, unless it is the empty string.metadatas (sa.MetaData | list[sa.MetaData] | dict[str, sa.MetaData | list[sa.MetaData]] | None) – One or more
MetaDatato inspect when generating migrations. A single metadata or list will be assigned to the"default"key. Or a map can be given that specifies one or more metadata for each key. When using Flask-SQLAlchemy,db.metadatais used if this is not given.engines (sa.Engine | dict[str, sa.Engine] | None) – One or more engines to perform migrations on. Must match the keys in
metadatas. A single engine will be assigned to the"default"key. Or a map can be given that specifies the engine for each key. When using Flask-SQLAlchemy(-Lite),db.enginesis used if this is not given.
Changelog
Changed in version 3.1: Added the
metadatasandenginessarguments. Support Flask-SQLAlchemy-Lite and plain SQLAlchemy in addition to Flask-SQLAlchemy. Support multiple databases and multiple metadata per database.Changed in version 3.1:
run_mkdirandcommand_nameare keyword-only arguments.- init_app(app)¶
Register this extension on an app.
- Parameters:
app (Flask) – App to register.
- Return type:
None
Changed in version 3.2:
run_mkdirandcommand_nameargs are removed.
- rev_id()¶
Generate a unique id for a revision.
By default, this uses the current UTC timestamp. Override this method, or assign a static method, to change this.
Changelog
Changed in version 3.0: Uses the current UTC timestamp instead of a UUID.
- Return type:
- property script_directory: ScriptDirectory¶
Get the Alembic
ScriptDirectoryfor the current app.
- property environment_context: EnvironmentContext¶
Get the Alembic
EnvironmentContextfor the current app.
- property migration_contexts: dict[str, MigrationContext]¶
Get the map of Alembic
MigrationContextfor each configured database key for the current app. Each context’s connection will be closed when the Flask application context ends.Changelog
Added in version 3.1.
- property migration_context: MigrationContext¶
Get the Alembic
MigrationContextfor the current app. If multiple databases are configured, this is the context for the"default"key. The context’s connection will be closed when the Flask application context ends.
- property ops: dict[str, Operations]¶
Get the Alembic
Operationscontext for each configured database key for the current app.Changelog
Added in version 3.1.
- property op: Operations¶
Get the Alembic
Operationscontext for the current app. If multiple databases are configured, this is the context for the"default"key.
- run_migrations(fn, **kwargs)¶
Configure an Alembic
MigrationContextto run migrations for the given function.This takes the place of Alembic’s
env.pyfile, specifically therun_migrations_onlinefunction.- Parameters:
- Return type:
None
Changelog
Changed in version 3.1: Support multiple databases.
- mkdir()¶
Create the script directory and template.
Alembic’s
generictemplate is used if a single database is configured. Themultidbtemplate if multiple databases are configured.Changelog
Changed in version 3.1: Support multiple databases.
- Return type:
None
- needs_upgrade()¶
Check whether the current revisions are the head revisions.
- Returns:
Trueif the database is not at all head revisions,Falseotherwise.- Return type:
- heads(resolve_dependencies=False)¶
Get the list of revisions that have no child revisions.
- branches()¶
Get the list of revisions that have more than one next revision.
- log(start='base', end='heads')¶
Get the list of revisions in the order they will run.
- stamp(target='heads')¶
Set the current database revision without running migrations.
- upgrade(target='heads')¶
Run migrations to upgrade database.
- downgrade(target=-1)¶
Run migrations to downgrade database.
- revision(message, empty=False, branch='default', parent='head', splice=False, depend=None, label=None, path=None)¶
Create a new revision. By default, auto-generate operations by comparing models and database.
- Parameters:
message (str) – Description of revision.
empty (bool) – Don’t auto-generate operations.
branch (str) – Use this independent branch name.
parent (str | Script | list[str] | list[Script] | tuple[str, ...] | tuple[Script, ...]) – Parent revision(s) of this revision.
splice (bool) – Allow non-head parent revision.
depend (str | Script | list[str] | list[Script] | tuple[str, ...] | tuple[Script, ...] | None) – Revision(s) this revision depends on.
label (str | list[str] | None) – Label(s) to apply to this revision.
path (str | None) – Where to store this revision.
- Returns:
List of new revisions.
- Return type:
- merge(revisions='heads', message=None, label=None)¶
Create a merge revision.
- Parameters:
- Returns:
A new revision object.
- Return type:
Script | None
- produce_migrations()¶
Generate the
MigrationScriptobject that would generate a new revision.- Return type:
- needs_revision()¶
Check if any changes between the database and models are detected.
- Returns:
True if changes are detected.
- Return type:
Added in version 3.2.
- compare_metadata()¶
Describe the operations that would be present in a new revision.
This only supports a single database. For multiple databases, use the following instead:
for ops in alembic.produce_migrations().upgrade_ops_list: name = ops.upgrade_token.removesuffix("_upgrades") diff = ops.as_diffs()