Example #1
0
    def __init__(self, config):
        self.config = config
        print(self.config)

        self.name = self.config['name']
        self.id = self.config['id']
        self.db_name = self.config['db_name']

        self.rdb = r.connect(host=self.config['db_host'],
                             port=self.config['db_port'])

        try:
            r.db_create(self.db_name).run(self.rdb)
            r.db(self.db_name)\
                .table_create('incidents', primary_key='slack_channel')\
                .run(self.rdb)
            print('Database setup completed.')
        except RqlRuntimeError:
            print('App database already exists.')

        self.rdb.close()

        self.pool = ConnectionPool(host=self.config['db_host'],
                                   port=self.config['db_port'],
                                   db=self.db_name)
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        r.db_create(resultsdb).run(connection)
        
        r.db(resultsdb).table_create('results').run(connection)
        r.db(resultsdb).table_create('gpa').run(connection)
        r.db(resultsdb).table_create('students', primary_key='regno').run(connection)

        r.db(resultsdb).table('gpa').index_create(
            'semester_',
            [
                r.row["regno"],
                r.row["semester"]
            ]
            ).run(connection)
        r.db(resultsdb).table('results').index_create(
            'subject',
            [
                r.row["regno"],
                r.row["code"],
                r.row["subject_name"]
            ]
            ).run(connection)
        
        print ('Database setup completed. Now run the app without --setup: '
               '`python downloaddb.py`')
    except r.RqlRuntimeError, e:
        print e
Example #3
0
def tables_check(progress, conn, files_info, force):
    # Ensure that all needed databases exist and tables don't
    db_list = r.db_list().run(conn)
    for db in set([file_info["db"] for file_info in files_info]):
        if db == "rethinkdb":
            raise RuntimeError(
                "Error: Cannot import tables into the system database: 'rethinkdb'"
            )
        if db not in db_list:
            r.db_create(db).run(conn)

    # Ensure that all tables do not exist (unless --forced)
    already_exist = []
    for file_info in files_info:
        table = file_info["table"]
        db = file_info["db"]
        if table in r.db(db).table_list().run(conn):
            if not force:
                already_exist.append("%s.%s" % (db, table))

            extant_pkey = r.db(db).table(table).info().run(conn)["primary_key"]
            if file_info["info"]["primary_key"] != extant_pkey:
                raise RuntimeError(
                    "Error: Table '%s.%s' already exists with a different primary key"
                    % (db, table))

    return already_exist
Example #4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-r", "--rethinkdb-host", default="localhost:28015")
    parser.add_argument("-m", "--machine-id", default=socket.gethostname())
    args = parser.parse_args()
    host, port = args.rethinkdb_host.split(":")

    r.connect(host, port).repl()
    try:
        r.db("logcentral")
    except r.ReqlOpFailedError:
        r.db_create("logcentral").run()

    db = r.db("logcentral")

    if 'cursor_state' not in db.table_list().run():
        r.db("logcentral").table_create("cursor_state").run()

    if 'log' not in db.table_list().run():
        r.db("logcentral").table_create("log").run()

    cursor_table = r.db("logcentral").table('cursor_state')
    log_table = r.db("logcentral").table('log')

    c = cursor_table.get(args.machine_id).run()
    c = None if c is None else c['cursor']

    for line in yield_log_lines(c):
        cursor, data = prepare_for_table(line, args.machine_id)
        log_table.insert(data).run()
        cursor_table.insert({"id": args.machine_id, "cursor": cursor}, durability="soft", conflict="replace").run()
Example #5
0
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
Example #6
0
    def __init__(self, mountpoint):
        self.root = mountpoint
        self.connection = r.connect("localhost", 28015)
        if META_DB_NAME not in r.db_list().run(self.connection):
            r.db_create(META_DB_NAME).run(self.connection)

        self._create_tables()
