Beispiel #1
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
Beispiel #2
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
Beispiel #3
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