Configuration¶
Configuration for Alembic and its migrations uses the following Flask config keys.
- flask_alembic.ALEMBIC¶
A dictionary containing general configuration, mostly used by
ConfigandScriptDirectory. See Alembic’s docs on config.If a value is a dict, the key will be equivalent to a section name in
alembic.ini, and the value will be the items in the section. This allows using Alembic’s post_write_hooks, for example.Changelog
Changed in version 3.1: Treat dict values as sections.
- flask_alembic.ALEMBIC_CONTEXT¶
A dictionary containing options passed to
MigrationContextbyEnvironmentContext.configure. See Alembic’s docs on runtime.
ALEMBIC["script_location"] is the location of the migrations directory. If
it is not an absolute path, it will be relative to the application root. It
defaults to migrations relative to the application root.
ALEMBIC_CONTEXT["compare_type"] defaults to True in Alembic.
ALEMBIC_CONTEXT["compare_server_default"] defaults to True, it would
otherwise default to False in Alembic.
Post Processing Revisions¶
It’s possible to tell Alembic to run one or more commands on a generated migration file. See Alembic’s docs for full details. Here are some useful examples.
black¶
Run the black formatter.
ALEMBIC = {
"post_write_hooks": {
"hooks": "black",
"black.type": "console_scripts",
"black.entrypoint": "black",
}
}
ruff¶
Run the ruff formatter.
ALEMBIC = {
"post_write_hooks": {
"hooks": "ruff",
"ruff.type": "console_scripts",
"ruff.entrypoint": "ruff",
"ruff.options": "format REVISION_SCRIPT_FILENAME",
}
}
pre-commit¶
Run all configured pre-commit hooks.
ALEMBIC = {
"post_write_hooks": {
"hooks": "pre-commit",
"pre-commit.type": "console_scripts",
"pre-commit.entrypoint": "pre-commit",
"pre-commit.options": "run --files REVISION_SCRIPT_FILENAME",
}
}
git add¶
Add the file to git so that you don’t forget when committing. This can be run after other hooks so that all the changes are staged.
ALEMBIC = {
"post_write_hooks": {
"hooks": "pre-commit, git",
"pre-commit.type": "console_scripts",
"pre-commit.entrypoint": "pre-commit",
"pre-commit.options": "run --files REVISION_SCRIPT_FILENAME",
"git.type": "exec",
"git.executable": "git",
"git.options": "add REVISION_SCRIPT_FILENAME",
}
}