src/dirbs/cli/db.py
def install(ctx):
    """Installs latest schema on clean DB instance."""
    logger = logging.getLogger('dirbs.db')
    config = common.ensure_config(ctx)
    db_config = config.db_config
    with utils.create_db_connection(db_config) as conn, conn.cursor() as cur:
        logger.info('Creating initial base DB schema in DB %s on host %s',
                    db_config.database,
                    db_config.host)

        # Check if there is stuff already in there
        cur.execute("""SELECT COUNT(*)
                         FROM pg_class c
                         JOIN pg_namespace n ON n.oid = c.relnamespace
                        WHERE n.nspname = current_schema()""")
        is_clean = (cur.fetchone()[0] == 0)
        if not is_clean:
            logger.error("Can\'t install latest schema into a non-clean DB")
            logger.error('Instead, use dirbs-db upgrade to upgrade the schema to the latest version')
            sys.exit(1)

        # Set our role here so that new objects get created with dirbs_core_power_user as owner by default
        with utils.db_role_setter(conn, role_name='dirbs_core_power_user'):
            # First we setup the schema, search path etc.
            sql = pkgutil.get_data('dirbs', 'sql/base/on_db_creation.sql')
            cur.execute(sql)

            # Install the base schema for v19 and set current version to 19
            base_schema = 'sql/base/v19_schema.sql'
            logger.info('Restoring base v19 schema from SQL file: %s', base_schema)
            sql = pkgutil.get_data('dirbs', base_schema)
            cur.execute(sql)
            utils.set_db_schema_version(conn, min_schema_version)
            logger.info('Successfully created base v{0:d} schema. Scheduling dirbs-db upgrade...'
                        .format(min_schema_version))

    # Then we call upgrade to complete the process
    rv = 0
    if code_db_schema_version > min_schema_version:
        rv = ctx.invoke(upgrade)
    else:
        # Can't do anything until we know the schema is the right version
        _store_job_metadata(config, 'install')

    return rv
Installs latest schema on clean DB instance.
Installs latest schema on clean DB instance.
Installs latest schema on clean DB instance .
def install(ctx):
    logger = logging.getLogger('dirbs.db')
    config = common.ensure_config(ctx)
    db_config = config.db_config
    with utils.create_db_connection(db_config) as conn, conn.cursor() as cur:
        logger.info('Creating initial base DB schema in DB %s on host %s',
                    db_config.database,
                    db_config.host)
        cur.execute("""SELECT COUNT(*)
                         FROM pg_class c
                         JOIN pg_namespace n ON n.oid = c.relnamespace
                        WHERE n.nspname = current_schema()""")
        is_clean = (cur.fetchone()[0] == 0)
        if not is_clean:
            logger.error("Can\'t install latest schema into a non-clean DB")
            logger.error('Instead, use dirbs-db upgrade to upgrade the schema to the latest version')
            sys.exit(1)
        with utils.db_role_setter(conn, role_name='dirbs_core_power_user'):
            sql = pkgutil.get_data('dirbs', 'sql/base/on_db_creation.sql')
            cur.execute(sql)
            base_schema = 'sql/base/v19_schema.sql'
            logger.info('Restoring base v19 schema from SQL file: %s', base_schema)
            sql = pkgutil.get_data('dirbs', base_schema)
            cur.execute(sql)
            utils.set_db_schema_version(conn, min_schema_version)
            logger.info('Successfully created base v{0:d} schema. Scheduling dirbs-db upgrade...'
                        .format(min_schema_version))
    rv = 0
    if code_db_schema_version > min_schema_version:
        rv = ctx.invoke(upgrade)
    else:
        _store_job_metadata(config, 'install')
    return rv
def install ( ctx ) : logger = logging . getLogger ( 'dirbs.db' ) config = common . ensure_config ( ctx ) db_config = config . db_config with utils . create_db_connection ( db_config ) as conn , conn . cursor ( ) as cur : logger . info ( 'Creating initial base DB schema in DB %s on host %s' , db_config . database , db_config . host ) cur . execute ( """SELECT COUNT(*)
                         FROM pg_class c
                         JOIN pg_namespace n ON n.oid = c.relnamespace
                        WHERE n.nspname = current_schema()""" ) is_clean = ( cur . fetchone ( ) [ 0 ] == 0 ) if not is_clean : logger . error ( "Can\'t install latest schema into a non-clean DB" ) logger . error ( 'Instead, use dirbs-db upgrade to upgrade the schema to the latest version' ) sys . exit ( 1 ) with utils . db_role_setter ( conn , role_name = 'dirbs_core_power_user' ) : sql = pkgutil . get_data ( 'dirbs' , 'sql/base/on_db_creation.sql' ) cur . execute ( sql ) base_schema = 'sql/base/v19_schema.sql' logger . info ( 'Restoring base v19 schema from SQL file: %s' , base_schema ) sql = pkgutil . get_data ( 'dirbs' , base_schema ) cur . execute ( sql ) utils . set_db_schema_version ( conn , min_schema_version ) logger . info ( 'Successfully created base v{0:d} schema. Scheduling dirbs-db upgrade...' . format ( min_schema_version ) ) rv = 0 if code_db_schema_version > min_schema_version : rv = ctx . invoke ( upgrade ) else : _store_job_metadata ( config , 'install' ) return rv
Installs latest schema on clean DB instance.
Installs latest schema on clean DB instance .
"""Installs latest schema on clean DB instance.""" # Check if there is stuff already in there # Set our role here so that new objects get created with dirbs_core_power_user as owner by default # First we setup the schema, search path etc. # Install the base schema for v19 and set current version to 19 # Then we call upgrade to complete the process # Can't do anything until we know the schema is the right version
ctx