Skip to content

Commit 2fc55eb

Browse files
committed
add support for sqlmigrate
1 parent b5b5b46 commit 2fc55eb

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-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: 41 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
@@ -82,13 +93,34 @@ def wrapper(self, *args, **kwargs):
8293

8394
return wrapper
8495

85-
# These are the operations that this backend uses.
86-
aggregate = logging_wrapper("aggregate")
87-
create_collection = logging_wrapper("create_collection")
88-
drop = logging_wrapper("drop")
89-
insert_many = logging_wrapper("insert_many")
90-
delete_many = logging_wrapper("delete_many")
91-
rename = logging_wrapper("rename")
92-
update_many = logging_wrapper("update_many")
9396

94-
del logging_wrapper
97+
if hasattr(OperationDebugWrapper, "logging_wrapper"):
98+
for attr in OperationDebugWrapper.wrapped_methods:
99+
setattr(OperationDebugWrapper, attr, OperationDebugWrapper.logging_wrapper(attr))
100+
del OperationDebugWrapper.logging_wrapper
101+
102+
103+
class OperationCollector(OperationDebugWrapper):
104+
def __init__(self, collected_sql=None, *, collection=None, db=None):
105+
super().__init__(db, collection)
106+
self.collected_sql = collected_sql
107+
108+
def log(self, op, args, kwargs=None):
109+
# If kwargs are used by any operations in the future, they must be
110+
# added to this logging.
111+
args = ", ".join(repr(arg) for arg in args)
112+
collection_name = f"{self.collection.name}." if self.collection is not None else ""
113+
operation = f"db.{collection_name}{op}({args})"
114+
self.collected_sql.append(operation)
115+
116+
def logging_wrapper(method):
117+
def wrapper(self, *args, **kwargs):
118+
self.log(method, args, kwargs)
119+
120+
return wrapper
121+
122+
123+
if hasattr(OperationCollector, "logging_wrapper"):
124+
for attr in OperationCollector.wrapped_methods:
125+
setattr(OperationCollector, attr, OperationCollector.logging_wrapper(attr))
126+
del OperationCollector.logging_wrapper

0 commit comments

Comments
 (0)