From: Mike Bayer Date: Thu, 19 Sep 2019 20:59:27 +0000 (-0400) Subject: Add --package flag to write __init__.py files X-Git-Tag: rel_1_2_0~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51dea2a900d2ef987e655e0073ee3e7e23528a47;p=thirdparty%2Fsqlalchemy%2Falembic.git Add --package flag to write __init__.py files 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 --- diff --git a/alembic/command.py b/alembic/command.py index ad8c6c39..d4acfdbf 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -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 diff --git a/alembic/config.py b/alembic/config.py index 307a2614..22320961 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -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 index 00000000..a428bfc9 --- /dev/null +++ b/docs/build/unreleased/463.rst @@ -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. diff --git a/tests/test_command.py b/tests/test_command.py index e1be6787..e0934792 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -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(), + ], + )