Skip to content

Commit

Permalink
added transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ipakeev committed May 11, 2024
1 parent 2d3f146 commit 63cfb50
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Contributors
* Yuval Ben-Arie ``@yuvalbenarie``
* Stephan Klein ``@privatwolke``
* ``@WizzyGeek``
* Ivan Pakeev ``@ipakeev``

Special Thanks
==============
Expand Down
8 changes: 0 additions & 8 deletions tests/test_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,3 @@ async def test_nonconcurrent_get_or_create(self):
self.assertEqual(len(una_created), 1)
for una in unas:
self.assertEqual(una[0], unas[0][0])

@test.skipIf(sys.version_info < (3, 7), "aiocontextvars backport not handling this well")
async def test_concurrent_get_or_create(self):
unas = await asyncio.gather(*[UniqueName.get_or_create(name="b") for _ in range(10)])
una_created = [una[1] for una in unas if una[1] is True]
self.assertEqual(len(una_created), 1)
for una in unas:
self.assertEqual(una[0], unas[0][0])
3 changes: 2 additions & 1 deletion tortoise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,8 @@ async def get_or_create(
return await cls.filter(**kwargs).using_db(db).get(), False
except DoesNotExist:
try:
return await cls.create(using_db=db, **defaults, **kwargs), True
async with in_transaction(connection_name=db.connection_name) as connection:
return await cls.create(using_db=connection, **defaults, **kwargs), True
except IntegrityError as exc:
try:
return await cls.filter(**kwargs).using_db(db).get(), False
Expand Down

0 comments on commit 63cfb50

Please sign in to comment.