def init_database_with_default_tables(args): """ Create a new RethinkDB database and initialise (default) tables :param args: an argparse argument (force) """ # Add additional (default) tables here... def_tables = ['determined_variants', 'strains_under_investigation', 'references', 'reference_features', 'strain_features'] with database.make_connection() as connection: try: r.db_create(connection.db).run(connection) for atable in def_tables: r.db(connection.db).table_create(atable).run(connection) except RqlRuntimeError: print ("Database %s already exists. Use '--force' option to " "reinitialise the database." % (connection.db)) if args.force: print "Reinitialising %s" % (connection.db) r.db_drop(connection.db).run(connection) r.db_create(connection.db).run(connection) for atable in def_tables: r.db(connection.db).table_create(atable).run(connection) else: sys.exit(1) print ("Initalised database %s. %s contains the following tables: " "%s" % (connection.db, connection.db, ', '.join(def_tables)))
def main(): # connect rethinkdb rethinkdb.connect("localhost", 28015, "mysql") try: rethinkdb.db_drop("mysql").run() except: pass rethinkdb.db_create("mysql").run() tables = [ "dept_emp", "dept_manager", "titles", "salaries", "employees", "departments" ] for table in tables: rethinkdb.db("mysql").table_create(table).run() stream = BinLogStreamReader( connection_settings=MYSQL_SETTINGS, blocking=True, only_events=[DeleteRowsEvent, WriteRowsEvent, UpdateRowsEvent], ) # process Feed for binlogevent in stream: if not isinstance(binlogevent, WriteRowsEvent): continue for row in binlogevent.rows: if not binlogevent.schema == "employees": continue vals = dict((str(k), str(v)) for k, v in row["values"].iteritems()) rethinkdb.table(binlogevent.table).insert(vals).run() stream.close()
def drop_db(self): try: print('Eliminando la base de datos: ', self.db_name) r.db_drop(self.db_name).run(self.connection) print('Base de datos: ', self.db_name, ' Eliminada') except ReqlOpFailedError: print('La base de datos: ', self.db_name, ' No existe')
def endPaperTrading(self, endCode, sysTuple): """logic to close all remaining positions in cache, add to oBook, pBook. endCode decides a hard exit or soft exit, e.g wait for strategies or close out of all positions regardless of strats """ if (endCode == cst.SOFT_EXIT): print("SOFT_EXIT INITIATED. ALL ONGOING TRADES BEING FINALIZED.") tradingRef = self.dbRef.table("PositionCache") while (int(self.tradingRef.count().run(self.conn)) > 0): self.loopSystemLogic(*sysTuple) if (endCode == cst.HARD_EXIT): print("HARD_EXIT INITIATED. ALL ONGOING TRADES TERMINATED.") self.databaseManager.read('trading', 'exitValidPositions', cst.HARD_EXIT, marketData) filledExitOrders, completedPositions = self.databaseManager.processTasks( ) self.databaseManager.write("statistics", "updateCapitalStats") self.databaseManager.write('books', 'addToPositionBook', completedPositions) self.databaseManager.write('books', 'addToOrderBook', filledExitOrders) self.databaseManager.processTasks() rStats, cStats, oBook, pBook = self.databaseManager.collectInstData() r.db_drop(self.dbName).run(self.conn) histDF = self.techInds[0].getDataframe() return (rStats, cStats, oBook, pBook, histDF)
def tearDown(self): """Clean the database.""" super().tearDown() with r.connect(**self._credentials) as conn: r.db(DB).table_drop(TABLE).run(conn) r.db_drop(DB).run(conn)
def del_db(_conn: r.net.DefaultConnection, _name_db: str = rtm_cfg_name_db, _expr: bool = False): p = False while True: try: if not cdn(_name_db): cdw_prevent_creation_or_deletion_if_string_check_fail( _name_db, True, False) return None if not check_db(_conn, _name_db): return None if _expr: return r.db_drop(_name_db) else: return r.db_drop(_name_db).run(_conn) except r.errors.ReqlDriverError: _conn.reconnect() if not p: print("\n{}{}\n{}".format( "there is no database connection and/or there is no ", "internet connection", "re - trying database connection")) p = True
def main(): import rethinkdb as r from rethinkdb.errors import RqlRuntimeError # Lib para auxilio na insercao de dados de teste from faker import Factory fake = Factory.create('pt_BR') # Conecta ao banco local r.connect(HOST, PORT).repl() try: r.db_drop(DBNAME).run() except RqlRuntimeError: pass # Cria o banco de dados r.db_create(DBNAME).run() # Cria a tabela r.db(DBNAME).table_create(TABLENAME).run() # Insere os registros na tabela for frase in range(TOTAL_FRASES): reg = { 'id': frase, 'frase': fake.text(), 'autor': fake.name() } r.db(DBNAME).table(TABLENAME).insert(reg).run()
def init(conn, event): # try to drop table (may or may not exist) rv = '' try: r.db_drop(TIX).run(conn) rv = 'dropped, then created' except: rv = 'created' r.db_create(TIX).run(conn) r.db(TIX).table_create(VENU).run(conn) r.db(TIX).table(VENU).index_create(TS).run(conn) smap = {} umap = {} for x in range(1, CNT + 1): smap[str(x)] = 'free' umap[str(x)] = '' rv += str(r.db(TIX).table(VENU).insert({ ID: 0, SMAP: smap, UMAP: umap, MAX: CNT, TS: time.time() }).run(conn)) return rv
def init(conn, event): # try to drop table (may or may not exist) rv = '' try: r.db_drop(TIX).run(conn) rv = 'dropped, then created' except: rv = 'created' r.db_create(TIX).run(conn) r.db(TIX).table_create(VENU).run(conn) r.db(TIX).table(VENU).index_create(TS).run(conn) smap = {} umap = {} for x in range(1, CNT + 1): smap[str(x)] = 'free' umap[str(x)] = '' rv += str( r.db(TIX).table(VENU).insert({ ID: 0, SMAP: smap, UMAP: umap, MAX: CNT, TS: time.time() }).run(conn)) return rv
def client(request): """ Client fixture. """ config = ConfigShim() # check if rethinkdb temp database already exists db_name = config.get("database", "database_name") with rethinkdb.connect(host=config.get("database", "ip"), port=config.get("database", "port"), db=config.get("database", "database_name")) as conn: if db_name in rethinkdb.db_list().run(conn): rethinkdb.db_drop(db_name).run(conn) log.warning("Dropped temporary test database {0}".format(db_name)) web = WebApp(config) test_client = web.flask_app.test_client() def teardown(): with web.db.connect() as conn: rethinkdb.db_drop(db_name).run(conn) log.info("Dropped temporary test database {0}".format(db_name)) request.addfinalizer(teardown) return test_client
def main(): # connect rethinkdb rethinkdb.connect("localhost", 28015, "mysql") try: rethinkdb.db_drop("mysql").run() except: pass rethinkdb.db_create("mysql").run() tables = ["dept_emp", "dept_manager", "titles", "salaries", "employees", "departments"] for table in tables: rethinkdb.db("mysql").table_create(table).run() stream = BinLogStreamReader( connection_settings=MYSQL_SETTINGS, blocking=True, only_events=[DeleteRowsEvent, WriteRowsEvent, UpdateRowsEvent], ) # process Feed for binlogevent in stream: if not isinstance(binlogevent, WriteRowsEvent): continue for row in binlogevent.rows: if not binlogevent.schema == "employees": continue vals = {} vals = {str(k): str(v) for k, v in row["values"].iteritems()} rethinkdb.table(binlogevent.table).insert(vals).run() stream.close()
def drop_test(): subprocess.check_call(['sudo', 'apt-get', 'install', 'python3-pip']) subprocess.check_call(['sudo', 'pip3', 'install', 'rethinkdb']) import rethinkdb as r conn = r.connect(host="localhost", port=config()['driver_port'], db='test').repl() r.db_drop('test').run(conn) conn.close()
def drop_db(): logger.info("Dropping RethinkDB database '{}'...".format(RETHINKDB_NAME)) try: with get_connection() as conn: r.db_drop(RETHINKDB_NAME).run(conn) logger.info("RethinkDB database dropped successfully.") except r.RqlRuntimeError: logger.info("Database '{}' does not exist on {}:{}".format( RETHINKDB_NAME, RETHINKDB_HOST, RETHINKDB_PORT))
def db_drop(): connection = r.connect(host=RDB_HOST, port=RDB_PORT) try: r.db_drop(DB).run(connection) print "Database dropped." except: print "Error in dropping database" finally: connection.close()
def delete_databases(dbnames=[]): b = Bigchain() for dbname in dbnames: logger.info('Dropping database: {}'.format(dbname)) try: r.db_drop(dbname).run(b.conn) except r.ReqlOpFailedError as e: logger.info(e.message)
def drop_database(rethinkdb_conn, db, testing=False): """ Utility function to drop the rethinkdb database and tables for notouch. rethinkdb_conn - A rethinkdb connection object. testing - Drop the database without prompting. """ if db in rethinkdb.db_list().run(rethinkdb_conn) and testing: rethinkdb.db_drop(db)
def drop_database(): """ Deletes the RethinkDB database """ try: with get_connection() as conn: rethink.db_drop(DEFAULT_DB_NAME).run(conn) except (RqlRuntimeError, RqlDriverError) as err: print(err.message)
def tearDownClass(self): """ Drops the test database after the classes' tests are finished """ with rethinkdb.connect(host='localhost', port=28015) as conn: if 'TEST' in rethinkdb.db_list().run(conn): rethinkdb.db_drop('TEST').run(conn) try: self.rdb.close() except AttributeError: pass
def drop_database(): """ Deletes the RethinkDB database """ try: with get_connection() as conn: rethink.db_drop(DEFAULT_DB_NAME).run(conn) except (RqlRuntimeError, RqlDriverError) as err: raise err
def setUpClass(cls): super(APITests, cls).setUpClass() cls.conn = r.connect(host=settings.RETHINK_DB_HOST, port=settings.RETHINK_DB_PORT) try: r.db_drop(settings.RETHINK_DB_DB).run(cls.conn) except: pass r.db_create(settings.RETHINK_DB_DB).run(cls.conn) cls.conn.db = settings.RETHINK_DB_DB management.call_command('syncrethinkdb', verbosity=0)
def init_db(): """ Initialize the database. """ with connect() as con: try: r.db_drop('awe').run(con) except r.ReqlOpFailedError: pass r.db_create('awe').run(con)
def test_configure(self): # r.table_drop(self.table_name).run(self.connection) r.db_drop(self.db_name).run(self.connection) self.db.configure() db_exists = r.db_list().contains(self.db_name).run(self.connection) self.assertTrue( db_exists ) table_exists = r.db(self.db_name).table_list().contains(self.table_name).run(self.connection) self.assertTrue( table_exists )
def reset_database(self): """ Database reset. If the database exists drops it and recreates. If it doesn't exist, create it """ db_name = self.db_name with self.connect() as conn: if db_name in r.db_list().run(conn): r.db_drop(db_name).run(conn) r.db_create(db_name).run(conn)
def rethink_unique_db(rethink_server_sess): """ Starts up a session-scoped server, and returns a connection to a unique database for the life of a single test, and drops it after """ dbid = uuid.uuid4().hex conn = rethink_server_sess.conn rethinkdb.db_create(dbid).run(conn) conn.use(dbid) yield conn rethinkdb.db_drop(dbid).run(conn)
def DropTable(name, conn): '''Dropping tables -- for clean-up.''' try: r.db_drop(name).run(conn) return True except Exception as e: print "Could not drop table `%s`" % name return False
def fin(): print('Deleting `{}` database'.format(db_name)) get_conn().repl() try: r.db_drop(db_name).run() except r.ReqlOpFailedError as e: if e.message != 'Database `{}` does not exist.'.format(db_name): raise print('Finished deleting `{}`'.format(db_name))
def test_configure(self): # r.table_drop(self.table_name).run(self.connection) r.db_drop(self.db_name).run(self.connection) self.db.configure() db_exists = r.db_list().contains(self.db_name).run(self.connection) self.assertTrue(db_exists) table_exists = r.db(self.db_name).table_list().contains( self.table_name).run(self.connection) self.assertTrue(table_exists)
def db_reset(): if yn_choice("Are you sure you wish to reset the Database?\nAll data will be lost and this cannot be undone.", 'n'): connection = r.connect(host=RDB_HOST, port=RDB_PORT, db=CP2020_DB) try: r.db_drop(CP2020_DB).run(connection) print ('Database deleted') except RqlRuntimeError: print ('Database could not be deleted') finally: connection.close() db_setup()
def real_stock_data_load(data, connection): for db in list(r.db_list().run(connection)): if db == u"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 db_init(dest_ip): # connect conn = r.connect(dest_ip, 28015) state = '' try: r.db_drop('authentication').run(conn) state = 'deleted, then created' except: state = 'created' r.db_create('authentication').run(conn) r.db('authentication').table_create('user', primary_key = 'account').run(conn)
def init_tables(): """ Create the tables we are going to use """ global connection, tables print "Creating databases/tables...", sys.stdout.flush() try: r.db_drop("test").run(connection) except r.errors.RqlRuntimeError, e: pass
def drop_database(self): """Drop the db for the application. If the db exists, log a warning. Returns: None """ dbs = r.db_list().run(self.connection) if self.db in dbs: r.db_drop(self.db).run(self.connection)
def setUpClass(cls): cls.connection = r.connect(host=DB.RDB_HOST, port=DB.RDB_PORT, db=DB_AWS_TEST) try: # Create the DB tables we need DB.Database.create_db_structure(DB_AWS_TEST) test_conf = TestingConfig(DB_AWS_TEST) cls.app = app.create_app(test_conf).test_client() # print 'Set-up DONE. DB-name: {db_name}'.format(db_name=DB_AWS_TEST) except RqlRuntimeError: # print 'The test-database already exist. Will remove it, and then re-run the test!' r.db_drop(DB_AWS_TEST).run(cls.connection) cls.setUpClass()
def main(): rethinkdbprocess = pexpect.spawn("rethinkdb -d {} --bind 127.0.0.1".format( jasperpath.RETHINKDB_DATA_PATH, )) rethinkdbprocess.expect('Listening on http addresses: 127.0.0.1, ::1', timeout=None) rethinkdb.connect("localhost", 28015).repl() try: rethinkdb.db_drop("test").run() rethinkdb.db_create("Jasper").run() except BaseException: pass rethinkdb.connect("localhost", 28015, db='Jasper').repl() birthday_list()
def setUpClass(cls): super(SeleniumTests, cls).setUpClass() cls.selenium = WebDriver() cls.selenium.implicitly_wait(10) cls.conn = r.connect(host=settings.RETHINK_DB_HOST, port=settings.RETHINK_DB_PORT) try: r.db_drop(settings.RETHINK_DB_DB).run(cls.conn) except: pass r.db_create(settings.RETHINK_DB_DB).run(cls.conn) cls.conn.db = settings.RETHINK_DB_DB management.call_command('syncrethinkdb', verbosity=0)
def rethink_module_db(rethink_server_sess): """ Starts up a session-scoped server, and returns a connection to a unique database for all the tests in one module. Drops the database after module tests are complete. """ dbid = uuid.uuid4().hex conn = rethink_server_sess.conn log.info("Making database") rethinkdb.db_create(dbid).run(conn) conn.use(dbid) yield conn log.info("Dropping database") rethinkdb.db_drop(dbid).run(conn)
def db_init(db_name, table_name): print 'Initializing %s table' % INFO conn = r.connect(host=DB_ADDRESS, port=DB_PORT) try: r.db_drop(db_name).run(conn) except: print "couldn't drop old table" pass r.db_create(db_name).run(conn) r.db(db_name).table_create(table_name).run(conn) r.db(db_name).table(table_name).index_create(USER_ID).run(conn) return 'DB initialized'
def init(conn, event): # try to drop table (may or may not exist) rv = '' try: r.db_drop(CHAT).run(conn) rv = 'dropped, then created' except: rv = 'created' r.db_create(CHAT).run(conn); r.db(CHAT).table_create(MSGS).run(conn); r.db(CHAT).table(MSGS).index_create(TS).run(conn) return rv
def init(conn, event): # try to drop table (may or may not exist) rv = '' try: r.db_drop(CHAT).run(conn) rv = 'dropped, then created' except: rv = 'created' r.db_create(CHAT).run(conn) r.db(CHAT).table_create(MSGS).run(conn) r.db(CHAT).table(MSGS).index_create(TS).run(conn) return rv
def drop_tables(): ''' Delete all data in the tables (destroy databases) ''' # Postgres Base.metadata.drop_all(engine) # Rethinkdb with get_reql_connection() as conn: try: r.db_drop(PURPLE_DB).run(conn) except RqlRuntimeError: pass
def _rethinkdb(request): if request.config.getoption("--only-client"): yield None return import rethinkdb as r conn = r.connect("localhost", 28015) # drop/create test database if "test" in r.db_list().run(conn): r.db_drop("test").run(conn) r.db_create("test").run(conn) # reconnect with db=test and repl r.connect(db="test").repl() r.table_create("ebooks", primary_key="ebook_id").run() r.table_create("versions", primary_key="version_id").run() r.table_create("formats", primary_key="file_hash").run() r.table_create("sync_events").run() def create_index(table, name, index=None): if name not in r.table(table).index_list().run(): if index is None: r.db("test").table(table).index_create(name).run() else: r.db("test").table(table).index_create(name, index).run() r.db("test").table(table).index_wait(name).run() # create FK indexes create_index("ebooks", "authortitle", index=[r.row["author"].downcase(), r.row["title"].downcase()]) create_index("ebooks", "asin", index=r.row["meta"]["asin"]) create_index("ebooks", "isbn", index=r.row["meta"]["isbn"]) create_index("versions", "ebook_id") create_index("versions", "original_filehash") create_index("versions", "ebook_username", index=[r.row["ebook_id"], r.row["username"]]) create_index("formats", "version_id") create_index("formats", "uploaded") create_index("formats", "uploaded_by") create_index("formats", "uploadedby_dedrm", index=[r.row["uploaded_by"], r.row["dedrm"]]) create_index("sync_events", "username") create_index("sync_events", "timestamp") create_index("sync_events", "user_new_books_count", index=[r.row["username"], r.row["new_books_count"]]) yield r # remove test database if "test" in r.db_list().run(conn): r.db_drop("test").run(conn) conn.close()
def db_init(host): # try to drop table (may or may not exist) conn = r.connect(host, 28015) rv = '' try: r.db_drop('vocab').run(conn) rv = 'dropped, then created' except: rv = 'created' r.db_create('vocab').run(conn); r.db('vocab').table_create('kindle_users', primary_key='fbid').run(conn); return rv
def main(): conn = r.connect( host='localhost', port=28015, db='rwrapper' ).repl() try: r.db_drop('rwrapper').run() except: pass r.db_create('rwrapper').run() r.table_create('test_1').run() # C create = MyTable( field1='something', field2='something else', field3={ 'test': 'created' }, field4=[{ 'record': 1 }, { 'record': 2 }] ) create.save() print "Inserted new row: %s" % create.id # R read = MyTable(id=create.id).get() print "Reading data for %s: %s" % ( create.id, read) # U update = MyTable(id=create.id).get(MyTable) update.field1 = 'something new' update.save() print "Updating data for: %s" % create.id read = MyTable(id=create.id).get() print "New data for %s: %s" % ( create.id, read) # D delete = MyTable(id=create.id).delete() print "Deleted %s" % create.id
def create_db(port): print "Creating samplesdb..." conn = r.connect("localhost", port) run(r.db_drop("samplesdb"), conn) run(r.db_create("samplesdb"), conn) conn.close() print "Done..."
def db_drop(name, server='default'): """DB Drop Deletes an existing DB from the given server Args: name (str): The name of the DB to create server (str): The name of the server to create the DB on Returns: bool """ try: # Fetch the connection with connect_with(server) as oCon: # Delete the DB dRes = r.db_drop(name).run(oCon) # If the DB wasn't deleted if 'dbs_dropped' not in dRes or not dRes['dbs_dropped']: return False # If there's no such DB except r.errors.RqlRuntimeError: return False # Return ok return True
def drop_database(connection, dbname): try: logger.info('Drop database `%s`', dbname) connection.run(r.db_drop(dbname)) logger.info('Done.') except r.ReqlOpFailedError: raise exceptions.DatabaseDoesNotExist('Database `{}` does not exist'.format(dbname))
def db_setup(): connection = r.connect(host=RDB_HOST, port=RDB_PORT) try: r.db_create(DEBATE_DB).run(connection) except RqlRuntimeError: # lets crate a new and clean db. r.db_drop(DEBATE_DB).run(connection) r.db_create(DEBATE_DB).run(connection) connection.close() connection = r.connect(host=RDB_HOST, port=RDB_PORT, db=DEBATE_DB) for table in DB_TABLES: r.db(DEBATE_DB).table_create(table).run(connection) print 'Table {0} created.'.format(table) connection.close()
def test_create_secondary_indexes(): conn = backend.connect() dbname = bigchaindb.config['database']['name'] # The db is set up by fixtures so we need to remove it # and recreate it just with one table conn.run(r.db_drop(dbname)) schema.create_database(conn, dbname) schema.create_tables(conn, dbname) schema.create_indexes(conn, dbname) # Bigchain table assert conn.run(r.db(dbname).table('bigchain').index_list().contains( 'block_timestamp')) is True assert conn.run(r.db(dbname).table('bigchain').index_list().contains( 'transaction_id')) is True assert conn.run(r.db(dbname).table('bigchain').index_list().contains( 'asset_id')) is True assert conn.run(r.db(dbname).table('bigchain').index_list().contains( 'inputs')) is True assert conn.run(r.db(dbname).table('bigchain').index_list().contains( 'outputs')) is True # Backlog table assert conn.run(r.db(dbname).table('backlog').index_list().contains( 'assignee__transaction_timestamp')) is True # Votes table assert conn.run(r.db(dbname).table('votes').index_list().contains( 'block_and_voter')) is True
def db_setup(): try: r.db_create(db_name).run(db_connection) except RqlRuntimeError: r.db_drop(db_name).run(db_connection) r.db_create(db_name).run(db_connection) try: r.db(db_name).table_create('township_village').run(db_connection) load_inital_data() print 'Table creation completed' except RqlRuntimeError: print 'Tables have been created. Nothing to do.' print 'Database already exists. Nothing to do.' db_connection.close()
def setup_database(request, node_config): print('Initializing test db') db_name = node_config['database']['name'] get_conn().repl() try: r.db_create(db_name).run() except r.ReqlOpFailedError as e: if e.message == 'Database `{}` already exists.'.format(db_name): r.db_drop(db_name).run() r.db_create(db_name).run() else: raise print('Finished initializing test db') # setup tables r.db(db_name).table_create('bigchain').run() r.db(db_name).table_create('backlog').run() # create the secondary indexes # to order blocks by timestamp r.db(db_name).table('bigchain').index_create( 'block_timestamp', r.row['block']['timestamp']).run() # to order blocks by block number r.db(db_name).table('bigchain').index_create( 'block_number', r.row['block']['block_number']).run() # to order transactions by timestamp r.db(db_name).table('backlog').index_create( 'transaction_timestamp', r.row['transaction']['timestamp']).run() # compound index to read transactions from the backlog per assignee r.db(db_name).table('backlog')\ .index_create('assignee__transaction_timestamp', [r.row['assignee'], r.row['transaction']['timestamp']])\ .run() def fin(): print('Deleting `{}` database'.format(db_name)) get_conn().repl() try: r.db_drop(db_name).run() except r.ReqlOpFailedError as e: if e.message != 'Database `{}` does not exist.'.format(db_name): raise print('Finished deleting `{}`'.format(db_name)) request.addfinalizer(fin)
def test_main(): """ Tests primary data acquisition API """ # Zero is the initial condition for main, where there are no existing timestamps to compare against out = main.main(0) pprint(out) assert out assert isinstance(out, dict) assert all(isinstance(out[elem], list) for elem in out) assert all(out[key] for key in out) must_fail = int(time.time()) failing = main.main(must_fail) assert not failing assert isinstance(failing, bool) r.db_drop(db_name).run(r.connect())