Skip to content

Commit 6166211

Browse files
committed
add debug logging for SchemaEditor's collection operations
1 parent 5c29684 commit 6166211

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

django_mongodb/schema.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55

66
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
7+
def get_collection(self, name):
8+
return self.connection.get_collection(name)
9+
710
@wrap_database_errors
811
def create_model(self, model):
912
self.connection.database.create_collection(model._meta.db_table)
@@ -17,7 +20,7 @@ def delete_model(self, model):
1720
for field in model._meta.local_many_to_many:
1821
if field.remote_field.through._meta.auto_created:
1922
self.delete_model(field.remote_field.through)
20-
self.connection.database[model._meta.db_table].drop()
23+
self.get_collection(model._meta.db_table).drop()
2124

2225
def add_field(self, model, field):
2326
# Create implicit M2M tables.
@@ -26,7 +29,7 @@ def add_field(self, model, field):
2629
return
2730
# Set default value on existing documents.
2831
if column := field.column:
29-
self.connection.database[model._meta.db_table].update_many(
32+
self.get_collection(model._meta.db_table).update_many(
3033
{}, [{"$set": {column: self.effective_default(field)}}]
3134
)
3235

@@ -41,7 +44,7 @@ def _alter_field(
4144
new_db_params,
4245
strict=False,
4346
):
44-
collection = self.connection.database[model._meta.db_table]
47+
collection = self.get_collection(model._meta.db_table)
4548
# Have they renamed the column?
4649
if old_field.column != new_field.column:
4750
collection.update_many({}, {"$rename": {old_field.column: new_field.column}})
@@ -59,7 +62,7 @@ def remove_field(self, model, field):
5962
return
6063
# Unset field on existing documents.
6164
if column := field.column:
62-
self.connection.database[model._meta.db_table].update_many({}, {"$unset": {column: ""}})
65+
self.get_collection(model._meta.db_table).update_many({}, {"$unset": {column: ""}})
6366

6467
def alter_index_together(self, model, old_index_together, new_index_together):
6568
pass
@@ -85,4 +88,4 @@ def remove_constraint(self, model, constraint):
8588
def alter_db_table(self, model, old_db_table, new_db_table):
8689
if old_db_table == new_db_table:
8790
return
88-
self.connection.database[old_db_table].rename(new_db_table)
91+
self.get_collection(old_db_table).rename(new_db_table)

django_mongodb/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ def wrapper(self, *args, **kwargs):
7878

7979
# These are the operations that this backend uses.
8080
aggregate = logging_wrapper("aggregate")
81+
drop = logging_wrapper("drop")
8182
insert_many = logging_wrapper("insert_many")
8283
delete_many = logging_wrapper("delete_many")
84+
rename = logging_wrapper("rename")
8385
update_many = logging_wrapper("update_many")
8486

8587
del logging_wrapper

0 commit comments

Comments
 (0)