Example #7
0
def create_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, True)
                return None

            if check_db(_conn, _name_db):
                return None

            if _expr:
                return r.db_create(_name_db)
            else:
                return r.db_create(_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
Example #8
0
def initialSetup():
    print "Setting up database..."
    dbs = rethinkdb.db_list().run()

    if not con.general.databases["rethink"]["db"] in dbs:
        print "Creating database in rethink"
        rethinkdb.db_create(con.general.databases["rethink"]["db"]).run()

    dbt = list(rethinkdb.table_list().run())
    for db in c.general.flush["rethink"]:
        if c.general.flush["rethink"][db]:
            print "Flushing rethink "+db+" table..."
            if db in dbt:
                rethinkdb.table_drop(db).run()
                dbt.pop(dbt.index(db))

    print "Creating new rethink tables..."
    for table in c.general.tables:
        if not table in dbt:
            print "Creating table {}".format(table)
            rethinkdb.table_create(table).run()

    for key in c.general.flush["redis"]:
        if c.general.flush["redis"][key]:
            print "Flushing redis "+key+" keys..."
            keys = con.redis.keys(key+":*")
            for key in keys: con.redis.delete(key)
def _initialise():
    log.debug('initializing rethinkdb for highstatestack data')

    try:
        conn = _connect()

        # Check if DB exists, if not create it
        db_exists = r.db_list().contains(DB).run(conn)
        if not db_exists:
            r.db_create(DB).run(conn)

        # Check if events table exist, if not create it
        table_exists = r.db(DB).table_list().contains(TABLE).run(conn)
        if not table_exists:
            result = r.db(DB).table_create(TABLE).run(conn)

        # Check if index exists if not add it
        rtable = r.db(DB).table(TABLE)
        current_indexes = rtable.index_list().run(conn)
        for index in INDEXES:
            if index not in current_indexes:
                log.debug('adding index {0}'.format(index))
                rtable.index_create(index).run(conn)

    except:
        log.error('could not connect to rehinkdb server on {0}:{1}'.format(DB, PORT))
        log.error(traceback.format_exc())
Example #10
0
def setup(db, host, port, tables, backup=None):
    '''Setup databases, tables, and indexes'''
    try:
        conn = r.connect(host=host,
                         port=port)  # Connect to the database server
    except:
        conn = r.connect(host=backup, port=port)

    try:
        r.db_create(db).run(conn)  # Create the database
    except:
        pass

    for table, indexes in tables.items(
    ):  # Seperate the tables and indexes and iterate over them
        try:
            r.db(db).table_create(table).run(conn)  # Try to create the table
        except:
            pass
        for index in indexes:  # Create the indexes for the specified table
            try:
                if type(index) is list:
                    r.db(db).table(table).index_create(
                        index[0], **index[1]).run(
                            conn)  # Create compound indexes if we need to
                else:
                    r.db(db).table(table).index_create(index).run(
                        conn)  # Create a normal index
            except:
                pass

    conn.close()
Example #11
0
def table_check(progress, conn, db, table, create_args, force):
    pkey = None

    if db == "rethinkdb":
        raise RuntimeError("Error: Cannot import a table into the system database: 'rethinkdb'")

    if db not in r.db_list().run(conn):
        r.db_create(db).run(conn)

    if table in r.db(db).table_list().run(conn):
        if not force:
            raise RuntimeError("Error: Table already exists, run with --force if you want to import into the existing table")

        if 'primary_key' in create_args:
            pkey = r.db(db).table(table).info()["primary_key"].run(conn)
            if create_args["primary_key"] != pkey:
                raise RuntimeError("Error: Table already exists with a different primary key")
    else:
        if 'primary_key' in create_args:
            pkey = create_args["primary_key"]
        else:
            if not options["quiet"]:
                print("no primary key specified, using default primary key when creating table")
        r.db(db).table_create(table, **create_args).run(conn)

    return pkey
Example #12
0
def dbSetup():
    connection = r.connect(host="localhost", port=28015)
    try:
        r.db_create(RCRDKEEPER_DB).run(connection)
        r.db(RCRDKEEPER_DB).table_create("records").run(connection)
        r.db(RCRDKEEPER_DB).table_create("users").run(connection)
        r.db(RCRDKEEPER_DB).table_create("record_condition").run(connection)
        r.db(RCRDKEEPER_DB).table_create("record_size").run(connection)
        r.db(RCRDKEEPER_DB).table_create("contact").run(connection)
        r.db("rcrdkeeper").table("record_condition").insert({"abbr": "M", "condition": "Mint", "order": 1}).run(
            connection
        )
        r.db("rcrdkeeper").table("record_condition").insert({"abbr": "VG", "condition": "Very Good", "order": 2}).run(
            connection
        )
        r.db("rcrdkeeper").table("record_condition").insert({"abbr": "G", "condition": "Good", "order": 3}).run(
            connection
        )
        r.db("rcrdkeeper").table("record_condition").insert({"abbr": "AV", "condition": "Average", "order": 4}).run(
            connection
        )
        r.db("rcrdkeeper").table("record_condition").insert({"abbr": "P", "condition": "Poor", "order": 5}).run(
            connection
        )
        r.db("rcrdkeeper").table("record_condition").insert({"abbr": "VP", "condition": "Very Poor", "order": 6}).run(
            connection
        )
        r.db("rcrdkeeper").table("record_size").insert({"size": "12 inch", "order": 1}).run(connection)
        r.db("rcrdkeeper").table("record_size").insert({"size": "10 inch", "order": 2}).run(connection)
        r.db("rcrdkeeper").table("record_size").insert({"size": "7 inch", "order": 3}).run(connection)
        print "Database setup completed. Now run the app without --setup."
    except RqlRuntimeError:
        print "App database already exists. Run the app without --setup."
    finally:
        connection.close()
Example #13
0
def rethink(request):
    """
    Set up a test database in Rethink and return a piper.db.rethink.RethinkDB
    instance.

    Tears down the database once done.

    """

    conn = rdb.connect(
        host=os.getenv('RETHINKDB_TEST_HOST', "localhost"),
        port=os.getenv('RETHINKDB_TEST_PORT', 28015),
    )

    db_name = 'piper_test_{0}'.format(str(time.time()).replace('.', '_'))
    rdb.db_create(db_name).run(conn)
    conn.use(db_name)

    rethink = RethinkDB()
    rethink.conn = conn  # Ugly faking to make the manager creation roll.
    rethink.create_tables(conn)

    def fin():
        rdb.db_drop(db_name).run(conn)

    request.addfinalizer(fin)

    return rethink
Example #14
0
def makeDB(host):
    conn = r.connect(host, 28015)
    dbs = r.db_list().run(conn)
    if AC in dbs:
        return 'already there'
    r.db_create(AC).run(conn)
    r.db(AC).table_create(WORDS, primary_key=LINE).run(conn)
    r.db(AC).table_create(PREFS, primary_key=PREF).run(conn)
    ra = {LINE: None, WORD: None, FREQ: None}
    f = open(os.path.join(SCRIPT_DIR, "wordsCSV.txt"), 'r')
    for line in f:
        line = line.strip()
        linesplit = line.split(',')
        ra[LINE] = int(linesplit[0])
        ra[WORD] = linesplit[1]
        ra[FREQ] = int(linesplit[2])
        if int(linesplit[0]) % 5000 == 0:
            print linesplit[0]
        r.db(AC).table(WORDS).insert(ra).run(conn)
    f.close()
    print "========================"
    g = open(os.path.join(SCRIPT_DIR, "rangesCSV.txt"), 'r')
    ra = {PREF: None, LOWER: None, UPPER: None}
    for line in g:
        line = line.strip()
        linesplit = line.split(',')
        ra[PREF] = linesplit[0]
        ra[LOWER] = int(linesplit[1])
        ra[UPPER] = int(linesplit[2])
        if len(linesplit[0]) == 1:
            print linesplit[0]

        r.db(AC).table(PREFS).insert(ra).run(conn)
    g.close()
    return 'initialized'
    def __init__(self, config):
        self.config = config
        print(self.config)

        self.name = self.config['name']
        self.id = self.config['id']
        self.db_name = self.config['db_name']

        self.rdb = r.connect(
            host=self.config['db_host'],
            port=self.config['db_port']
        )

        try:
            r.db_create(self.db_name).run(self.rdb)
            r.db(self.db_name)\
                .table_create('incidents', primary_key='slack_channel')\
                .run(self.rdb)
            print('Database setup completed.')
        except RqlRuntimeError:
            print('App database already exists.')

        self.rdb.close()

        self.pool = ConnectionPool(
            host=self.config['db_host'],
            port=self.config['db_port'],
            db=self.db_name
        )
def perform_job(fonts_dir, fonts_prefix):
    global db
    clone(REPO_URL, "clonedir", depth=MAX_NUM_ITERATIONS)
    os.chdir("clonedir")
    run(["git", "checkout", "master"])
    print("We're now at master branch.")

    lines = run(["git", "log", "--oneline", "."]).strip().split('\n')
    commits = ["master"] + [line.split()[0].strip() for line in lines]
    print("The commits we'll iterate over are: {}".format(commits))

    db_host = os.environ.get("RETHINKDB_DRIVER_SERVICE_HOST", 'db')
    r.connect(db_host, 28015).repl()
    try:
        r.db_create('fontbakery').run()
        r.db('fontbakery').table_create('fb_log').run()
        r.db('fontbakery').table_create('check_results').run()
        r.db('fontbakery').table_create('cached_stats').run()
    except:
        # OK, database and tables already exist.
        pass

    db = r.db('fontbakery')

    for i, commit in enumerate(commits):
        print("[{} of {}] Checking out commit '{}'".format(
            i + 1, len(commits), commit))
        run(["git", "checkout", commit])
        print("==> Running fontbakery on commit '{}'...".format(commit))
        run_fontbakery_on_commit(fonts_dir, fonts_prefix, commit, i)
Example #17
0
def init_database():
    try:
        connection = r.connect(host=RDB_HOST, port=RDB_PORT, db=RDB_DB)
    except RqlDriverError:
        debug("Could not connect to the database %s:%d, exiting..." % (RDB_HOST, RDB_PORT), True)
        exit(1)

    try:
        r.db_create("tomatoesfridge").run( connection )
    except r.errors.RqlRuntimeError as e:
        # We could parse the error... later...
        debug("Database `tomatoesfridge` not created. Reason:")
        debug(str(e))
    else:
        debug("Database `tomatoesfridge` created")

    try:
        r.table_create("movie").run( connection )
    except r.errors.RqlRuntimeError as e:
        # We could parse the error... later...
        debug("Table `movie` in `tomatoesfridge` not created. Reason")
        debug(str(e))
    else:
        debug("Table `movie` in `tomatoesfridge` created")

    try:
        connection.close()
    except AttributeError:
        debug("Could not close a connection", True)
        pass
def table_check(progress, conn, db, table, pkey, force):
    if db == "rethinkdb":
        raise RuntimeError(
            "Error: Cannot import a table into the system database: 'rethinkdb'"
        )

    if db not in r.db_list().run(conn):
        r.db_create(db).run(conn)

    if table in r.db(db).table_list().run(conn):
        if not force:
            raise RuntimeError(
                "Error: Table already exists, run with --force if you want to import into the existing table"
            )

        extant_pkey = r.db(db).table(table).info().run(conn)["primary_key"]
        if pkey is not None and pkey != extant_pkey:
            raise RuntimeError(
                "Error: Table already exists with a different primary key")
        pkey = extant_pkey
    else:
        if pkey is None:
            print(
                "no primary key specified, using default primary key when creating table"
            )
            r.db(db).table_create(table).run(conn)
        else:
            r.db(db).table_create(table, primary_key=pkey).run(conn)
    return pkey
def index():

    import rethinkdb as r
    r.connect('localhost', 28015).repl()

    try:
        r.db_create('wlps').run()
    except RqlRuntimeError:
        pass

    try:
        r.db('wlps').table_create('episode').run()
    except RqlRuntimeError:
        pass

    try:
        r.db('wlps').table_create('show').run()
    except RqlRuntimeError:
        pass

    try:
        r.db('wlps').table_create('notifications').run()
    except RqlRuntimeError:
        pass

    try:
        r.db('wlps').table_create('queue').run()
    except RqlRuntimeError:
        pass
	def setUp(self):
		
		self.db_name = 'radiowcs_test'
		assert self.db_name != 'radiowcs'
		self.table_name = 'test'

		self.db = database.Database()
		self.db.database_name = self.db_name
		self.db.table_name = self.table_name

		self.db.connect()

		self.connection = r.connect(
			host='localhost',
			port=28015,
			db=self.db_name,
			auth_key='',
			timeout=30
		)
		try:
			r.db_create(self.db_name).run(self.connection)
			r.table_create(self.table_name).run(self.connection)
		except r.RqlRuntimeError:
			print 'unittest setup: Drop table'
			r.table_drop(self.table_name).run(self.connection)
			r.table_create(self.table_name).run(self.connection)
		r.db(self.db_name).table(self.table_name).index_create( 'title').run(self.connection)
		r.db(self.db_name).table(self.table_name).index_create('artist').run(self.connection)
		r.db(self.db_name).table(self.table_name).index_create(  'date').run(self.connection)
		# 'out of order' insertions
		r.db(self.db_name).table(self.table_name).insert({'title':'foobar',      'artist': 'Selena',   'date': '1430183323'}).run(self.connection)
		r.db(self.db_name).table(self.table_name).insert({'title':'hello world', 'artist': 'John',     'date': '1430082566'}).run(self.connection)
		r.db(self.db_name).table(self.table_name).insert({'title':'zombie apoc', 'artist': 'xxJANExx', 'date': '1430385845'}).run(self.connection)
		r.db(self.db_name).table(self.table_name).insert({'title':'Black',       'artist': 'Kettle',   'date': '1430284300'}).run(self.connection)
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()
Example #22
0
 def init_db():
     'Set up the database'
     if 'ld33' not in r.db_list().run(rdb.conn):
         r.db_create('ld33').run(rdb.conn)
     if 'games' not in app.db.table_list().run(rdb.conn):
         app.db.table_create('games',
                             primary_key='container_id').run(rdb.conn)
def get_default_connection(request, url=None, **rethink_options):

    conn = getattr(request.registry, '_r_conn', None)

    if conn is not None:
        return conn

    if url is not None:
        rethink_options.pop('password', None)
        rethink_options.pop('user', None)
        rethink_options.pop('host', None)
        rethink_options.pop('port', None)
        rethink_options.pop('db', None)

        rethink_options.update(parse_url(url))

    LOG.debug(rethink_options)
    conn = r.connect(**rethink_options)

    if rethink_options.get('db', R_DB) not in r.db_list().run(conn):
        r.db_create(R_DB).run(conn)

    if R_TABLE not in r.table_list().run(conn):
        r.table_create(R_TABLE).run(conn)

    setattr(request.registry, '_r_conn', conn)

    return conn
Example #24
0
    def create_db(self):
        db_list = r.db_list().run()

        if self.db in db_list:
            pass
        else:
            r.db_create(self.db).run()
Example #25
0
 def setUp(self):
     self.servers = test_util.RethinkDBTestServers(4, server_build_dir=server_build_dir)
     self.servers.__enter__()
     self.port = self.servers.driver_port()
     conn = r.connect(port=self.port)
     if 'test' not in r.db_list().run(conn):
         r.db_create('test').run(conn)
Example #26
0
File: db.py Project: ajmal017/cs261
def create_tables():
    '''
    Create all tables
    '''
    # Postgres
    Base.metadata.create_all(engine)

    # Rethinkdb
    with get_reql_connection() as conn:
        try:
            # Database
            r.db_create(PURPLE_DB).run(conn)
            # Tables
            r.db(PURPLE_DB).table_create('alerts').run(
                conn)  # holds financial alerts
            r.db(PURPLE_DB).table_create('notifications').run(
                conn)  # holds realtime notifications
            r.db(PURPLE_DB).table_create('settings').run(
                conn)  # holds webapp settings
            r.db(PURPLE_DB).table_create('tasks').run(
                conn)  # holds current bg tasks
            # Create indices
            r.db(PURPLE_DB).table('tasks').index_create('pid').run(conn)
            r.db(PURPLE_DB).table('alerts').index_create('severity').run(conn)
            # default settings
            set_default_settings()
        except RqlRuntimeError:
            # Fail silently
            pass
        finally:
            print 'Rethinkdb setup complete.'
Example #27
0
def table_check(progress, conn, db, table, create_args, force):
    pkey = None

    if db == "rethinkdb":
        raise RuntimeError(
            "Error: Cannot import a table into the system database: 'rethinkdb'"
        )

    if db not in r.db_list().run(conn):
        r.db_create(db).run(conn)

    if table in r.db(db).table_list().run(conn):
        if not force:
            raise RuntimeError(
                "Error: Table already exists, run with --force if you want to import into the existing table"
            )

        if 'primary_key' in create_args:
            pkey = r.db(db).table(table).info()["primary_key"].run(conn)
            if create_args["primary_key"] != pkey:
                raise RuntimeError(
                    "Error: Table already exists with a different primary key")
    else:
        if 'primary_key' in create_args:
            pkey = create_args["primary_key"]
        else:
            print(
                "no primary key specified, using default primary key when creating table"
            )
        r.db(db).table_create(table, **create_args).run(conn)

    return pkey
Example #28
0
def rethinkdb():
    """Prepare database and table in RethinkDB"""
    from rethinkdb.errors import ReqlOpFailedError, ReqlRuntimeError
    conn = r.connect(host=conf.RethinkDBConf.HOST)

    # Create database
    try:
        r.db_create(conf.RethinkDBConf.DB).run(conn)
        click.secho('Created database {}'.format(conf.RethinkDBConf.DB),
                    fg='yellow')
    except ReqlOpFailedError:
        click.secho('Database {} already exists'.format(conf.RethinkDBConf.DB),
                    fg='green')

    # Create table 'domains'
    conn = r.connect(host=conf.RethinkDBConf.HOST,
                     db=conf.RethinkDBConf.DB)
    try:
        r.table_create('domains', durability=conf.RethinkDBConf.DURABILITY).\
            run(conn)
        click.secho('Created table domains', fg='yellow')
    except ReqlOpFailedError:
        click.secho('Table domains already exists', fg='green')
    
    # Create index on domains.name
    try:
        r.table('domains').index_create('name').run(conn)
        click.secho('Created index domains.name', fg='yellow')
    except ReqlRuntimeError:
        click.secho('Index domains.name already exists', fg='green')
Example #29
0
 def init_db():
     'Set up the database'
     if 'ld33' not in r.db_list().run(rdb.conn):
         r.db_create('ld33').run(rdb.conn)
     if 'games' not in app.db.table_list().run(rdb.conn):
         app.db.table_create('games',
                             primary_key='container_id').run(rdb.conn)
Example #30
0
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()
Example #31
0
 def initialize_db(self):
     try:
         r.db_create('mytimetable').run(self.connection)
         r.db('mytimetable').table_create('users').run(self.connection)
         logger.info('DB was successfully configured')
     except r.errors.ReqlOpFailedError:
         logger.info('DB was configured already')
Example #32
0
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)))
Example #33
0
    def save(self):
        try:
            r.db_create(self.db).run(self.bigchain.conn)
        except r.ReqlOpFailedError:
            pass

        try:
            r.db(self.db).table_create('accounts').run(self.bigchain.conn)
        except r.ReqlOpFailedError:
            pass

        user_exists = list(r.db(self.db)
                           .table('accounts')
                           .filter(lambda user: (user['name'] == self.name)
                                                & (user['ledger']['id'] == self.ledger['id']))
                           .run(self.bigchain.conn))
        if not len(user_exists):
            r.db(self.db)\
                .table('accounts')\
                .insert(self.as_dict(), durability='hard')\
                .run(self.bigchain.conn)
        else:
            user_persistent = user_exists[0]
            self.vk = user_persistent['vk']
            self.sk = user_persistent['sk']
