예제 #1
0
 def setUpClass(cls):
     cls.conn = r.connect()
     try:
         r.db_drop('test').run(cls.conn)
     except errors.ReqlOpFailedError:
         pass
     r.db_create('test').run(cls.conn)
예제 #2
0
def mk_db():
    try:
        r.db_create(db_name).run(rethink_conn)
    except rethinkdb.errors.ReqlOpFailedError as e:
        if 'already exists' in str(e).lower():
            logger.info("db already exists, won't remake it")
        else:
            raise e
예제 #3
0
def create_db(database, connection):
    # TODO REFACTOR TO contains.do(r.branch) BLOCK
    # TODO PyPi package 2.3.0post6 contains error for the mentioned refactor,
    #  wait for new release
    if r.db_list().contains(database).run(connection):
        return {"dbs_created": 0}
    else:
        r.db_create(database).run(connection)
        return {"dbs_created": 1}
예제 #4
0
    def setUp(self):
        super(DbBaseTestCase, self).setUp()

        # Create new database and switch to it
        try:
            with get_conn() as conn:
                r.db_create('testing').run(conn)
                conn.use('testing')
        except RqlDriverError:
            raise unittest.SkipTest('RethinkDB is unaccessible')
예제 #5
0
def real_stock_data_load(data, connection):
    for db in list(r.db_list().run(connection)):
        if db == "rethinkdb":
            # This db is special and can't be deleted.
            continue
        r.db_drop(db).run(connection)
    for db_name, db_data in iteritems(data['dbs']):
        r.db_create(db_name).run(connection)
        for table_name, table_data in iteritems(db_data['tables']):
            r.db(db_name).table_create(table_name).run(connection)
            r.db(db_name).table(table_name).insert(table_data).run(connection)
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        connection.run(r.db_create(TODO_DB))
        connection.run(r.db(TODO_DB).table_create('todos'))
        print 'Database setup completed. Now run the app without --setup.'
    except Exception:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()
예제 #7
0
    def __init__(self):
        self.db_name = "bitfinex"
        r.connect("localhost", 28015).repl()
        databases = r.db_list().run()
        if self.db_name not in databases:
            r.db_create(self.db_name).run()

        indexes = ["currency", "timestamp"]
        database_tables = r.db(self.db_name).table_list().run()
        tables = ["balance", "orders", "tickers", "trades", "run_exec"]
        for table in tables:
            if table not in database_tables:
                r.db(self.db_name).table_create(table).run()
            # indexes
            indexes_tables = r.db(self.db_name).table(table).index_list().run()
            for index in indexes:
                if index not in indexes_tables:
                    if r.db(self.db_name).table(table).has_fields(index):
                        r.db(self.db_name).table(table).index_create(index).run()
예제 #8
0
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        connection.run(r.db_create(DB_NAME))
        connection.run(r.db(DB_NAME).table_create('tools'))
        connection.run(r.db(DB_NAME).table_create('users'))
        connection.run(r.db(DB_NAME).table_create('categories'))
        connection.run(r.db(DB_NAME).table_create('toolboxes'))
        print 'Database setup completed. Now run the app without --setup.'
    except Exception:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()
예제 #9
0
    def initialise(self):
        """
        Set up database with tables and indexes
        """

        self.connect_with_retry()

        try:
            log.info("rethinkdb initialising")

            # Create databases
            db_exists = r.db_list().contains(self.DB).run(self.conn)
            if not db_exists:
                log.info(f'creating database {self.DB}')
                r.db_create(self.DB).run(self.conn)

            # Create tables
            table_exists = r.db(self.DB).table_list().contains(self.TABLE).run(self.conn)

            if not table_exists:
                log.info(f'adding table {self.TABLE}')
                r.db(self.DB).table_create(self.TABLE).run(self.conn)

            # Create indexes
            rtable = r.db(self.DB).table(self.TABLE)

            current_indexes = rtable.index_list().run(self.conn)
            for index in self.INDEXES:
                if index not in current_indexes:
                    log.info(f'adding index {index}')
                    rtable.index_create(index).run(self.conn)

            log.info("rethinkdb ready")

        except ReqlDriverError as err:
            log.error(f"rethinkdb failed to initialise: {err}")
            sys.exit(1)
예제 #10
0
def connect():
    conn = r.connect("db")
    queries = [
        r.db_create(DB),
        db().table_create(REPORTS),
        db().table_create(TARGETS),
        t_reports().index_create("target_id"),
        t_reports().index_create("date"),
    ]
    for q in queries:
        try:
            q.run(conn)
        except r.RqlRuntimeError:
            pass
    return conn
