Skip to content

Commit 02ff29c

Browse files
committed
pass session to queries
1 parent 9cbd9ed commit 02ff29c

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

django_mongodb_backend/base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ def _commit(self):
197197
self.session = None
198198

199199
def _rollback(self):
200-
pass
200+
if self.session:
201+
self.session.abort_transaction()
202+
self.session = None
201203

202204
def _start_session(self):
203205
if self.session is None:
@@ -210,9 +212,6 @@ def _start_transaction_under_autocommit(self):
210212
def _set_autocommit(self, autocommit, force_begin_transaction_with_broken_autocommit=False):
211213
if not autocommit:
212214
self._start_session()
213-
else:
214-
if self.session:
215-
self.commit()
216215

217216
def _close(self):
218217
# Normally called by close(), this method is also called by some tests.

django_mongodb_backend/compiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,9 @@ def execute_sql(self, result_type):
770770

771771
@wrap_database_errors
772772
def update(self, criteria, pipeline):
773-
return self.collection.update_many(criteria, pipeline).matched_count
773+
return self.collection.update_many(
774+
criteria, pipeline, session=self.connection.session
775+
).matched_count
774776

775777
def check_query(self):
776778
super().check_query()

django_mongodb_backend/features.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8484
# Value.as_mql() doesn't call output_field.get_db_prep_save():
8585
# https://github.com/mongodb/django-mongodb-backend/issues/282
8686
"model_fields.test_jsonfield.TestSaveLoad.test_bulk_update_custom_get_prep_value",
87+
# to debug
88+
"transactions.tests.AtomicMiscTests.test_mark_for_rollback_on_error_in_transaction",
8789
}
8890
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
8991
_django_test_expected_failures_bitwise = {
@@ -505,16 +507,11 @@ def django_test_expected_failures(self):
505507
"Connection health checks not implemented.": {
506508
"backends.base.test_base.ConnectionHealthChecksTests",
507509
},
508-
"transaction.atomic() is not supported.": {
509-
"backends.base.test_base.DatabaseWrapperLoggingTests",
510-
"migrations.test_executor.ExecutorTests.test_atomic_operation_in_non_atomic_migration",
511-
"migrations.test_operations.OperationTests.test_run_python_atomic",
512-
},
513-
"transaction.rollback() is not supported.": {
514-
"transactions.tests.AtomicMiscTests.test_mark_for_rollback_on_error_in_autocommit",
515-
"transactions.tests.AtomicMiscTests.test_mark_for_rollback_on_error_in_transaction",
516-
"transactions.tests.NonAutocommitTests.test_orm_query_after_error_and_rollback",
517-
},
510+
# "transaction.atomic() is not supported.": {
511+
# "backends.base.test_base.DatabaseWrapperLoggingTests",
512+
# "migrations.test_executor.ExecutorTests.test_atomic_operation_in_non_atomic_migration",
513+
# "migrations.test_operations.OperationTests.test_run_python_atomic",
514+
# },
518515
"migrate --fake-initial is not supported.": {
519516
"migrations.test_commands.MigrateTests.test_migrate_fake_initial",
520517
"migrations.test_commands.MigrateTests.test_migrate_fake_split_initial",

django_mongodb_backend/query.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,19 @@ def delete(self):
6363
"""Execute a delete query."""
6464
if self.compiler.subqueries:
6565
raise NotSupportedError("Cannot use QuerySet.delete() when a subquery is required.")
66-
return self.compiler.collection.delete_many(self.match_mql).deleted_count
66+
return self.compiler.collection.delete_many(
67+
self.match_mql, session=self.compiler.connection.session
68+
).deleted_count
6769

6870
@wrap_database_errors
6971
def get_cursor(self):
7072
"""
7173
Return a pymongo CommandCursor that can be iterated on to give the
7274
results of the query.
7375
"""
74-
return self.compiler.collection.aggregate(self.get_pipeline())
76+
return self.compiler.collection.aggregate(
77+
self.get_pipeline(), session=self.compiler.connection.session
78+
)
7579

7680
def get_pipeline(self):
7781
pipeline = []

django_mongodb_backend/queryset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, pipeline, using, model):
3535
def _execute_query(self):
3636
connection = connections[self.using]
3737
collection = connection.get_collection(self.model._meta.db_table)
38-
self.cursor = collection.aggregate(self.pipeline)
38+
self.cursor = collection.aggregate(self.pipeline, session=connection.session)
3939

4040
def __str__(self):
4141
return str(self.pipeline)

tests/raw_query_/test_raw_aggregate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def test_different_db_key_order(self):
182182
{
183183
field.name: getattr(author, field.name)
184184
for field in reversed(Author._meta.concrete_fields)
185-
}
185+
},
186+
session=connection.session,
186187
)
187188
query = []
188189
authors = Author.objects.all()

0 commit comments

Comments
 (0)