Example #34
0
def create_database(conn, dbname):
    if r.db_list().contains(dbname).run(conn):
        raise exceptions.DatabaseAlreadyExists(
            'Database `{}` already exists'.format(dbname))

    logger.info('Create database `%s`.', dbname)
    r.db_create(dbname).run(conn)
Example #35
0
 def rethink(self):
     print('Attempting to connect to RethinkDB')
     tables = ['settings', 'numbers']
     dbc = self.config.get('db')
     try:
         self.conn = r.connect(host=dbc['host'],
                               port=dbc['port'],
                               db=dbc['db'],
                               user=dbc['username'],
                               password=dbc['password'])
         dbl = r.db_list().run(self.conn)
         if dbc['db'] not in dbl:
             print('Creating DB...')
             r.db_create(dbc['db']).run(self.conn)
         tab = r.table_list().run(self.conn)
         for i in tables:
             if i not in tab:
                 print(f'Table {i} not found. Now creating...')
                 r.table_create(i).run(self.conn)
     except Exception as e:
         print(
             f'DB connection failed! Exiting...\nError details:\n{type(e).__name__}: {e}'
         )
         sys.exit(1)
     print('Connected successfully.')
Example #36
0
    def init_rethinkdb(self):
        print('Now initialising RethinkDB...')
        dbc = self.config['RETHINKDB']

        try:
            self.conn = r.connect(host=dbc['HOST'],
                                  port=dbc['PORT'],
                                  db=dbc['DB'],
                                  user=dbc['USERNAME'],
                                  password=dbc['PASSWORD'])

            dbs = r.db_list().run(self.conn)
            if self.rdb not in dbs:
                print('Database not present. Creating...')
                r.db_create(self.rdb).run(self.conn)
            tables = r.db(self.rdb).table_list().run(self.conn)
            for i in self.rtables:
                if i not in tables:
                    print(f'Table {i} not found. Creating...')
                    r.table_create(i).run(self.conn)

        except Exception as e:
            print('RethinkDB init error!\n{}: {}'.format(type(e).__name__, e))
            sys.exit(1)
        print('RethinkDB initialisation successful.')
