]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Add --package flag to write __init__.py files
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Sep 2019 20:59:27 +0000 (16:59 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Sep 2019 00:48:35 +0000 (20:48 -0400)
Added new flag ``--package`` to ``alembic init``.  For environments where
the Alembic migration files and such are within the package tree and
importable as modules, this flag can be specified which will add the
additional ``__init__.py`` files in the version location and the
environment location.

Change-Id: I4993d17936f2b8a9833a28f0788389b19ff4333e
Fixes: #463
alembic/command.py
alembic/config.py
docs/build/unreleased/463.rst [new file with mode: 0644]
tests/test_command.py

index ad8c6c3945f7bbc96280369e0435de1989b697ec..d4acfdbf12a43b69acf0d99dfd72489f92bc3fb4 100644 (file)
@@ -25,7 +25,7 @@ def list_templates(config):
     config.print_stdout("\n  alembic init --template generic ./scripts")
 
 
-def init(config, directory, template="generic"):
+def init(config, directory, template="generic", package=False):
     """Initialize a new scripts directory.
 
     :param config: a :class:`.Config` object.
@@ -35,6 +35,12 @@ def init(config, directory, template="generic"):
     :param template: string name of the migration environment template to
      use.
 
+    :param package: when True, write ``__init__.py`` files into the
+     environment location as well as the versions/ location.
+
+     .. versionadded:: 1.2
+
+
     """
 
     if os.access(directory, os.F_OK) and os.listdir(directory):
@@ -76,6 +82,14 @@ def init(config, directory, template="generic"):
             output_file = os.path.join(directory, file_)
             script._copy_file(file_path, output_file)
 
+    if package:
+        for path in [
+            os.path.join(os.path.abspath(directory), "__init__.py"),
+            os.path.join(os.path.abspath(versions), "__init__.py"),
+        ]:
+            file_ = util.status("Adding %s" % path, open, path, "w")
+            file_.close()
+
     util.msg(
         "Please edit configuration/connection/logging "
         "settings in %r before proceeding." % config_file
index 307a2614d6ba1f1e513f713c0dfa0f1c01667f54..2232096149ca45f879e8f56edaed990c647e2b14 100644 (file)
@@ -433,6 +433,14 @@ class CommandLine(object):
                         "before stamping",
                     ),
                 ),
+                "package": (
+                    "--package",
+                    dict(
+                        action="store_true",
+                        help="Write empty __init__.py files to the "
+                        "environment and version locations",
+                    ),
+                ),
             }
             positional_help = {
                 "directory": "location of scripts directory",
diff --git a/docs/build/unreleased/463.rst b/docs/build/unreleased/463.rst
new file mode 100644 (file)
index 0000000..a428bfc
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: feature, environment
+    :tickets: 463
+
+    Added new flag ``--package`` to ``alembic init``.  For environments where
+    the Alembic migration files and such are within the package tree and
+    importable as modules, this flag can be specified which will add the
+    additional ``__init__.py`` files in the version location and the
+    environment location.
index e1be67875d3165b6b12a1c428c0806c602fdc915..e09347925b71c6b4e3002ef663c0172ef4773c02 100644 (file)
@@ -2,6 +2,7 @@ from contextlib import contextmanager
 import inspect
 from io import BytesIO
 from io import TextIOWrapper
+import os
 import re
 
 from sqlalchemy import exc as sqla_exc
@@ -15,6 +16,7 @@ from alembic.testing import assert_raises
 from alembic.testing import assert_raises_message
 from alembic.testing import eq_
 from alembic.testing import mock
+from alembic.testing.env import _get_staging_directory
 from alembic.testing.env import _no_sql_testing_config
 from alembic.testing.env import _sqlite_file_db
 from alembic.testing.env import _sqlite_testing_config
@@ -909,6 +911,10 @@ class CommandLineTest(TestBase):
         cls.cfg = _sqlite_testing_config()
         cls.a, cls.b, cls.c = three_rev_fixture(cls.cfg)
 
+    @classmethod
+    def teardown_class(cls):
+        clear_staging_env()
+
     def test_run_cmd_args_missing(self):
         canary = mock.Mock()
 
@@ -1037,3 +1043,26 @@ class CommandLineTest(TestBase):
                 makedirs.mock_calls,
                 [mock.call("foobar"), mock.call("foobar/versions")],
             )
+
+    def test_init_w_package(self):
+
+        path = os.path.join(_get_staging_directory(), "foobar")
+
+        with mock.patch("alembic.command.open") as open_:
+            command.init(self.cfg, directory=path, package=True)
+            eq_(
+                open_.mock_calls,
+                [
+                    mock.call(
+                        os.path.abspath(os.path.join(path, "__init__.py")), "w"
+                    ),
+                    mock.call().close(),
+                    mock.call(
+                        os.path.abspath(
+                            os.path.join(path, "versions", "__init__.py")
+                        ),
+                        "w",
+                    ),
+                    mock.call().close(),
+                ],
+            )