-
-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement transaction savepoints #1816
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a13456
Implement savepoints for psycopg and asyncpg
henadzit 86fe862
Rename BaseTransactionWrapper.start to begin
henadzit 0569919
Implement savepoints for mysql
henadzit 2f9200d
Implement savepoints for MSSQL
henadzit 287b208
Temporary changes to make CI pass
henadzit 5f05900
Implement savepoints for Sqlite
henadzit 3aa4745
Use IsolatedTestCase for transaction tests
henadzit 4c6a85c
Improve test isolation
henadzit ae87a0a
Update docs
henadzit b9e764e
Update CHANGELOG and plan for 0.23.1 release
henadzit 944194b
CR fixes
henadzit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,31 @@ Transactions | |
Tortoise ORM provides a simple way to manage transactions. You can use the | ||
``atomic()`` decorator or ``in_transaction()`` context manager. | ||
|
||
``atomic()`` and ``in_transaction()`` can be nested, and the outermost block will | ||
be the one that actually commits the transaciton. Tortoise ORM doesn't support savepoints yet. | ||
``atomic()`` and ``in_transaction()`` can be nested. The inner blocks will create transaction savepoints, | ||
and if an exception is raised and then caught outside of a nested block, the transaction will be rolled back | ||
to the state before the block was entered. The outermost block will be the one that actually commits the transaction. | ||
The savepoints are supported for Postgres, SQLite, MySQL and SQLite. For other databases, it is advised to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 times for SQlite? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed :) |
||
propagate the exception to the outermost block to ensure that the transaction is rolled back. | ||
|
||
In most cases ``asyncio.gather`` or similar ways to spin up concurrent tasks can be used safely | ||
when querying the database or using transactions. Tortoise ORM will ensure that for the duration | ||
of a query, the database connection is used exclusively by the task that initiated the query. | ||
.. code-block:: python3 | ||
|
||
# this block will commit changes on exit | ||
async with in_transaction(): | ||
await MyModel.create(name='foo') | ||
try: | ||
# this block will create a savepoint and rollback to it if an exception is raised | ||
async with in_transaction(): | ||
await MyModel.create(name='bar') | ||
# this will rollback to the savepoint, meaning that | ||
# the 'bar' record will not be created, however, | ||
# the 'foo' record will be created | ||
raise Exception() | ||
except Exception: | ||
pass | ||
|
||
When using ``asyncio.gather`` or similar ways to spin up concurrent tasks in a transaction block, | ||
avoid having nested transaction blocks in the concurrent tasks. Transactions are stateful and nested | ||
blocks are expected to run sequentially, not concurrently. | ||
|
||
|
||
.. automodule:: tortoise.transactions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be 0.23.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.