Example #37
0
    def __init__(self,
                 database='apscheduler',
                 table='jobs',
                 client=None,
                 pickle_protocol=pickle.HIGHEST_PROTOCOL,
                 **connect_args):
        super(RethinkDBJobStore, self).__init__()
        self.pickle_protocol = pickle_protocol

        if not database:
            raise ValueError('The "database" parameter must not be empty')
        if not table:
            raise ValueError('The "table" parameter must not be empty')

        if client:
            self.conn = maybe_ref(client)
        else:
            self.conn = r.connect(db=database, **connect_args)

        if database not in r.db_list().run(self.conn):
            r.db_create(database).run(self.conn)

        if table not in r.table_list().run(self.conn):
            r.table_create(table).run(self.conn)

        if 'next_run_time' not in r.table(table).index_list().run(self.conn):
            r.table(table).index_create('next_run_time').run(self.conn)

        self.table = r.db(database).table(table)
Example #38
0
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
Example #39
0
 def setUp(self):
     self.servers = test_util.RethinkDBTestServers(
         4, server_build_dir=server_build_dir)
     self.servers.__enter__()
     self.port = self.servers.driver_port()
     conn = r.connect(port=self.port)
     r.db_create('test').run(conn)
Example #40
0
def knn_setup(max_tries=-1):
    isSetup = False
    if max_tries > 0:
        n = 0
    else:
        n = max_tries - 1
    while (not isSetup and n < max_tries):
        connection = False

        try:
            connection = rdb.connect(RDB_HOST, RDB_PORT)

            if not rdb.db_list().contains(RDB_DB).run(connection):
                rdb.db_create(RDB_DB).run(connection)
            if not rdb.db(RDB_DB).table_list().contains(
                    RDB_JOB_OPTIMIZED_TABLE).run(connection):
                rdb.db(RDB_DB).table_create(RDB_JOB_OPTIMIZED_TABLE).run(
                    connection)

            isSetup = update_knn(connection)
        except Exception as e:
            print 'DB error:', e
            if connection:
                connection.close()
            time.sleep(5)
            if max_tries > 0:
                n = n + 1

    if not isSetup:
        # if still not setup: use data file
        print "# No DB connection: Using backup file"
        isSetup = update_knn(None)

    return isSetup
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()
Example #42
0
def create_db(name):
    try:
        r.db_create(name).run(conn)
        return name
    except Exception as e:
        return name
        pass
