diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-11-20 12:48:55 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-11-20 12:48:55 -0500 |
commit | e6f0e9e6c0bdacf12296463c8da2af1dc00e6579 (patch) | |
tree | 02a1acd79ca923b914311462ce065d322f5d6345 | |
parent | 739f5347506947de2036fac55e5ac6e18726e2c7 (diff) | |
parent | 0b63884a2ce2b4b89f2ddf0db7fba47fce5540f0 (diff) | |
download | alembic-e6f0e9e6c0bdacf12296463c8da2af1dc00e6579.tar.gz |
Merge branch 'master' into multi_branch
Conflicts:
docs/build/changelog.rst
-rw-r--r-- | alembic/config.py | 17 | ||||
-rw-r--r-- | docs/build/changelog.rst | 9 | ||||
-rw-r--r-- | tests/test_config.py | 22 |
3 files changed, 45 insertions, 3 deletions
diff --git a/alembic/config.py b/alembic/config.py index a388a84..7d1beaf 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -51,10 +51,21 @@ class Config(object): ..versionadded:: 0.4 + :param 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 the :attr:`.Config.file_config` attribute is + first invoked, the replacement variable ``here`` will be added to this + dictionary before the dictionary is passed to ``SafeConfigParser()`` + to parse the .ini file. + + ..versionadded:: 0.7.0 + """ def __init__(self, file_=None, ini_section='alembic', output_buffer=None, - stdout=sys.stdout, cmd_opts=None): + stdout=sys.stdout, cmd_opts=None, + config_args=util.immutabledict()): """Construct a new :class:`.Config` """ @@ -63,6 +74,7 @@ class Config(object): self.output_buffer = output_buffer self.stdout = stdout self.cmd_opts = cmd_opts + self.config_args = dict(config_args) cmd_opts = None """The command-line options passed to the ``alembic`` script. @@ -113,7 +125,8 @@ class Config(object): here = os.path.abspath(os.path.dirname(self.config_file_name)) else: here = "" - file_config = SafeConfigParser({'here': here}) + self.config_args['here'] = here + file_config = SafeConfigParser(self.config_args) if self.config_file_name: file_config.read([self.config_file_name]) else: diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 085643f..f352d09 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -14,6 +14,15 @@ Changelog alone by default; use ``--verbose`` to get at additional output. .. change:: + :tags: feature, config + :pullreq: bitbucket:33 + + Added new argument :paramref:`.Config.config_args`, allows a dictionary + of replacement variables to be passed which will serve as substitution + values when an API-produced :class:`.Config` consumes the ``.ini`` + file. Pull request courtesy Noufal Ibrahim. + + .. change:: :tags: bug, oracle :tickets: 245 diff --git a/tests/test_config.py b/tests/test_config.py index 941504c..2d8f964 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,8 @@ #!coding: utf-8 +import os +import tempfile + from alembic import config, util, compat from alembic.migration import MigrationContext from alembic.operations import Operations @@ -10,7 +13,24 @@ from alembic.testing.mock import Mock, call from alembic.testing import eq_, assert_raises_message from alembic.testing.fixtures import capture_db from alembic.testing.env import _no_sql_testing_config, clear_staging_env,\ - staging_env + staging_env, _write_config_file + + +class FileConfigTest(TestBase): + def test_config_args(self): + cfg = _write_config_file(""" +[alembic] +migrations = %(base_path)s/db/migrations +""") + test_cfg = config.Config( + cfg.config_file_name, config_args=dict(base_path="/home/alembic") + ) + eq_( + test_cfg.get_section_option("alembic", "migrations"), + "/home/alembic/db/migrations") + + def tearDown(self): + clear_staging_env() class ConfigTest(TestBase): |