Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit cb9ae6c

Browse files
porthuntalfinkel
authored andcommitted
142 Fixing database existance double check (#286)
* Fix exists() double call in client create_database method * Add throw_on_exists functionality to database create method * Add logic to database test validating new create functionality
1 parent da96255 commit cb9ae6c

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
2.5.0 (Unreleased)
22
==================
33
- [FIXED] Fixed ``TypeError`` when setting revision limits on Python>=3.6.
4+
- [FIXED] Fixed the ``exists()`` double check on ``client.py`` and ``database.py``.
45
- [FIXED] Fixed Cloudant exception code 409 with 412 when creating a database that already exists.
56

67
2.4.0 (2017-02-14)

src/cloudant/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
from ._2to3 import bytes_, unicode_
2424
from .database import CloudantDatabase, CouchDatabase
2525
from .feed import Feed, InfiniteFeed
26-
from .error import CloudantArgumentError, CloudantClientException
26+
from .error import (
27+
CloudantArgumentError,
28+
CloudantClientException,
29+
CloudantDatabaseException)
2730
from ._common_util import (
2831
USER_AGENT,
2932
append_response_error_content,
@@ -224,10 +227,11 @@ def create_database(self, dbname, **kwargs):
224227
:returns: The newly created database object
225228
"""
226229
new_db = self._DATABASE_CLASS(self, dbname)
227-
if new_db.exists():
228-
if kwargs.get('throw_on_exists', True):
230+
try:
231+
new_db.create(kwargs.get('throw_on_exists', False))
232+
except CloudantDatabaseException as ex:
233+
if ex.status_code == 412:
229234
raise CloudantClientException(412, dbname)
230-
new_db.create()
231235
super(CouchDB, self).__setitem__(dbname, new_db)
232236
return new_db
233237

src/cloudant/database.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,19 @@ def get_view_result(self, ddoc_id, view_name, raw_result=False, **kwargs):
329329
else:
330330
return view.result
331331

332-
def create(self):
332+
def create(self, throw_on_exists=False):
333333
"""
334334
Creates a database defined by the current database object, if it
335335
does not already exist and raises a CloudantException if the operation
336336
fails. If the database already exists then this method call is a no-op.
337337
338+
:param bool throw_on_exists: Boolean flag dictating whether or
339+
not to throw a CloudantDatabaseException when attempting to
340+
create a database that already exists.
341+
338342
:returns: The database object
339343
"""
340-
if self.exists():
344+
if not throw_on_exists and self.exists():
341345
return self
342346

343347
resp = self.r_session.put(self.database_url)

tests/unit/database_tests.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ def test_create_db_delete_db(self):
202202
# No issue should arise if attempting to create existing database
203203
db_2 = db.create()
204204
self.assertEqual(db, db_2)
205+
# If we use throw_on_exists=True, it will raise a
206+
# CloudantDatabaseException if the database already exists.
207+
with self.assertRaises(CloudantDatabaseException) as cm:
208+
db.create(throw_on_exists=True)
209+
self.assertEqual(cm.exception.status_code, 412)
205210
except Exception as err:
206211
self.fail('Exception {0} was raised.'.format(str(err)))
207212
finally:
@@ -690,7 +695,7 @@ def test_revisions_diff(self):
690695
)
691696
# Test differences
692697
self.assertEqual(
693-
self.db.revisions_diff('julia006', *revs),
698+
self.db.revisions_diff('julia006', *revs),
694699
{'julia006': {'missing': revs, 'possible_ancestors': [doc['_rev']]}}
695700
)
696701
# Test no differences
@@ -1150,7 +1155,7 @@ def test_create_json_index(self):
11501155
'lists': {},
11511156
'shows': {},
11521157
'language': 'query',
1153-
'views': {index.name: {'map': {'fields': {'name': 'asc',
1158+
'views': {index.name: {'map': {'fields': {'name': 'asc',
11541159
'age': 'asc'}},
11551160
'reduce': '_count',
11561161
'options': {'def': {'fields': ['name',
@@ -1241,7 +1246,7 @@ def test_create_multiple_indexes_one_ddoc(self):
12411246
'lists': {},
12421247
'shows': {},
12431248
'views': {'json-index-001': {
1244-
'map': {'fields': {'name': 'asc',
1249+
'map': {'fields': {'name': 'asc',
12451250
'age': 'asc'}},
12461251
'reduce': '_count',
12471252
'options': {'def': {'fields': ['name',
@@ -1258,7 +1263,7 @@ def test_create_multiple_indexes_one_ddoc(self):
12581263
'default': 'keyword',
12591264
'fields': {'$default': 'standard'}}}}}
12601265
)
1261-
1266+
12621267
def test_create_query_index_failure(self):
12631268
"""
12641269
Tests that a type of something other than 'json' or 'text' will cause

0 commit comments

Comments
 (0)