From b12f31b40a0fad39857fad4d432c8e471c22581a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 11 Oct 2019 11:04:24 -0400 Subject: Allow ALEMBIC_CONFIG for config file location The ALEMBIC_CONFIG environment variable is now used as the default alembic config file name for the -c option of the command line runner, if present. Bump revision to 1.3, as Alembic releases are infrequent, start using a more semver-like approach to feature adds. Change-Id: I2fdbd6a1ec2d173037e2e371f8851b0258e3efd5 Fixes: #608 --- alembic/__init__.py | 2 +- alembic/config.py | 6 ++++-- docs/build/unreleased/608.rst | 8 ++++++++ tests/test_command.py | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 docs/build/unreleased/608.rst diff --git a/alembic/__init__.py b/alembic/__init__.py index 907d838..767b592 100644 --- a/alembic/__init__.py +++ b/alembic/__init__.py @@ -6,7 +6,7 @@ from . import op # noqa from .runtime import environment from .runtime import migration -__version__ = '1.2.2' +__version__ = "1.3.0" package_dir = path.abspath(path.dirname(__file__)) diff --git a/alembic/config.py b/alembic/config.py index b0700d5..48c41cb 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -468,12 +468,14 @@ class CommandLine(object): subparser.add_argument(arg, help=positional_help.get(arg)) parser = ArgumentParser(prog=prog) + parser.add_argument( "-c", "--config", type=str, - default="alembic.ini", - help="Alternate config file", + default=os.environ.get("ALEMBIC_CONFIG", "alembic.ini"), + help="Alternate config file; defaults to value of " + 'ALEMBIC_CONFIG environment variable, or "alembic.ini"', ) parser.add_argument( "-n", diff --git a/docs/build/unreleased/608.rst b/docs/build/unreleased/608.rst new file mode 100644 index 0000000..ad49451 --- /dev/null +++ b/docs/build/unreleased/608.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: feature, command + :tickets: 608 + + Added support for ALEMBIC_CONFIG environment variable, + refers to the location of the alembic configuration script + in lieu of using the -c command line option. + diff --git a/tests/test_command.py b/tests/test_command.py index 2aeb28c..af8272d 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -946,6 +946,9 @@ class CommandLineTest(TestBase): cls.cfg = _sqlite_testing_config() cls.a, cls.b, cls.c = three_rev_fixture(cls.cfg) + def teardown(self): + os.environ.pop("ALEMBIC_CONFIG", None) + @classmethod def teardown_class(cls): clear_staging_env() @@ -1038,6 +1041,40 @@ class CommandLineTest(TestBase): directory=directory, ) + def test_config_file_default(self): + cl = config.CommandLine() + with mock.patch.object(cl, "run_cmd") as run_cmd: + cl.main(argv=["list_templates"]) + + cfg = run_cmd.mock_calls[0][1][0] + eq_(cfg.config_file_name, "alembic.ini") + + def test_config_file_c_override(self): + cl = config.CommandLine() + with mock.patch.object(cl, "run_cmd") as run_cmd: + cl.main(argv=["-c", "myconf.ini", "list_templates"]) + + cfg = run_cmd.mock_calls[0][1][0] + eq_(cfg.config_file_name, "myconf.ini") + + def test_config_file_env_variable(self): + os.environ["ALEMBIC_CONFIG"] = "/foo/bar/bat.conf" + cl = config.CommandLine() + with mock.patch.object(cl, "run_cmd") as run_cmd: + cl.main(argv=["list_templates"]) + + cfg = run_cmd.mock_calls[0][1][0] + eq_(cfg.config_file_name, "/foo/bar/bat.conf") + + def test_config_file_env_variable_c_override(self): + os.environ["ALEMBIC_CONFIG"] = "/foo/bar/bat.conf" + cl = config.CommandLine() + with mock.patch.object(cl, "run_cmd") as run_cmd: + cl.main(argv=["-c", "myconf.conf", "list_templates"]) + + cfg = run_cmd.mock_calls[0][1][0] + eq_(cfg.config_file_name, "myconf.conf") + def test_init_file_exists_and_is_empty(self): def access_(path, mode): if "generic" in path or path == "foobar": -- cgit v1.2.1