fix: Issues with DBConnection
class
#845
Merged
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.
Our custom
DBConnection
class can be used as a context manager like this:https://github.com/ankipalace/ankihub_addon/blob/b76d2118c8afee21ae6ad589846efbd5ac64e801/tests/addon/test_unit.py#L1131-L1133
(
ankihub_db.connection()
returns an instance ofDBConnection
)One could expect that if there was an exception in the context, the operations would be rolled back. This is how it works for
sqlite3.Connection
when it's used as a context manager. However this is currently not the case forDBConnection
. This will be fixed by this PR.For context: The intention behind supporting the usage of
DBConnection
as a context manager was mainly to improve performance. Instantiating aDBConnection
object requires opening a new database connection, which is relatively expensive. It also can't be re-used when not used as a context manager:https://github.com/ankipalace/ankihub_addon/blob/b76d2118c8afee21ae6ad589846efbd5ac64e801/tests/addon/test_unit.py#L1114-L1121
So using the
DBConnection
as a context manager was a way to improve performance when multiple queries need to be performed in a loop.Related issues
fix: Issues with DBConnection class
Proposed changes
DBConnection
to rollback changes when there is an exception in the contextDBConnection
class and its methodsDBConnection
class