Python alembic.command.init() Examples

The following are 15 code examples of alembic.command.init(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module alembic.command , or try the search function .
Example #1
Source File: env.py    From jbox with MIT License 6 votes vote down vote up
def staging_env(create=True, template="generic", sourceless=False):
    from alembic import command, script
    cfg = _testing_config()
    if create:
        path = os.path.join(_get_staging_directory(), 'scripts')
        if os.path.exists(path):
            shutil.rmtree(path)
        command.init(cfg, path, template=template)
        if sourceless:
            try:
                # do an import so that a .pyc/.pyo is generated.
                util.load_python_file(path, 'env.py')
            except AttributeError:
                # we don't have the migration context set up yet
                # so running the .env py throws this exception.
                # theoretically we could be using py_compiler here to
                # generate .pyc/.pyo without importing but not really
                # worth it.
                pass
            make_sourceless(os.path.join(path, "env.py"))

    sc = script.ScriptDirectory.from_config(cfg)
    return sc 
Example #2
Source File: db.py    From flask-restplus-server-example with MIT License 6 votes vote down vote up
def init_development_data(context, upgrade_db=True, skip_on_failure=False):
    """
    Fill a database with development data like default users.
    """
    if upgrade_db:
        context.invoke_execute(context, 'app.db.upgrade')

    log.info("Initializing development data...")

    from migrations import initial_development_data
    try:
        initial_development_data.init()
    except AssertionError as exception:
        if not skip_on_failure:
            log.error("%s", exception)
        else:
            log.debug(
                "The following error was ignored due to the `skip_on_failure` flag: %s",
                exception
            )
            log.info("Initializing development data step is skipped.")
    else:
        log.info("Fixtures have been successfully applied.") 
Example #3
Source File: test_command.py    From alembic with MIT License 6 votes vote down vote up
def test_init_file_exists_and_is_empty(self):
        def access_(path, mode):
            if "generic" in path or path == "foobar":
                return True
            else:
                return False

        def listdir_(path):
            if path == "foobar":
                return []
            else:
                return ["file1", "file2", "alembic.ini.mako"]

        with mock.patch(
            "alembic.command.os.access", side_effect=access_
        ), mock.patch("alembic.command.os.makedirs") as makedirs, mock.patch(
            "alembic.command.os.listdir", side_effect=listdir_
        ), mock.patch(
            "alembic.command.ScriptDirectory"
        ):
            command.init(self.cfg, directory="foobar")
            eq_(
                makedirs.mock_calls,
                [mock.call(os.path.normpath("foobar/versions"))],
            ) 
Example #4
Source File: test_command.py    From alembic with MIT License 6 votes vote down vote up
def test_init_file_doesnt_exist(self):
        def access_(path, mode):
            if "generic" in path:
                return True
            else:
                return False

        with mock.patch(
            "alembic.command.os.access", side_effect=access_
        ), mock.patch("alembic.command.os.makedirs") as makedirs, mock.patch(
            "alembic.command.ScriptDirectory"
        ):
            command.init(self.cfg, directory="foobar")
            eq_(
                makedirs.mock_calls,
                [
                    mock.call("foobar"),
                    mock.call(os.path.normpath("foobar/versions")),
                ],
            ) 
Example #5
Source File: test_command.py    From alembic with MIT License 6 votes vote down vote up
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(),
                ],
            ) 
Example #6
Source File: __init__.py    From jbox with MIT License 5 votes vote down vote up
def init(directory=None, multidb=False):
    """Generates a new migration"""
    if directory is None:
        directory = current_app.extensions['migrate'].directory
    config = Config()
    config.set_main_option('script_location', directory)
    config.config_file_name = os.path.join(directory, 'alembic.ini')
    config = current_app.extensions['migrate'].\
        migrate.call_configure_callbacks(config)
    if multidb:
        command.init(config, directory, 'flask-multidb')
    else:
        command.init(config, directory, 'flask') 
Example #7
Source File: cli.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def init_db(config):
    """Creates a new migration repository."""

    directory = os.path.join('yui', 'migrations')
    c = Config()
    c.set_main_option('script_location', directory)
    c.set_main_option('sqlalchemy.url', config.DATABASE_URL)

    c.config_file_name = os.path.join(directory, 'alembic.ini')

    command.init(c, directory, 'chatterbox') 
