Configuration¶
Note
this section discusses the internal API of Alembic as regards internal configuration constructs. This section is only useful for developers who wish to extend the capabilities of Alembic. For documentation on configuration of an Alembic environment, please see Tutorial.
The Config object represents the configuration
passed to the Alembic environment. From an API usage perspective,
it is needed for the following use cases:
- to create a
ScriptDirectory, which allows you to work with the actual script files in a migration environment - to create an
EnvironmentContext, which allows you to actually run theenv.pymodule within the migration environment - to programatically run any of the commands in the Commands module.
The Config is not needed for these cases:
- to instantiate a
MigrationContextdirectly - this object only needs a SQLAlchemy connection or dialect name. - to instantiate a
Operationsobject - this object only needs aMigrationContext.
-
class
alembic.config.Config(file_=None, ini_section='alembic', output_buffer=None, stdout=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, cmd_opts=None, config_args={}, attributes=None)¶ Represent an Alembic configuration.
Within an
env.pyscript, this is available via theEnvironmentContext.configattribute, which in turn is available atalembic.context:from alembic import context some_param = context.config.get_main_option("my option")
When invoking Alembic programatically, a new
Configcan be created by passing the name of an .ini file to the constructor:from alembic.config import Config alembic_cfg = Config("/path/to/yourapp/alembic.ini")
With a
Configobject, you can then run Alembic commands programmatically using the directives inalembic.command.The
Configobject can also be constructed without a filename. Values can be set programmatically, and new sections will be created as needed:from alembic.config import Config alembic_cfg = Config() alembic_cfg.set_main_option("script_location", "myapp:migrations") alembic_cfg.set_main_option("url", "postgresql://foo/bar") alembic_cfg.set_section_option("mysection", "foo", "bar")
Warning
When using programmatic configuration, make sure the
env.pyfile in use is compatible with the target configuration; including that the call to Pythonlogging.fileConfig()is omitted if the programmatic configuration doesn’t actually include logging directives.For passing non-string values to environments, such as connections and engines, use the
Config.attributesdictionary:with engine.begin() as connection: alembic_cfg.attributes['connection'] = connection command.upgrade(alembic_cfg, "head")
Parameters: - file_¶ – name of the .ini file to open.
- ini_section¶ – name of the main Alembic section within the .ini file
- output_buffer¶ – optional file-like input buffer which
will be passed to the
MigrationContext- used to redirect the output of “offline generation” when using Alembic programmatically. - stdout¶ –
buffer where the “print” output of commands will be sent. Defaults to
sys.stdout.New in version 0.4.
- config_args¶ –
A dictionary of keys and values that will be used for substitution in the alembic config file. The dictionary as given is copied to a new one, stored locally as the attribute
.config_args. When theConfig.file_configattribute is first invoked, the replacement variableherewill be added to this dictionary before the dictionary is passed toSafeConfigParser()to parse the .ini file.New in version 0.7.0.
- attributes¶ –
optional dictionary of arbitrary Python keys/values, which will be populated into the
Config.attributesdictionary.New in version 0.7.5.
Construct a new
Config-
attributes¶ A Python dictionary for storage of additional state.
This is a utility dictionary which can include not just strings but engines, connections, schema objects, or anything else. Use this to pass objects into an env.py script, such as passing a
sqlalchemy.engine.base.Connectionwhen calling commands fromalembic.commandprogrammatically.New in version 0.7.5.
-
cmd_opts= None¶ The command-line options passed to the
alembicscript.Within an
env.pyscript this can be accessed via theEnvironmentContext.configattribute.New in version 0.6.0.
See also
-
config_file_name= None¶ Filesystem path to the .ini file in use.
-
config_ini_section= None¶ Name of the config file section to read basic configuration from. Defaults to
alembic, that is the[alembic]section of the .ini file. This value is modified using the-n/--nameoption to the Alembic runnier.
-
file_config¶ Return the underlying
ConfigParserobject.Direct access to the .ini file is available here, though the
Config.get_section()andConfig.get_main_option()methods provide a possibly simpler interface.
-
get_main_option(name, default=None)¶ Return an option from the ‘main’ section of the .ini file.
This defaults to being a key from the
[alembic]section, unless the-n/--nameflag were used to indicate a different section.
-
get_section(name)¶ Return all the configuration options from a given .ini file section as a dictionary.
-
get_section_option(section, name, default=None)¶ Return an option from the given section of the .ini file.
-
get_template_directory()¶ Return the directory where Alembic setup templates are found.
This method is used by the alembic
initandlist_templatescommands.
-
print_stdout(text, *arg)¶ Render a message to standard out.
When
Config.print_stdout()is called with additional args those arguments will formatted against the provided text, otherwise we simply output the provided text verbatim.e.g.:
>>> config.print_stdout('Some text %s', 'arg') Some Text arg
-
set_main_option(name, value)¶ Set an option programmatically within the ‘main’ section.
This overrides whatever was in the .ini file.
Parameters: - name¶ – name of the value
- value¶ – the value. Note that this value is passed to
ConfigParser.set, which supports variable interpolation using pyformat (e.g.%(some_value)s). A raw percent sign not part of an interpolation symbol must therefore be escaped, e.g.%%. The given value may refer to another value already in the file using the interpolation format.
-
set_section_option(section, name, value)¶ Set an option programmatically within the given section.
The section is created if it doesn’t exist already. The value here will override whatever was in the .ini file.
Parameters: - section¶ – name of the section
- name¶ – name of the value
- value¶ – the value. Note that this value is passed to
ConfigParser.set, which supports variable interpolation using pyformat (e.g.%(some_value)s). A raw percent sign not part of an interpolation symbol must therefore be escaped, e.g.%%. The given value may refer to another value already in the file using the interpolation format.
-
alembic.config.main(argv=None, prog=None, **kwargs)¶ The console runner function for Alembic.