Exemple #1
0
 def execute_alembic(action, target):
     alembic_config = Config('alembic.ini')
     # main section options are set when main section is read
     main_section = alembic_config.config_ini_section
     alembic_config.get_section(main_section)
     alembic_config.cmd_opts = Namespace(
         x=["main_config=config_development.jsonc"])
     if action == 'upgrade':
         command.upgrade(alembic_config, target)
     elif action == 'downgrade':
         command.downgrade(alembic_config, target)
Exemple #2
0
def bootstrap_db(config_uri=None, engine=None, with_migration=True):
    """Bring a blank database to a functional state."""
    if engine is None:
        engine = create_engine(config_uri)
    db.configure(bind=engine)

    if with_migration:
        context = MigrationContext.configure(engine.connect())
        db_version = context.get_current_revision()

        if db_version:
            sys.stderr.write('Database already initialized. Bailing out.\n')
            sys.exit(2)

        config = Config(config_uri)
        app_cfg = config.get_section('app:main')
        script_dir = ScriptDirectory.from_config(config)
        heads = script_dir.get_heads()

        if len(heads) > 1:
            sys.stderr.write('Error: migration scripts have more than one '
                             'head.\nPlease resolve the situation before '
                             'attempting to bootstrap the database.\n')
            sys.exit(2)

    metadata.create_all(engine)

    with transaction.manager:
        School(name=app_cfg.get('sortie.school', 'Default School')).save()

    # Clean up the sccoped session to allow a later app instantiation.
    db.remove()

    if with_migration and heads:
        command.stamp(config, 'head')
Exemple #3
0
def run_migrations_online(config: Config, target_metadata: MetaData) -> None:
    """
    Run migrations in 'online' mode.

    In this scenario we need to create an Engine and associate a connection
    with the context.
    """
    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        # RNC
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            render_as_batch=
            True,  # for SQLite mode; http://stackoverflow.com/questions/30378233  # noqa
            version_table=ALEMBIC_VERSION_TABLE,
            compare_type=custom_compare_type,
            # process_revision_directives=writer,
            process_revision_directives=process_revision_directives,
        )
        with context.begin_transaction():
            context.run_migrations()
Exemple #4
0
def alembic_config(config_filename: str) -> Config:
    """Return altered Alembic config.

    First, read project's alembic configuration (alembic.ini).
    Second, alter project's existing config by passing Ahjo's credential
    configuration file as an 'x' argument and setting 'config_file_name'
    to point to Ahjo's logging configuration file.

    This way Alembic will use Ahjo's loggers and project's configurations
    when running Alembic operations.
    """
    config = Config('alembic.ini')
    main_section = config.config_ini_section
    # main section options are set when main section is read
    config.get_section(main_section)
    config.cmd_opts = Namespace(x=["main_config=" + config_filename])
    config.config_file_name = path.join(AHJO_PATH, 'resources/logger.ini')
    return config
Exemple #5
0
def main(argv, _db_rw=None, _raven_client=None):
    parser = argparse.ArgumentParser(prog=argv[0],
                                     description='Initialize Ichnaea database')

    parser.add_argument('--alembic_ini',
                        help='Path to the alembic migration config.')
    parser.add_argument('--location_ini',
                        help='Path to the ichnaea app config.')
    parser.add_argument('--initdb',
                        action='store_true',
                        help='Initialize database')

    args = parser.parse_args(argv[1:])

    if args.initdb:
        # Either use explicit config file location or fallback
        # on environment variable or finally file in current directory
        if not args.location_ini:
            location_ini = os.environ.get('ICHNAEA_CFG', 'ichnaea.ini')
        else:
            location_ini = args.location_ini
        location_ini = os.path.abspath(location_ini)
        location_cfg = read_config(filename=location_ini)

        # Either use explicit config file location or fallback
        # to a file in the same directory as the ichnaea.ini
        if not args.alembic_ini:
            alembic_ini = os.path.join(os.path.dirname(location_ini),
                                       'alembic.ini')
        else:
            alembic_ini = args.alembic_ini
        alembic_ini = os.path.abspath(alembic_ini)
        alembic_cfg = Config(alembic_ini)
        alembic_section = alembic_cfg.get_section('alembic')

        if _db_rw is None:
            db_rw = Database(alembic_section['sqlalchemy.url'])
        else:
            db_rw = _db_rw
        configure_raven(location_cfg.get('ichnaea', 'sentry_dsn'),
                        _client=_raven_client)

        engine = db_rw.engine
        create_schema(engine, alembic_cfg, location_cfg)
    else:
        parser.print_help()
