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}
def drop_db(): ''' Drops Database ''' try: db_name = app.config['DATABASE_NAME'] conn = r.connect() if db_name in r.db_list().run(conn): r.db_drop(db_name).run(conn) except Exception as e: cprint("An error occured --> {0}".format(e), 'red', attrs=['bold'])
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 __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()
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)
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'])
def db_list(self, conn): # rethinkdb is special and always present; we don't care, for these tests return set(r.db_list().run(conn)) - {'rethinkdb'}
def _create_database(self): if self.db not in r.db_list().run(self.connection): r.db_create(self.db).run(self.connection)
def list_db(): return r.db_list()