Example #1
0
 def _get_store_internal(self, dbname):
     from stoqlib.database.runtime import StoqlibStore
     uri = self._create_uri(dbname)
     try:
         self._log_connect(uri)
         store = StoqlibStore(create_database(uri))
     except OperationalError as e:
         log.info('OperationalError: %s' % e)
         raise DatabaseError(e.args[0])
     except Exception as e:
         value = sys.exc_info()[1]
         raise DatabaseError(
             _("Could not connect to %s database. The error message is "
               "'%s'. Please fix the connection settings you have set "
               "and try again.") % (DEFAULT_RDBMS, value))
     return store
Example #2
0
    def lock_database(self):
        """Tries to lock the database.

        Raises an DatabaseError if the locking has failed (ie, other clients are
        using the database).
        """
        try:
            # Locking requires a transaction to work, but this conection does
            # not begin one explicitly
            self.execute('BEGIN TRANSACTION')
            self.execute(self.get_lock_database_query())
        except OperationalError as e:
            raise DatabaseError("ERROR: Could not obtain lock: %s" % (e, ))
Example #3
0
 def _get_store_internal(self, dbname):
     from stoqlib.database.runtime import StoqlibStore
     uri = self._create_uri(dbname)
     try:
         if uri.host == "":
             pair = test_local_database()
             if pair is None:
                 raise DatabaseError(
                     _("Could not find a database server on this computer"))
             uri.host = pair[0]
             uri.port = int(pair[1])
         self._log_connect(uri)
         store = StoqlibStore(create_database(uri))
     except OperationalError as e:
         log.info('OperationalError: %s' % e)
         raise DatabaseError(e.args[0])
     except Exception as e:
         value = sys.exc_info()[1]
         raise DatabaseError(
             _("Could not connect to %s database. The error message is "
               "'%s'. Please fix the connection settings you have set "
               "and try again.") % (DEFAULT_RDBMS, value))
     return store
Example #4
0
 def has_database(self):
     """Checks if the database specified in the settings exists
     :returns: if the database exists
     """
     try:
         super_store = self.create_super_store()
     except OperationalError as e:
         msg = e.args[0]
         details = None
         if ';' in msg:
             msg, details = msg.split(';')
         msg = msg.replace('\n', '').strip()
         details = details.replace('\n', '').strip()
         raise DatabaseError('Database Error:\n%s' % msg, details)
     retval = _database_exists(super_store, self.dbname)
     super_store.close()
     return retval
Example #5
0
def get_database_version(store):
    """Gets the database version as a tuple

    :param store: a store
    :returns: the version as a 3 item tuple
    :raises: :exc:`stoqlib.exceptions.DatabaseError` if any error ocour
        in the process
    """
    full_version = store.execute('SELECT VERSION();').get_one()[0]
    server_version = full_version.split(' ')[1].split('.')

    try:
        version_tuple = (int(server_version[0]), int(server_version[1]),
                         int(server_version[2]))
    except ValueError:
        raise DatabaseError("Error getting server version: %s" %
                            (server_version, ))

    return version_tuple
Example #6
0
    def _create_uri(self, dbname=None):
        # postgres is a special database which is always present,
        # it was added in 8.1 which is thus our requirement'
        dbname = dbname or 'postgres'

        # Do not output the password in the logs
        if not self.first:
            log.info('connecting to %s' % self._build_dsn(
                dbname, filter_password=True))
            self.first = False

        dsn = self._build_dsn(dbname, filter_password=False)
        uri = URI(dsn)
        uri.options['isolation'] = 'read-committed'

        if uri.host == "":
            pair = test_local_database()
            if pair is None:
                raise DatabaseError(
                    _("Could not find a database server on this computer"))
            uri.host = pair[0]
            uri.port = int(pair[1])

        return uri