Example #43
0
def create_tables():

    connection = r.connect(host=DB_HOST, port=DB_PORT)
    if DB_NAME == "EDIT_ME":
        print("You haven't edited DB_NAME. Go to base.py and check Database Settings")
        os.exit(0)
    try:
        print("Attempting to create database %s" % DB_NAME)
        r.db_create(DB_NAME).run(connection)
        print("Database Created.")
    except errors.RqlRuntimeError:
        print("Database already exists")
    db = r.db(DB_NAME)
    for table in tables:
        try:
            print("Creating table %s" % table)
            db.table_create(table).run(connection)
        except errors.RqlRuntimeError:
            print("Table already Exists")
        if tables[table]:
            for index in tables[table]:
                try:
                    print("Creating index %s on table %s" % (index, table))
                    db.table(table).index_create(index).run(connection)
                except errors.RqlRuntimeError:
                    print("Index already created.")
    print("All done. Follow further instructions to complete the setup.")
Example #44
0
def create_db(db_name):
    try:
        r.db_create(db_name).run()
    except RqlRuntimeError as rql:
        print 'Failed to create the database ', rql
    except Exception as ex:
        print 'Exception occurred ', ex
Example #45
0
    def create(self):
        conn = self.connect()

        db_list = r.db_list().run(conn)

        db_created = False
        table_created = False

        if not self.db_name in db_list:
            r.db_create(self.db_name).run(conn)
            db_created = True

        table_list = r.db(self.db_name).table_list().run(conn)

        if not self.config_table_name in table_list:
            r.db(self.db_name).table_create(
                self.config_table_name, primary_key=self.primary_key
            ).run(conn)

            r.db(self.db_name).table(self.config_table_name)\
                .index_create(self.secondary_index).run(conn)

            table_created = True

        return {"db": db_created, "table": table_created}
