Skip to content

add support for sqlmigrate #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ def django_test_expected_failures(self):
"delete.tests.DeletionTests.test_only_referenced_fields_selected",
"lookup.tests.LookupTests.test_in_ignore_none",
"lookup.tests.LookupTests.test_textfield_exact_null",
"migrations.test_commands.MigrateTests.test_migrate_syncdb_app_label",
"migrations.test_commands.MigrateTests.test_migrate_syncdb_deferred_sql_executed_with_schemaeditor",
"queries.tests.ExistsSql.test_exists",
"queries.tests.Queries6Tests.test_col_alias_quoted",
"schema.tests.SchemaTests.test_rename_column_renames_deferred_sql_references",
Expand Down Expand Up @@ -668,17 +670,6 @@ def django_test_expected_failures(self):
"migrations.test_commands.MigrateTests.test_migrate_fake_split_initial",
"migrations.test_executor.ExecutorTests.test_soft_apply",
},
"SchemaEditor doesn't log or collect queries.": {
# https://github.com/mongodb-labs/django-mongodb/issues/141
"migrations.test_commands.MigrateTests.test_migrate_syncdb_app_label",
"migrations.test_commands.MigrateTests.test_migrate_syncdb_deferred_sql_executed_with_schemaeditor",
"migrations.test_commands.MigrateTests.test_sqlmigrate_backwards",
"migrations.test_commands.MigrateTests.test_sqlmigrate_for_non_atomic_migration",
"migrations.test_commands.MigrateTests.test_sqlmigrate_for_non_transactional_databases",
"migrations.test_commands.MigrateTests.test_sqlmigrate_forwards",
"migrations.test_commands.MigrateTests.test_sqlmigrate_replaced_migration",
"migrations.test_commands.MigrateTests.test_sqlmigrate_squashed_migration",
},
}

@cached_property
Expand Down
5 changes: 5 additions & 0 deletions django_mongodb/schema.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

from .query import wrap_database_errors
from .utils import OperationCollector


class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def get_collection(self, name):
if self.collect_sql:
return OperationCollector(self.collected_sql, collection=self.connection.database[name])
return self.connection.get_collection(name)

def get_database(self):
if self.collect_sql:
return OperationCollector(self.collected_sql, db=self.connection.database)
return self.connection.get_database()

@wrap_database_errors
Expand Down
46 changes: 37 additions & 9 deletions django_mongodb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,28 @@ def check_django_compatability():
)


def set_wrapped_methods(cls):
"""Initialize the wrapped methods on cls."""
if hasattr(cls, "logging_wrapper"):
for attr in cls.wrapped_methods:
setattr(cls, attr, cls.logging_wrapper(attr))
del cls.logging_wrapper
return cls


@set_wrapped_methods
class OperationDebugWrapper:
# The PyMongo database and collection methods that this backend uses.
wrapped_methods = {
"aggregate",
"create_collection",
"drop",
"insert_many",
"delete_many",
"rename",
"update_many",
}

def __init__(self, db, collection=None):
self.collection = collection
self.db = db
Expand Down Expand Up @@ -79,13 +100,20 @@ def wrapper(self, *args, **kwargs):

return wrapper

# These are the operations that this backend uses.
aggregate = logging_wrapper("aggregate")
create_collection = logging_wrapper("create_collection")
drop = logging_wrapper("drop")
insert_many = logging_wrapper("insert_many")
delete_many = logging_wrapper("delete_many")
rename = logging_wrapper("rename")
update_many = logging_wrapper("update_many")

del logging_wrapper
@set_wrapped_methods
class OperationCollector(OperationDebugWrapper):
def __init__(self, collected_sql=None, *, collection=None, db=None):
super().__init__(db, collection)
self.collected_sql = collected_sql

def log(self, op, args, kwargs=None):
args = ", ".join(repr(arg) for arg in args)
operation = f"db.{self.collection_name}{op}({args})"
self.collected_sql.append(operation)

def logging_wrapper(method):
def wrapper(self, *args, **kwargs):
self.log(method, args, kwargs)

return wrapper