Ejemplo n.º 1
0
def create_config(app):
    """Programmatically create Alembic config"""
    cfg = Config(stdout=StringIO())
    cfg.get_template_directory = get_template_directory
    migrations = app.get("migrations_dir") or os.path.join(
        app["cwd"], "migrations")
    cfg.set_main_option("script_location", migrations)
    cfg.config_file_name = os.path.join(migrations, "alembic.ini")
    db = app["db"]
    cfg.set_section_option("default", "sqlalchemy.url",
                           str(db.sync_engine.url))
    # put database in main options
    cfg.set_main_option("databases", "default")
    # create empty logging section to avoid raising errors in env.py
    cfg.set_section_option("logging", "path", "")
    cfg.metadata = dict(default=db.metadata)
    return cfg
Ejemplo n.º 2
0
    def to_alembic_config(self) -> AlembicConfig:
        def get_template_directory() -> str:
            return self.template_directory

        alembic = AlembicConfig(
            join(self.script_location, self.config_file_name))

        # set the template directory getter
        alembic.get_template_directory = get_template_directory

        # default alembic confs
        if self.sqlalchemy_url:
            alembic.set_main_option('sqlalchemy.url', self.sqlalchemy_url)

        alembic.set_main_option('script_location', self.script_location)
        alembic.set_main_option('file_template', self.file_template)
        alembic.set_main_option('truncate_slug_length',
                                self.truncate_slug_length)

        return alembic
Ejemplo n.º 3
0
    def _create_config(self):
        """Programmatically create Alembic config. To determine databases,
        DATASTORE from project's config file is used. To customize Alembic
        use MIGRATIONS in you config file.

        Example::

            MIGRATIONS = {
                'alembic': {
                    'script_location': '<path>',
                    'databases': '<db_name1>,<db_name2>',
                },
                '<db_name1>': {
                    'sqlalchemy.url': 'driver://*****:*****@localhost/dbname',
                },
                '<bd_name2>': {
                    'sqlalchemy.url': 'driver://*****:*****@localhost/dbname',
                },
                'logging': {
                    'path': '<path_to_logging_config>',
                }
            }

        For more information about possible options, please visit Alembic
        documentation:
        https://alembic.readthedocs.org/en/latest/index.html
        """
        app = self.app
        cfg = Config()
        cfg.get_template_directory = lux_template_directory
        migrations = os.path.join(app.meta.path, 'migrations')

        cfg.set_main_option('script_location', migrations)
        cfg.config_file_name = os.path.join(migrations, 'alembic.ini')
        odm = app.odm()
        databases = []
        # set section for each found database
        for name, engine in odm.keys_engines():
            if not name:
                name = 'default'
            databases.append(name)
            # url = str(engine.url).replace('+green', '')
            url = str(engine.url)
            cfg.set_section_option(name, 'sqlalchemy.url', url)
        # put databases in main options
        cfg.set_main_option("databases", ','.join(databases))
        # create empty logging section to avoid raising errors in env.py
        cfg.set_section_option('logging', 'path', '')
        # obtain the metadata required for `auto` command
        metadata = {}
        for key, db_engine in odm.keys_engines():
            if not key:
                key = 'default'
            metadata[key] = meta = MetaData()
            for table, engine in odm.binds.items():
                if engine == db_engine:
                    table.tometadata(meta)

        cfg.metadata = metadata

        config = app.config.get('MIGRATIONS')
        if config:
            for section in config.keys():
                for key, value in config[section].items():
                    if section == 'alembic':
                        cfg.set_main_option(key, value)
                    else:
                        cfg.set_section_option(section, key, value)

        return cfg