Example #46
0
def create_database():
    r.connect("localhost", 28015).repl()
    if (r.db_list().contains(database_name).run()):
        pass
    else:
        r.db_create(database_name).run()
        create_tables()
Example #47
0
    def __init__(self, database='apscheduler', table='jobs', client=None,
                 pickle_protocol=pickle.HIGHEST_PROTOCOL, **connect_args):
        super(RethinkDBJobStore, self).__init__()
        self.pickle_protocol = pickle_protocol

        if not database:
            raise ValueError('The "database" parameter must not be empty')
        if not table:
            raise ValueError('The "table" parameter must not be empty')

        if client:
            self.conn = maybe_ref(client)
        else:
            self.conn = r.connect(db=database, **connect_args)

        if database not in r.db_list().run(self.conn):
            r.db_create(database).run(self.conn)

        if table not in r.table_list().run(self.conn):
            r.table_create(table).run(self.conn)

        if 'next_run_time' not in r.table(table).index_list().run(self.conn):
            r.table(table).index_create('next_run_time').run(self.conn)

        self.table = r.db(database).table(table)
Example #48
0
def setup():
    tables = [
        {
            'name' : 'testbeds',
            'pkey' : 'id'
        },
        {
            'name' : 'resources',
            'pkey' : 'hostname'
        }
    ]

    c = connect()

    try:
        r.db_create(Config.rethinkdb["db"]).run(c)
        logger.info('MyOps2 database created successfully')
    except RqlRuntimeError:
        logger.info('MyOps2 database already exists')

    for t in tables:
        try:
            r.db(Config.rethinkdb["db"]).table_create(t['name'], primary_key=t['pkey']).run(c)
            logger.info('MyOps2 table %s setup completed', t['name'])
        except RqlRuntimeError:
            logger.info('MyOps2 table %s already exists', t['name'])

    c.close()