Exemple #6
0
def main(argv, _db_rw=None, _raven_client=None):
    parser = argparse.ArgumentParser(
        prog=argv[0], description='Initialize Ichnaea database')

    parser.add_argument('--alembic_ini',
                        help='Path to the alembic migration config.')
    parser.add_argument('--location_ini',
                        help='Path to the ichnaea app config.')
    parser.add_argument('--initdb', action='store_true',
                        help='Initialize database')

    args = parser.parse_args(argv[1:])

    if args.initdb:
        # Either use explicit config file location or fallback
        # on environment variable or finally file in current directory
        if not args.location_ini:
            location_ini = os.environ.get('ICHNAEA_CFG', 'ichnaea.ini')
        else:
            location_ini = args.location_ini
        location_ini = os.path.abspath(location_ini)
        location_cfg = read_config(filename=location_ini)

        # Either use explicit config file location or fallback
        # to a file in the same directory as the ichnaea.ini
        if not args.alembic_ini:
            alembic_ini = os.path.join(
                os.path.dirname(location_ini), 'alembic.ini')
        else:
            alembic_ini = args.alembic_ini
        alembic_ini = os.path.abspath(alembic_ini)
        alembic_cfg = Config(alembic_ini)
        alembic_section = alembic_cfg.get_section('alembic')

        if _db_rw is None:
            db_rw = Database(alembic_section['sqlalchemy.url'])
        else:
            db_rw = _db_rw
        configure_raven(
            location_cfg.get('ichnaea', 'sentry_dsn'),
            transport='sync', _client=_raven_client)

        engine = db_rw.engine
        create_schema(engine, alembic_cfg, location_cfg)
    else:
        parser.print_help()
Exemple #7
0
def initialize_db():
    alembic_cfg = Config(DEFAULT_ALEMBIC_INI_PATH)

    db = alembic_cfg.get_main_option("sqlalchemy.url",
                                     PITHOS_BACKEND_DB_CONNECTION)
    alembic_cfg.set_main_option("sqlalchemy.url", db)

    engine = sa.engine_from_config(alembic_cfg.get_section(
        alembic_cfg.config_ini_section),
                                   prefix='sqlalchemy.')

    node.create_tables(engine)
    groups.create_tables(engine)
    public.create_tables(engine)
    xfeatures.create_tables(engine)

    # then, load the Alembic configuration and generate the
    # version table, "stamping" it with the most recent rev:
    command.stamp(alembic_cfg, "head")
Exemple #8
0
def initialize_db(dbconnection):
    alembic_cfg = Config(DEFAULT_ALEMBIC_INI_PATH)

    db = alembic_cfg.get_main_option("sqlalchemy.url", dbconnection)
    alembic_cfg.set_main_option("sqlalchemy.url", db)

    engine = sa.engine_from_config(
        alembic_cfg.get_section(alembic_cfg.config_ini_section),
        prefix='sqlalchemy.')

    node.create_tables(engine)
    groups.create_tables(engine)
    public.create_tables(engine)
    xfeatures.create_tables(engine)
    quotaholder_serials.create_tables(engine)

    # then, load the Alembic configuration and generate the
    # version table, "stamping" it with the most recent rev:
    command.stamp(alembic_cfg, "head")
Exemple #9
0
def alembic_upgrade():
    # set the paths values
    this_file_directory = os.path.dirname(
        os.path.abspath(inspect.stack()[0][1]))
    root_directory = os.path.join(this_file_directory, "..", "..")
    alembic_directory = this_file_directory
    ini_path = os.path.join(root_directory, "alembic.ini")

    # LOGGER.info(f"this_file_directory = {this_file_directory}")
    # LOGGER.info(f"root_directory = {root_directory}")
    # LOGGER.info(f"alembic_directory = {alembic_directory}")
    # LOGGER.info(f"ini_path = {ini_path}")

    # create Alembic config and feed it with paths
    config = AlembicConfig(ini_path)
    # print(config.get_section("alembic"))
    LOGGER.info(f"get_section(alembic) = %s" % (config.get_section("alembic")))
    LOGGER.info(f"get_section(loggers) = %s" % (config.get_section("loggers")))
    LOGGER.info(f"get_section(handlers) = %s" %
                (config.get_section("handlers")))
    LOGGER.info(f"get_section(formatters) = %s" %
                (config.get_section("formatters")))
    LOGGER.info(f"get_section(logger_root) = %s" %
                (config.get_section("logger_root")))
    LOGGER.info(f"get_section(logger_sqlalchemy) = %s" %
                (config.get_section("logger_sqlalchemy")))
    LOGGER.info(f"get_section(logger_alembic) = %s" %
                (config.get_section("logger_alembic")))
    LOGGER.info(f"get_section(handler_console) = %s" %
                (config.get_section("handler_console")))

    config.set_main_option("script_location", alembic_directory)
    # config.cmd_opts = argparse.Namespace()   # arguments stub

    # prepare and run the command
    revision = "head"
    sql = False
    tag = None

    # upgrade command
    # try:
    # import pdb;pdb.set_trace()
    command.upgrade(config, revision, sql=sql, tag=tag)
Exemple #10
0
import os
import sys

from alembic.config import Config

sys.path = ["", ".."] + sys.path[1:]

from alembic import command
from sqlalchemy import engine_from_config

from app.db import metadata

config = Config(
    os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
                 "alembic.ini"))
engine = engine_from_config(config.get_section(config.config_ini_section))


def main():
    metadata.create_all(engine)
    command.stamp(config, "head")


if __name__ == "__main__":
    main()