Skip to content

Commit bc36d5d

Browse files
committed
add support for sqlmigrate
1 parent 15b71a5 commit bc36d5d

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,6 @@ def django_test_expected_failures(self):
670670
"migrations.test_commands.MigrateTests.test_migrate_fake_split_initial",
671671
"migrations.test_executor.ExecutorTests.test_soft_apply",
672672
},
673-
"SchemaEditor doesn't log or collect queries.": {
674-
# https://github.com/mongodb-labs/django-mongodb/issues/141
675-
"migrations.test_commands.MigrateTests.test_sqlmigrate_backwards",
676-
"migrations.test_commands.MigrateTests.test_sqlmigrate_for_non_atomic_migration",
677-
"migrations.test_commands.MigrateTests.test_sqlmigrate_for_non_transactional_databases",
678-
"migrations.test_commands.MigrateTests.test_sqlmigrate_forwards",
679-
"migrations.test_commands.MigrateTests.test_sqlmigrate_replaced_migration",
680-
"migrations.test_commands.MigrateTests.test_sqlmigrate_squashed_migration",
681-
},
682673
}
683674

684675
@cached_property

django_mongodb/schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
22

33
from .query import wrap_database_errors
4+
from .utils import OperationCollector
45

56

67
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
78
def get_collection(self, name):
9+
if self.collect_sql:
10+
return OperationCollector(self.collected_sql, collection=self.connection.database[name])
811
return self.connection.get_collection(name)
912

1013
def get_database(self):
14+
if self.collect_sql:
15+
return OperationCollector(self.collected_sql, db=self.connection.database)
1116
return self.connection.get_database()
1217

1318
@wrap_database_errors

django_mongodb/utils.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def check_django_compatability():
2626

2727

2828
class OperationDebugWrapper:
29+
# The PyMongo database and collection methods that this backend uses.
30+
wrapped_methods = {
31+
"aggregate",
32+
"create_collection",
33+
"drop",
34+
"insert_many",
35+
"delete_many",
36+
"rename",
37+
"update_many",
38+
}
39+
2940
def __init__(self, db, collection=None):
3041
self.collection = collection
3142
self.db = db
@@ -79,13 +90,33 @@ def wrapper(self, *args, **kwargs):
7990

8091
return wrapper
8192

82-
# These are the operations that this backend uses.
83-
aggregate = logging_wrapper("aggregate")
84-
create_collection = logging_wrapper("create_collection")
85-
drop = logging_wrapper("drop")
86-
insert_many = logging_wrapper("insert_many")
87-
delete_many = logging_wrapper("delete_many")
88-
rename = logging_wrapper("rename")
89-
update_many = logging_wrapper("update_many")
9093

91-
del logging_wrapper
94+
def set_wrapped_methods(cls):
95+
"""Initialize the wrapped methods on cls."""
96+
if hasattr(cls, "logging_wrapper"):
97+
for attr in cls.wrapped_methods:
98+
setattr(cls, attr, cls.logging_wrapper(attr))
99+
del cls.logging_wrapper
100+
101+
102+
set_wrapped_methods(OperationDebugWrapper)
103+
104+
105+
class OperationCollector(OperationDebugWrapper):
106+
def __init__(self, collected_sql=None, *, collection=None, db=None):
107+
super().__init__(db, collection)
108+
self.collected_sql = collected_sql
109+
110+
def log(self, op, args, kwargs=None):
111+
args = ", ".join(repr(arg) for arg in args)
112+
operation = f"db.{self.collection_name}{op}({args})"
113+
self.collected_sql.append(operation)
114+
115+
def logging_wrapper(method):
116+
def wrapper(self, *args, **kwargs):
117+
self.log(method, args, kwargs)
118+
119+
return wrapper
120+
121+
122+
set_wrapped_methods(OperationCollector)

0 commit comments

Comments
 (0)