Ejemplo n.º 1
0
def db_ctx(cfgfn='rosshm.ini',
           cfginit=True,
           create=True,
           db_t=False,
           close=True,
           dbn='testing'):
    conn = None
    if dbn != 'testing':
        cfgfn = path.join(dbn, cfgfn)
    with config_ctx(fn=cfgfn, init=cfginit) as config:
        dbcfg = config.database()
        conn = db.connect(dbcfg)
        dbn = config.get('testing.dbn', dbn)
        if create:
            db.create(dbn, conn)
            if db_t:
                _t = DBTesting()
                _t.dbn = dbn
                _t.set(conn, value='testing', option='testing')
            conn.commit()
        try:
            yield conn
        finally:
            if close and conn is not None:
                conn.commit()
                conn.close()
                del conn
Ejemplo n.º 2
0
def test_connect(testing_config):
	with testing_config('mysql.ini') as config:
		conn = None
		dbcfg = config.database()
		assert dbcfg == {
			'config': '',
			'driver': 'mysql',
			'name': 'rosshmdb',
		}
		try:
			conn = db.connect(dbcfg)
		finally:
			if conn:
				conn.close()
		assert db.DatabaseError == MySQLdb.DatabaseError
		assert db.IntegrityError == MySQLdb.IntegrityError
		assert sql.lang.name == 'mysql'
		MySQLdb.connect.assert_called_once_with(
			charset='utf8',
			connect_timeout=60,
			cursorclass='TestingDictCursor',
			db='rosshmdb',
			host='localhost',
			passwd=None,
			use_unicode=True,
			user='******',
		)
Ejemplo n.º 3
0
Archivo: db.py Proyecto: jrmsgit/rosshm
		def wrapper(*args, **kwargs):
			log.debug(f"wrapper {ctx.name}")
			conn = db.connect(self.cfg)
			kwargs['db'] = conn
			try:
				resp = callback(*args, **kwargs)
				conn.commit()
			except db.IntegrityError as err:
				log.error(str(err))
				conn.rollback()
				raise bottle.HTTPError(500, "database error", err)
			finally:
				conn.close()
			return resp
Ejemplo n.º 4
0
def checkdb(config, conn=None):
    dbcfg = config.database()
    log.debug(f"checkdb {dbcfg['driver']} {dbcfg['name']} {dbcfg['config']}")
    if dbcfg.get('driver') == 'sqlite' and not dbcfg.get('name').startswith(
            ':memory:'):
        dbfn = dbcfg.get('name')
        if not path.isfile(dbfn):
            makedirs(path.dirname(dbfn), mode=0o750, exist_ok=True)
    rv = True
    try:
        if conn is None:
            conn = db.connect(dbcfg)
        rv = _check(conn)
    except db.DatabaseError as err:
        log.error(f"check database: {err}")
        return False
    finally:
        if conn is not None:
            conn.close()
    return rv
Ejemplo n.º 5
0
def _dbconn():
    """get database connection"""
    dbcfg = config.database()
    log.debug(f"dbcfg {dbcfg['driver']} {dbcfg['name']}")
    return db.connect(dbcfg)