Commands¶
Note
this section discusses the internal API of Alembic as regards its command invocation system. This section is only useful for developers who wish to extend the capabilities of Alembic. For documentation on using Alembic commands, please see Tutorial.
Alembic commands are all represented by functions in the Commands
package. They all accept the same style of usage, being sent
the Config
object as the first argument.
Commands can be run programmatically, by first constructing a Config
object, as in:
from alembic.config import Config
from alembic import command
alembic_cfg = Config("/path/to/yourapp/alembic.ini")
command.upgrade(alembic_cfg, "head")
In many cases, and perhaps more often than not, an application will wish
to call upon a series of Alembic commands and/or other features. It is
usually a good idea to link multiple commands along a single connection
and transaction, if feasible. This can be achieved using the
Config.attributes
dictionary in order to share a connection:
with engine.begin() as connection:
alembic_cfg.attributes['connection'] = connection
command.upgrade(alembic_cfg, "head")
This recipe requires that env.py
consumes this connection argument;
see the example in Sharing a Connection with a Series of Migration Commands and Environments for details.
To write small API functions that make direct use of database and script directory
information, rather than just running one of the built-in commands,
use the ScriptDirectory
and MigrationContext
classes directly.
-
alembic.command.
branches
(config, verbose=False)¶ Show current branch points.
Parameters:
-
alembic.command.
current
(config, verbose=False, head_only=False)¶ Display the current revision for a database.
Parameters:
-
alembic.command.
downgrade
(config, revision, sql=False, tag=None)¶ Revert to a previous version.
Parameters:
-
alembic.command.
edit
(config, rev)¶ Edit revision script(s) using $EDITOR.
Parameters:
-
alembic.command.
heads
(config, verbose=False, resolve_dependencies=False)¶ Show current available heads in the script directory
Parameters:
-
alembic.command.
history
(config, rev_range=None, verbose=False, indicate_current=False)¶ List changeset scripts in chronological order.
Parameters:
-
alembic.command.
init
(config, directory, template='generic')¶ Initialize a new scripts directory.
Parameters:
-
alembic.command.
list_templates
(config)¶ List available templates
Parameters: config¶ – a Config
object.
-
alembic.command.
merge
(config, revisions, message=None, branch_label=None, rev_id=None)¶ Merge two revisions together. Creates a new migration file.
New in version 0.7.0.
Parameters: See also
-
alembic.command.
revision
(config, message=None, autogenerate=False, sql=False, head='head', splice=False, branch_label=None, version_path=None, rev_id=None, depends_on=None, process_revision_directives=None)¶ Create a new revision file.
Parameters: - config¶ – a
Config
object. - message¶ – string message to apply to the revision; this is the
-m
option toalembic revision
. - autogenerate¶ – whether or not to autogenerate the script from
the database; this is the
--autogenerate
option toalembic revision
. - sql¶ – whether to dump the script out as a SQL string; when specified,
the script is dumped to stdout. This is the
--sql
option toalembic revision
. - head¶ – head revision to build the new revision upon as a parent;
this is the
--head
option toalembic revision
. - splice¶ – whether or not the new revision should be made into a
new head of its own; is required when the given
head
is not itself a head. This is the--splice
option toalembic revision
. - branch_label¶ – string label to apply to the branch; this is the
--branch-label
option toalembic revision
. - version_path¶ – string symbol identifying a specific version path
from the configuration; this is the
--version-path
option toalembic revision
. - rev_id¶ – optional revision identifier to use instead of having
one generated; this is the
--rev-id
option toalembic revision
. - depends_on¶ – optional list of “depends on” identifiers; this is the
--depends-on
option toalembic revision
. - process_revision_directives¶ –
this is a callable that takes the same form as the callable described at
EnvironmentContext.configure.process_revision_directives
; will be applied to the structure generated by the revision process where it can be altered programmatically. Note that unlike all the other parameters, this option is only available via programmatic use ofcommand.revision()
New in version 0.9.0.
- config¶ – a
-
alembic.command.
show
(config, rev)¶ Show the revision(s) denoted by the given symbol.
Parameters:
-
alembic.command.
stamp
(config, revision, sql=False, tag=None)¶ ‘stamp’ the revision table with the given revision; don’t run any migrations.
Parameters:
-
alembic.command.
upgrade
(config, revision, sql=False, tag=None)¶ Upgrade to a later version.
Parameters: