~ubuntuone-control-tower/desktopcouch/1.0-stable

« back to all changes in this revision

Viewing changes to desktopcouch/records/database.py

Change creation of the Database object to EAFP. LBYL is impossible with HTTP and no locking. If create-flag is set, then go do it and swallow an exception about it already existing. (LP: #707321)

In addition, simplify that code so it makes fewer round-trips to the server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
# pylint: enable=F0401
44
44
 
45
45
from couchdb import Server
46
 
from couchdb.http import  ResourceNotFound, ResourceConflict
 
46
from couchdb.http import ResourceNotFound, ResourceConflict, PreconditionFailed
47
47
from couchdb.design import ViewDefinition
48
48
 
49
49
from desktopcouch.records import Record
134
134
        """Reconnect after losing connection."""
135
135
        self._server = self._server_class(uri or self.server_uri,
136
136
                **self._server_class_extras)
137
 
        if self._database_name not in self._server:
138
 
            if self._create:
 
137
        if self._create:
 
138
            try:
139
139
                self._server.create(self._database_name)
 
140
            except PreconditionFailed:
 
141
                pass  # Not an error to want it created and DB exists.
 
142
 
 
143
        try:
 
144
            if self.db is None:
 
145
                self.db = self._server[self._database_name]
140
146
            else:
141
 
                raise NoSuchDatabase(self._database_name)
142
 
        if self.db is None:
143
 
            self.db = self._server[self._database_name]
144
 
        else:
145
 
            # Monkey-patch the object the user already uses.  Oook!
146
 
            new_db = self._server[self._database_name]
147
 
            self.db.resource = new_db.resource
 
147
                # Monkey-patch the object the user already uses.  Oook!
 
148
                new_db = self._server[self._database_name]
 
149
                self.db.resource = new_db.resource
 
150
        except ResourceNotFound:
 
151
            raise NoSuchDatabase(self._database_name)
148
152
 
149
153
    def _temporary_query(self, map_fun, reduce_fun=None, language='javascript',
150
154
            wrapper=None, **options):