def table_creation(conn,db):
    r.db_create(db).run(conn)
    r.db(db).table_create('questions', primary_key='num').run(conn)
    r.db(db).table_create('clientpagestatus').run(conn)
    r.db(db).table('clientpagestatus').insert({'pageStatus':'hide'}).run(conn)
    r.db(db).table_create('team_details', primary_key='teamname').run(conn)
    r.db(db).table_create('team_answer', primary_key='question_num').run(conn)
def create_db(app):
    db_name = app.config.get("RETHINKDB_DB")

    conn = app.rethinkdb.conn
    try:
        r.db_create(db_name).run(conn)
    except r.errors.ReqlOpFailedError:
        pass
Example #51
0
def connect_and_create():
    r.connect('rtdb.goodes.net').repl()
    if 'fb4s' not in r.db_list().run():
        r.db_create('fb4s').run()
    db = r.db('fb4s')
    if 'students' not in db.table_list().run():
        db.table_create('students').run()
    return db.table('students')
Example #52
0
 def __init__(self):
     conn = r.connect(RDB_HOST, RDB_PORT)
     try:
         r.db_create(RDB_DBNAME).run(conn)
         r.db(RDB_DBNAME).table_create("devices").run(conn)
         print("App database created")
     except (RqlRuntimeError, RqlDriverError):
         print("App already exists")
