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.
: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):
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
"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",
--- /dev/null
+.. 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.
import inspect
from io import BytesIO
from io import TextIOWrapper
+import os
import re
from sqlalchemy import exc as sqla_exc
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
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()
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(),
+ ],
+ )