API Reference¶
- class flask_alembic.Alembic(app=None, run_mkdir=True, command_name='db')¶
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
stderr
for thesqlalchemy
andalembic
loggers if they do not already have handlers.- Parameters:
app – Call
init_app
on this app.run_mkdir – Run
mkdir()
duringinit_app
.command_name – Register a Click command with this name during
init_app
, or skip ifFalse
.
- init_app(app, run_mkdir=None, command_name=None)¶
Register this extension on an app. Will automatically set up migration directory by default.
Keyword arguments on this method override those set during
__init__()
if notNone
.- Parameters:
app – App to register.
run_mkdir – Run
mkdir()
automatically.command_name – Register a Click command with this name, or skip if
False
.
- rev_id()¶
Generate a unique id for a revision.
By default this uses
alembic.util.rev_id()
. Override this method, or assign a static method, to change this.For example, to use the current timestamp:
alembic = Alembic(app) alembic.rev_id = lambda: str(datetime.utcnow().timestamp())
- property script_directory¶
Get the Alembic
ScriptDirectory
for the current app.
- property environment_context¶
Get the Alembic
EnvironmentContext
for the current app.
- property migration_context¶
Get the Alembic
MigrationContext
for the current app.Accessing this property opens a database connection but can’t close it automatically. Make sure to call
migration_context.connection.close()
when you’re done.
- property op¶
Get the Alembic
Operations
context for the current app.Accessing this property opens a database connection but can’t close it automatically. Make sure to call
migration_context.connection.close()
when you’re done.
- run_migrations(fn, **kwargs)¶
Configure an Alembic
MigrationContext
to run migrations for the given function.This takes the place of Alembic’s
env.py
file, specifically therun_migrations_online
function.- Parameters:
fn – Use this function to control what migrations are run.
kwargs – Extra arguments passed to
upgrade
ordowngrade
in each revision.
- mkdir()¶
Create the script directory and template.
- current()¶
Get the list of current revisions.
- heads(resolve_dependencies=False)¶
Get the list of revisions that have no child revisions.
- Parameters:
resolve_dependencies – Treat dependencies as down 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.
- Parameters:
start – Only get entries since this revision.
end – Only get entries until this revision.
- stamp(target='heads')¶
Set the current database revision without running migrations.
- Parameters:
target – Revision to set to.
- upgrade(target='heads')¶
Run migrations to upgrade database.
- Parameters:
target – Revision to go up to.
- downgrade(target=-1)¶
Run migrations to downgrade database.
- Parameters:
target – Revision to go down to.
- 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 – Description of revision.
empty – Don’t auto-generate operations.
branch – Use this independent branch name.
parent – Parent revision(s) of this revision.
splice – Allow non-head parent revision.
depend – Revision(s) this revision depends on.
label – Label(s) to apply to this revision.
path – Where to store this revision.
- Returns:
List of new revisions.
- merge(revisions='heads', message=None, label=None)¶
Create a merge revision.
- Parameters:
revisions – Revisions to merge.
message – Description of merge, will default to
revisions
param.label – Label(s) to apply to this revision.
- Returns:
A new revision object.
- produce_migrations()¶
Generate the
MigrationScript
object that would generate a new revision.
- compare_metadata()¶
Generate a list of operations that would be present in a new revision.