]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Allow ALEMBIC_CONFIG for config file location
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 11 Oct 2019 15:04:24 +0000 (11:04 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 11 Oct 2019 15:05:28 +0000 (11:05 -0400)
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
alembic/config.py
docs/build/unreleased/608.rst [new file with mode: 0644]
tests/test_command.py

index 907d8380d6b0be2d370ec6d7aa1c0c6c0f89322b..767b59234aad0cefd9d103393df0ab07dc31c002 100644 (file)
@@ -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__))
 
index b0700d56914e324b2e1182f134a51a8d2154a839..48c41cb861c0ffbd27973020f0aaae56323984eb 100644 (file)
@@ -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 (file)
index 0000000..ad49451
--- /dev/null
@@ -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.
+
index 2aeb28c8debbdd588e87068e9f095ac0bc33d614..af8272d3fc852956f27379662add57fd3b66269e 100644 (file)
@@ -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":