Esempio n. 1
0
def populate(dbconfig):
    """
    Populates a database
    :param dbconfig: a dict containing db connection settings
    """

    if not dbconfig['yes']:
        verify(dbconfig)

    recreate(dbconfig)
    conn = connect(dbconfig)
    cur = conn.cursor()

    batch_file = os.path.join(sql_repo, 'batch')

    for line in [l.strip() for l in open(batch_file) if not l.startswith("#")]:
        if not line:  # skip empty lines
            continue
        print "processing %s" % line
        sql_file = os.path.join(sql_repo, line)
        with open(sql_file) as sql_handler:
            sql = sql_handler.read()
            dialected = dialectise(sql, dbconfig['engine'], tokens).strip()

            if not dialected:  # empty query, can happen
                continue

            try:
                cur.execute(dialected)
            except Exception as e:
                sys.stderr.write("\nproblem with file \"%s\"\n\n" % sql_file)
                raise
    if dbconfig['engine'] == 'postgresql':
        conn.commit()
    conn.close()
Esempio n. 2
0
def populate(dbconfig):
    """
    Populates a database with TRAP tables.

    args:
        dbconfig: a dict containing db connection settings

    raises an exception when one of the tables already exists.
    """

    if not dbconfig['yes']:
        verify(dbconfig)

    if dbconfig['destroy']:
        destroy(dbconfig)

    conn = connect(dbconfig)
    cur = conn.cursor()

    if dbconfig['engine'] == 'postgresql':
        # make sure plpgsql is enabled
        try:
            cur.execute("CREATE LANGUAGE plpgsql;")
        except conn.ProgrammingError:
            conn.rollback()
    if dbconfig['engine'] == 'monetdb':
        set_monetdb_schema(cur, dbconfig)
        # reconnect to switch to schema
        conn.close()
        conn = connect(dbconfig)
        cur = conn.cursor()

    batch_file = os.path.join(sql_repo, 'batch')

    error = "\nproblem processing \"%s\".\nMaybe the DB is already populated. "\
            "Try -d/--destroy argument for initdb cmd.\n\n"

    for line in [l.strip() for l in open(batch_file) if not l.startswith("#")]:
        if not line:  # skip empty lines
            continue
        print "processing %s" % line
        sql_file = os.path.join(sql_repo, line)
        with open(sql_file) as sql_handler:
            sql = sql_handler.read()
            dialected = dialectise(sql, dbconfig['engine'], tokens).strip()

            if not dialected:  # empty query, can happen
                continue

            try:
                cur.execute(dialected)
            except Exception as e:
                sys.stderr.write(error % sql_file)
                raise
    if dbconfig['engine'] == 'postgresql':
        conn.commit()
    conn.close()
Esempio n. 3
0
def populate(dbconfig):
    """
    Populates a database with TRAP tables.

    args:
        dbconfig: a dict containing db connection settings

    raises an exception when one of the tables already exists.
    """

    if not dbconfig['yes']:
        verify(dbconfig)

    # configure the database before we do anyting else
    get_database_config(dbconfig, apply=True)

    database = tkp.db.database.Database()
    database.connect(check=False)

    if dbconfig['destroy']:
        destroy(dbconfig)

    if dbconfig['engine'] == 'postgresql':
        # make sure plpgsql is enabled
        try:
            database.session.execute("CREATE LANGUAGE plpgsql;")
        except ProgrammingError:
            database.session.rollback()
    if dbconfig['engine'] == 'monetdb':
        set_monetdb_schema(database.session, dbconfig)
        # reconnect to switch to schema
        database.session.commit()
        database.reconnect()

    batch_file = os.path.join(sql_repo, 'batch')

    error = "\nproblem processing \"%s\".\nMaybe the DB is already populated. "\
            "Try -d/--destroy argument for initdb cmd.\n\n"

    tkp.db.model.Base.metadata.create_all(database.alchemy_engine)

    version = tkp.db.model.Version(name='revision',
                                   value=tkp.db.model.SCHEMA_VERSION)
    database.session.add(version)

    tkp.db.quality.sync_rejectreasons(database.session)

    for line in [l.strip() for l in open(batch_file) if not l.startswith("#")]:
        if not line:  # skip empty lines
            continue
        print "processing %s" % line
        sql_file = os.path.join(sql_repo, line)
        with open(sql_file) as sql_handler:
            sql = sql_handler.read()
            dialected = dialectise(sql, dbconfig['engine']).strip()

            if not dialected:  # empty query, can happen
                continue
            try:
                database.session.execute(dialected)
            except Exception as e:
                sys.stderr.write(error % sql_file)
                raise

        database.session.commit()
        database.close()
Esempio n. 4
0
def populate(dbconfig):
    """
    Populates a database with TRAP tables.

    args:
        dbconfig: a dict containing db connection settings

    raises an exception when one of the tables already exists.
    """

    if not dbconfig['yes']:
        verify(dbconfig)

    # configure the database before we do anyting else
    get_database_config(dbconfig, apply=True)

    database = tkp.db.database.Database()
    database.connect(check=False)

    if dbconfig['destroy']:
        destroy(dbconfig)

    if dbconfig['engine'] == 'postgresql':
        # make sure plpgsql is enabled
        try:
            database.session.execute("CREATE LANGUAGE plpgsql;")
        except ProgrammingError:
            database.session.rollback()
    if dbconfig['engine'] == 'monetdb':
        set_monetdb_schema(database.session, dbconfig)
        # reconnect to switch to schema
        database.session.commit()
        database.reconnect()

    batch_file = os.path.join(sql_repo, 'batch')

    error = "\nproblem processing \"%s\".\nMaybe the DB is already populated. "\
            "Try -d/--destroy argument for initdb cmd.\n\n"

    tkp.db.model.Base.metadata.create_all(database.alchemy_engine)

    version = tkp.db.model.Version(name='revision',
                                   value=tkp.db.model.SCHEMA_VERSION)
    database.session.add(version)

    tkp.db.quality.sync_rejectreasons(database.session)

    for line in [l.strip() for l in open(batch_file) if not l.startswith("#")]:
        if not line:  # skip empty lines
            continue
        print "processing %s" % line
        sql_file = os.path.join(sql_repo, line)
        with open(sql_file) as sql_handler:
            sql = sql_handler.read()
            dialected = dialectise(sql, dbconfig['engine']).strip()

            if not dialected:  # empty query, can happen
                continue
            try:
                database.session.execute(dialected)
            except Exception as e:
                sys.stderr.write(error % sql_file)
                raise

        database.session.commit()
        database.close()