Example #53
0
def setup_db(table):
    """to setup a DB"""
    try:
        r.db_create(DB).run(g.db)
        r.db(DB).table_create(table).run(g.db)
        print('Using newly created database.')
    except RqlRuntimeError:
        print('Using existing database.')
Example #54
0
def setUp():
    conn = r.connect(db=DB, host=HOST, port=PORT)
    r.db_create(DB).run(conn)
    r.db(DB).table_create("users").run(conn)
    r.db(DB).table_create("groups").run(conn)
    r.db(DB).table_create("user_sessions").run(conn)
    r.db(DB).table_create("group_states").run(conn)
    conn.close()
Example #55
0
File: db.py Project: andyzg/journal
def ensure_db_exists(db_name, *args, **kwargs):
    """Creates a DB if it doesn't exist."""
    conn = r.connect()

    try:
        r.db_create(db_name, *args, **kwargs).run(conn)
    except r.RqlRuntimeError:
        pass  # Ignore DB already created
Example #56
0
def create_db_and_table():
    try:
        r.db_create('ep16').run(conn)
    except ReqlOpFailedError:
        pass
    try:
        r.db('ep16').table_create('scores').run(conn)
    except ReqlOpFailedError:
        pass
Example #57
0
def setup_db():
    """
    Set up the database.
    Include a sequence to make sure databases and tables exist where they
    need to be.
    """

    db_conn = r.connect(config['rdb_host'], config['rdb_port'])

    # add all setup needed here:
    if config['rdb_db'] not in r.db_list().run(db_conn):
        r.db_create(config['rdb_db']).run(db_conn)

    from models.user import User
    from models.notice import Notice
    from models.topic import Topic
    from models.post import Post
    from models.proposal import Proposal
    from models.vote import Vote
    from models.card import Card
    from models.unit import Unit
    from models.set import Set
    from models.card_parameters import CardParameters
    from models.unit_parameters import UnitParameters
    from models.set_parameters import SetParameters
    from models.follow import Follow
    from models.user_sets import UserSets
    from models.response import Response

    models = (User, Notice, Topic, Post, Proposal, Vote,
              Card, Unit, Set,
              CardParameters, UnitParameters, SetParameters,
              Follow, UserSets, Response)

    tables = r.db(config['rdb_db']).table_list().run(db_conn)

    for model_cls in models:
        tablename = getattr(model_cls, 'tablename', None)
        if tablename and tablename not in tables:
            (r.db(config['rdb_db'])
              .table_create(tablename)
              .run(db_conn))
            tables.append(tablename)

        existant_indexes = (r.db(config['rdb_db'])
                             .table(tablename)
                             .index_list()
                             .run(db_conn))
        indexes = getattr(model_cls, 'indexes', [])
        for index in indexes:
            if index[0] not in existant_indexes:
                (r.db(config['rdb_db'])
                  .index_create(*index)
                  .run(db_conn))

    db_conn.close()
Example #58
0
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        r.db_create(LOCATION_DB).run(connection)
        r.db(LOCATION_DB).table_create('locations').run(connection)
        print 'Database setup completed. Now run the app without --setup.'
    except RqlRuntimeError:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()