Example #8
Source File: db.py    From flask-restplus-server-example with MIT License 5 votes vote down vote up
def init(context, directory='migrations', multidb=False):
    """Generates a new migration"""
    config = Config()
    config.set_main_option('script_location', directory)
    config.config_file_name = os.path.join(directory, 'alembic.ini')
    if multidb:
        command.init(config, directory, 'flask-multidb')
    else:
        command.init(config, directory, 'flask') 
Example #9
Source File: env.py    From alembic with MIT License 5 votes vote down vote up
def staging_env(create=True, template="generic", sourceless=False):
    from alembic import command, script

    cfg = _testing_config()
    if create:

        path = os.path.join(_get_staging_directory(), "scripts")
        assert not os.path.exists(path), (
            "staging directory %s already exists; poor cleanup?" % path
        )

        command.init(cfg, path, template=template)
        if sourceless:
            try:
                # do an import so that a .pyc/.pyo is generated.
                util.load_python_file(path, "env.py")
            except AttributeError:
                # we don't have the migration context set up yet
                # so running the .env py throws this exception.
                # theoretically we could be using py_compiler here to
                # generate .pyc/.pyo without importing but not really
                # worth it.
                pass
            assert sourceless in (
                "pep3147_envonly",
                "simple",
                "pep3147_everything",
            ), sourceless
            make_sourceless(
                os.path.join(path, "env.py"),
                "pep3147" if "pep3147" in sourceless else "simple",
            )

    sc = script.ScriptDirectory.from_config(cfg)
    return sc 
Example #10
Source File: test_command.py    From alembic with MIT License 5 votes vote down vote up
def test_init_file_exists_and_is_not_empty(self):
        with mock.patch(
            "alembic.command.os.listdir", return_value=["file1", "file2"]
        ), mock.patch("alembic.command.os.access", return_value=True):
            directory = "alembic"
            assert_raises_message(
                util.CommandError,
                "Directory %s already exists and is not empty" % directory,
                command.init,
                self.cfg,
                directory=directory,
            ) 
Example #11
Source File: migrations.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def init(self):
        dirname = self.cfg.get_main_option('script_location')
        alembic_cmd.init(self.cfg, dirname, template='lux') 
Example #12
Source File: alembic_wrapper.py    From build-relengapi with Mozilla Public License 2.0 5 votes vote down vote up
def make_parser(self, subparsers):
        parser = subparsers.add_parser('init', help=self.init.__doc__)
        parser.add_argument('-d', '--directory', dest='directory', default=None,
                            help="migration script directory (default is 'migrations')")
        return parser 
Example #13
Source File: alembic_wrapper.py    From build-relengapi with Mozilla Public License 2.0 5 votes vote down vote up
def run(self, parser, args):
        self.init(**vars(args)) 
Example #14
Source File: alembic_wrapper.py    From build-relengapi with Mozilla Public License 2.0 5 votes vote down vote up
def init(self, directory, **kwargs):
        """Generates a new migration"""
        config = Config()
        config.set_main_option('script_location', directory)
        config.config_file_name = os.path.join(directory, 'alembic.ini')
        command.init(config, directory, 'relengapi') 
Example #15
Source File: env.py    From android_universal with MIT License 5 votes vote down vote up
def staging_env(create=True, template="generic", sourceless=False):
    from alembic import command, script
    cfg = _testing_config()
    if create:
        path = os.path.join(_get_staging_directory(), 'scripts')
        if os.path.exists(path):
            shutil.rmtree(path)
        command.init(cfg, path, template=template)
        if sourceless:
            try:
                # do an import so that a .pyc/.pyo is generated.
                util.load_python_file(path, 'env.py')
            except AttributeError:
                # we don't have the migration context set up yet
                # so running the .env py throws this exception.
                # theoretically we could be using py_compiler here to
                # generate .pyc/.pyo without importing but not really
                # worth it.
                pass
            assert sourceless in (
                "pep3147_envonly", "simple", "pep3147_everything"), sourceless
            make_sourceless(
                os.path.join(path, "env.py"),
                "pep3147" if "pep3147" in sourceless else "simple"
            )

    sc = script.ScriptDirectory.from_config(cfg)
    return sc