예제 #11
0
    def setup(self):
        """ setup must be called before everything """
        if self.__is_setup:
            return
        self.__is_setup = True

        conn = r.connect(**self.__connect_kwargs)

        def safe_run(rsql, show_error=False):
            try:
                return rsql.run(conn)
            except r.RqlRuntimeError as e:
                if show_error:
                    logger.warning("safe_run rsql:%s, error:%s", rsql, e)
                return False

        # init databases here
        safe_run(r.db_create(self.__dbname))

        rdb = r.db(self.__dbname)
        for tbl in self.__tables.values():
            table_name = tbl['name']
            primary_key = tbl.get('primary_key', 'id')
            safe_run(rdb.table_create(table_name, primary_key=primary_key))

        # reset database
        safe_run(rdb.table("users").index_create("token"))
        safe_run(rdb.table("devices").replace(lambda q: q.without("sources")))

        # reload add idle check functions
        from .views.device import D  # must import in here

        devices = safe_run(rdb.table("devices").filter({
            "using": True
        }).pluck("udid"),
                           show_error=True)

        if devices:
            for d in devices:
                logger.debug("Device: %s is in using state", d['udid'])
                D(d['udid']).release_until_idle()

        r.set_loop_type("tornado")
예제 #12
0
def migrate():
    '''
    Creates Database
    '''
    try:
        db_name = app.config['DATABASE_NAME']
        conn = r.connect()

        # Create Tables
        if db_name not in r.db_list().run(conn):
            db = r.db_create(db_name).run(conn)
            print("Created database '{0}'...".format(db_name))

        # Create the application tables if they do not exist
        lib = importlib.import_module('api.models')
        for cls in inspect.getmembers(lib, inspect.isclass):
            for base in cls[1].__bases__:
                if base.__name__ == "RethinkDBModel":
                    table_name = getattr(cls[1], '_table')
                    r.db(db_name).table_create(table_name).run(conn)
                    print("Created table '{0}'...".format(table_name))
        print("Running RethinkDB migration command")
    except Exception as e:
        cprint("An error occured --> {0}".format(e), 'red', attrs=['bold'])
예제 #13
0
from rethinkdb import r

r.connect('localhost', 28015).repl()
r.db_create('cosmic-retro').run()

# Table creation
r.db('cosmic-retro').table_create('feedbacks').run()
예제 #14
0
 def _create_database(self):
     if self.db not in r.db_list().run(self.connection):
         r.db_create(self.db).run(self.connection)
예제 #15
0
def create_db(db_name):
    return r.db_create(db_name)
예제 #16
0
 def test_db_create(self, conn):
     expected = set(['db_one', 'db_two', 'db_three'])
     r.db_create('db_three').run(conn)
     result = self.db_list(conn)
     assertEqual(expected, result)
def main(mmCIFPath, logPath):
    # Start time
    start = time.time()
    # Logging
    logging.basicConfig(
        filename=logPath,
        level=logging.DEBUG
    )

    # Connect to DB
    try:
        conn = r.connect()
        logging.info('Connected to DB')
        print('Connected to DB')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Create DB and connect to it
    try:
        r.db_create('pdb_compounds').run()
        conn.use('pdb_compounds')
        logging.info('Created DB and connected to it')
        print('Created DB and connected to it')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Create table
    try:
        r.db('pdb_compounds') \
            .table_create('compounds', primary_key='_chem_comp.id') \
            .run()
        logging.info('Created Table: compounds')
        print('Created Table: compounds')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Iterate through the mmCIF files and write to DB
    for cifFile in glob.iglob(os.path.join(mmCIFPath, '*.cif')):
        try:
            data = MMCIF2Dict(cifFile)
            dataJSON = json.dumps(data)         # Creates JSON string
            dataDecoded = json.loads(dataJSON)  # Needed to create valid JSON
            # Insert the data into the DB
            result = r.table('compounds').insert(dataDecoded).run()
            logging.info(
                'Insertion: ID: {id} | Error: {error} | ' \
                'Inserted: {inserted}'.format(
                    id=data['_chem_comp.id'],
                    error=result['errors'],
                    inserted=result['inserted']
                )
            )
            print('Success: ', cifFile)
        except Exception as e:
            logging.debug(
                'File: {filename} | Error: {error}'.format(
                    filename=cifFile,
                    error=e
                )
            )
            print('Error: ', cifFile)
    # Close DB Connection
    conn.close()
    logging.info('Disconnected to DB')
    print('Disconnected from DB')

    # End time
    end = time.time()
    timeTaken = (end - start) / 60
    logging.info('Time Taken: {time} mins'.format(time=timeTaken))
    print('Time Taken: {time} mins'.format(time=timeTaken))