Skip to content

Commit abbfe91

Browse files
committed
add debug logging for SchemaEditor's create_collection() calls
1 parent 6166211 commit abbfe91

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

django_mongodb/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .operations import DatabaseOperations
1313
from .query_utils import regex_match
1414
from .schema import DatabaseSchemaEditor
15-
from .utils import CollectionDebugWrapper
15+
from .utils import OperationDebugWrapper
1616

1717

1818
class Cursor:
@@ -137,9 +137,14 @@ def __init__(self, *args, **kwargs):
137137
def get_collection(self, name, **kwargs):
138138
collection = Collection(self.database, name, **kwargs)
139139
if self.queries_logged:
140-
collection = CollectionDebugWrapper(collection, self)
140+
collection = OperationDebugWrapper(self, collection)
141141
return collection
142142

143+
def get_database(self):
144+
if self.queries_logged:
145+
return OperationDebugWrapper(self)
146+
return self.database
147+
143148
def __getattr__(self, attr):
144149
"""
145150
Connect to the database the first time `connection` or `database` are

django_mongodb/schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
77
def get_collection(self, name):
88
return self.connection.get_collection(name)
99

10+
def get_database(self):
11+
return self.connection.get_database()
12+
1013
@wrap_database_errors
1114
def create_model(self, model):
12-
self.connection.database.create_collection(model._meta.db_table)
15+
self.get_database().create_collection(model._meta.db_table)
1316
# Make implicit M2M tables.
1417
for field in model._meta.local_many_to_many:
1518
if field.remote_field.through._meta.auto_created:

django_mongodb/utils.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ def check_django_compatability():
2525
)
2626

2727

28-
class CollectionDebugWrapper:
29-
def __init__(self, collection, db):
28+
class OperationDebugWrapper:
29+
def __init__(self, db, collection=None):
3030
self.collection = collection
3131
self.db = db
3232

3333
def __getattr__(self, attr):
34-
return getattr(self.collection, attr)
34+
if self.collection is not None:
35+
return getattr(self.collection, attr)
36+
return getattr(self.db, attr)
3537

3638
def profile_call(self, func, args=(), kwargs=None):
3739
start = time.monotonic()
@@ -44,7 +46,8 @@ def log(self, op, duration, args, kwargs=None):
4446
# added to this logging.
4547
msg = "(%.3f) %s"
4648
args = ", ".join(str(arg) for arg in args)
47-
operation = f"{self.collection.name}.{op}({args})"
49+
collection_name = f"{self.collection.name}." if self.collection is not None else ""
50+
operation = f"db.{collection_name}{op}({args})"
4851
if len(settings.DATABASES) > 1:
4952
msg += f"; alias={self.db.alias}"
5053
self.db.queries_log.append(
@@ -66,7 +69,10 @@ def log(self, op, duration, args, kwargs=None):
6669

6770
def logging_wrapper(method):
6871
def wrapper(self, *args, **kwargs):
69-
func = getattr(self.collection, method)
72+
if self.collection is not None:
73+
func = getattr(self.collection, method)
74+
else:
75+
func = getattr(self.db.database, method)
7076
# Collection.insert_many() mutates args (the documents) by adding
7177
# _id. deepcopy() to avoid logging that version.
7278
original_args = copy.deepcopy(args)
@@ -78,6 +84,7 @@ def wrapper(self, *args, **kwargs):
7884

7985
# These are the operations that this backend uses.
8086
aggregate = logging_wrapper("aggregate")
87+
create_collection = logging_wrapper("create_collection")
8188
drop = logging_wrapper("drop")
8289
insert_many = logging_wrapper("insert_many")
8390
delete_many = logging_wrapper("delete_many")

0 commit comments

Comments
 (0)