Example #1
0
def db_command(config, command, database='postgres'):
    '''Perform some sort of database-level command. Retry 10 times if we
    fail by conflicting with another user.

    Since PostgreSQL version 8.1 there is a database "postgres",
    before "template1" seems to habe been used, so we fall back to it. 
    Compare to issue2550543.
    '''
    template1 = connection_dict(config)
    template1['database'] = database

    try:
        conn = psycopg.connect(**template1)
    except psycopg.OperationalError as message:
        if str(message).find('database "postgres" does not exist') >= 0:
            return db_command(config, command, database='template1')
        raise hyperdb.DatabaseError(message)

    conn.set_isolation_level(0)
    cursor = conn.cursor()
    try:
        for n in range(10):
            if pg_command(cursor, command):
                return
    finally:
        conn.close()
    raise RuntimeError('10 attempts to create database failed')
Example #2
0
 def sql_open_connection(self):
     db = connection_dict(self.config, 'database')
     logging.getLogger('roundup.hyperdb').info('open database %r' %
                                               db['database'])
     try:
         conn = psycopg.connect(**db)
     except psycopg.OperationalError, message:
         raise hyperdb.DatabaseError(message)
Example #3
0
 def sql_open_connection(self):
     kwargs = connection_dict(self.config, 'db')
     self.log_info('open database %r'%(kwargs['db'],))
     try:
         conn = MySQLdb.connect(**kwargs)
     except MySQLdb.OperationalError as message:
         raise hyperdb.DatabaseError(message)
     cursor = conn.cursor()
     cursor.execute("SET AUTOCOMMIT=0")
     lvl = isolation_levels [self.config.RDBMS_ISOLATION_LEVEL]
     cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL %s" % lvl)
     cursor.execute("START TRANSACTION")
     return (conn, cursor)
Example #4
0
    def sql_open_connection(self):
        db = connection_dict(self.config, 'database')
        logging.getLogger('roundup.hyperdb').info(
            'open database %r'%db['database'])
        try:
            conn = psycopg.connect(**db)
        except psycopg.OperationalError as message:
            raise hyperdb.DatabaseError(message)

        cursor = conn.cursor()
        if ISOLATION_LEVEL_REPEATABLE_READ is not None:
            lvl = isolation_levels [self.config.RDBMS_ISOLATION_LEVEL]
            conn.set_isolation_level(lvl)

        return (conn, cursor)
Example #5
0
 def cache_db_type(self, path):
     ''' determine which DB wrote the class file, and cache it as an
         attribute of __class__ (to allow for subclassed DBs to be
         different sorts)
     '''
     db_type = ''
     if os.path.exists(path):
         db_type = whichdb(path)
         if not db_type:
             raise hyperdb.DatabaseError(
                 _("Couldn't identify database type"))
     elif os.path.exists(path + '.db'):
         # if the path ends in '.db', it's a dbm database, whether
         # anydbm says it's dbhash or not!
         db_type = 'dbm'
     self.__class__._db_type = db_type
Example #6
0
def db_command(config, command, database='postgres'):
    '''Perform some sort of database-level command. Retry 10 times if we
    fail by conflicting with another user.

    Since PostgreSQL version 8.1 there is a database "postgres",
    before "template1" seems to habe been used, so we fall back to it. 
    Compare to issue2550543.
    '''
    template1 = connection_dict(config)
    template1['database'] = database

    try:
        conn = psycopg.connect(**template1)
    except psycopg.OperationalError, message:
        if str(message).find('database "postgres" does not exist') >= 0:
            return db_command(config, command, database='template1')
        raise hyperdb.DatabaseError(message)
Example #7
0
    def open_connection(self):
        # make sure the database actually exists
        if not db_exists(self.config):
            db_create(self.config)

        self.conn, self.cursor = self.sql_open_connection()

        try:
            self.load_dbschema()
        except MySQLdb.OperationalError as message:
            if message.args[0] != ER.NO_DB_ERROR:
                raise
        except MySQLdb.ProgrammingError as message:
            if message.args[0] != ER.NO_SUCH_TABLE:
                raise hyperdb.DatabaseError(message)
            self.init_dbschema()
            self.sql("CREATE TABLE `schema` (`schema` TEXT) ENGINE=%s"%
                self.mysql_backend)
            self.sql('''CREATE TABLE ids (name VARCHAR(255),
                num INTEGER) ENGINE=%s'''%self.mysql_backend)
            self.sql('create index ids_name_idx on ids(name)')
            self.create_version_2_tables()