From bbec58cc004a4c6ae6521f28c4060bd66522c858 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 12 Sep 2024 09:19:06 -0400 Subject: [PATCH 1/4] remove expected failures fixed on the Django fork --- django_mongodb/features.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/django_mongodb/features.py b/django_mongodb/features.py index e3cc4aee1..17de02084 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -93,7 +93,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "schema.tests.SchemaTests.test_order_index", "schema.tests.SchemaTests.test_text_field_with_db_index", # AlterField - "schema.tests.SchemaTests.test_alter", "schema.tests.SchemaTests.test_alter_auto_field_to_integer_field", "schema.tests.SchemaTests.test_alter_field_add_index_to_integerfield", "schema.tests.SchemaTests.test_alter_field_default_dropped", @@ -104,13 +103,11 @@ class DatabaseFeatures(BaseDatabaseFeatures): "schema.tests.SchemaTests.test_alter_int_pk_to_int_unique", "schema.tests.SchemaTests.test_alter_not_unique_field_to_primary_key", "schema.tests.SchemaTests.test_alter_null_to_not_null", - "schema.tests.SchemaTests.test_alter_null_to_not_null_keeping_default", "schema.tests.SchemaTests.test_alter_primary_key_the_same_name", "schema.tests.SchemaTests.test_autofield_to_o2o", # AlterField (rename) "schema.tests.SchemaTests.test_rename", "schema.tests.SchemaTests.test_rename_keep_db_default", - "schema.tests.SchemaTests.test_rename_keep_null_status", # AlterField (db_index) "schema.tests.SchemaTests.test_indexes", "schema.tests.SchemaTests.test_remove_constraints_capital_letters", @@ -126,13 +123,9 @@ class DatabaseFeatures(BaseDatabaseFeatures): "schema.tests.SchemaTests.test_remove_unique_together_does_not_remove_meta_constraints", "schema.tests.SchemaTests.test_unique_together", # ManyToManyField - "schema.tests.SchemaTests.test_m2m_rename_field_in_target_model", "schema.tests.SchemaTests.test_m2m_repoint", "schema.tests.SchemaTests.test_m2m_repoint_custom", "schema.tests.SchemaTests.test_m2m_repoint_inherited", - "schema.tests.SchemaTests.test_m2m_through_alter", - "schema.tests.SchemaTests.test_m2m_through_alter_custom", - "schema.tests.SchemaTests.test_m2m_through_alter_inherited", "schema.tests.SchemaTests.test_m2m_through_remove", # add/remove_constraint "schema.tests.SchemaTests.test_composed_constraint_with_fk", From 8d872495165f4e8136c2ada286c9982a8f8f1503 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 12 Sep 2024 09:25:28 -0400 Subject: [PATCH 2/4] fix ManyToManyField repointing --- django_mongodb/features.py | 5 ----- django_mongodb/fields/auto.py | 6 +++--- django_mongodb/schema.py | 12 +++++++++++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 17de02084..fce867a1b 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -122,11 +122,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): # alter_unique_together "schema.tests.SchemaTests.test_remove_unique_together_does_not_remove_meta_constraints", "schema.tests.SchemaTests.test_unique_together", - # ManyToManyField - "schema.tests.SchemaTests.test_m2m_repoint", - "schema.tests.SchemaTests.test_m2m_repoint_custom", - "schema.tests.SchemaTests.test_m2m_repoint_inherited", - "schema.tests.SchemaTests.test_m2m_through_remove", # add/remove_constraint "schema.tests.SchemaTests.test_composed_constraint_with_fk", "schema.tests.SchemaTests.test_remove_ignored_unique_constraint_not_create_fk_index", diff --git a/django_mongodb/fields/auto.py b/django_mongodb/fields/auto.py index c79a21e8b..1a8a9ee32 100644 --- a/django_mongodb/fields/auto.py +++ b/django_mongodb/fields/auto.py @@ -1,6 +1,6 @@ from bson import ObjectId, errors from django.core import exceptions -from django.db.models.fields import AutoField, Field +from django.db.models.fields import AutoField from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ @@ -31,8 +31,8 @@ def get_prep_value(self, value): return int(value) raise ValueError(f"Field '{self.name}' expected an ObjectId but got {value!r}.") from e - def rel_db_type(self, connection): - return Field().db_type(connection=connection) + def db_type(self, connection): + return "ObjectId" def to_python(self, value): if value is None or isinstance(value, int): diff --git a/django_mongodb/schema.py b/django_mongodb/schema.py index 896ad8dd2..6927d6ff6 100644 --- a/django_mongodb/schema.py +++ b/django_mongodb/schema.py @@ -27,7 +27,17 @@ def add_field(self, model, field): {}, [{"$set": {column: self.effective_default(field)}}] ) - def alter_field(self, model, old_field, new_field, strict=False): + def _alter_field( + self, + model, + old_field, + new_field, + old_type, + new_type, + old_db_params, + new_db_params, + strict=False, + ): pass def remove_field(self, model, field): From 7fe880edda20ed6b7905ad9d10cdcca7a3005d3b Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 12 Sep 2024 20:19:22 -0400 Subject: [PATCH 3/4] move db_default expected failures to skips --- django_mongodb/features.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/django_mongodb/features.py b/django_mongodb/features.py index fce867a1b..6cbef9db6 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -33,9 +33,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): uses_savepoints = False _django_test_expected_failures = { - # Database defaults not supported: bson.errors.InvalidDocument: - # cannot encode object: Date: Thu, 12 Sep 2024 20:20:54 -0400 Subject: [PATCH 4/4] add support for column rename --- django_mongodb/features.py | 2 -- django_mongodb/schema.py | 6 +++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 6cbef9db6..6236ed770 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -102,8 +102,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "schema.tests.SchemaTests.test_alter_null_to_not_null", "schema.tests.SchemaTests.test_alter_primary_key_the_same_name", "schema.tests.SchemaTests.test_autofield_to_o2o", - # AlterField (rename) - "schema.tests.SchemaTests.test_rename", # AlterField (db_index) "schema.tests.SchemaTests.test_indexes", "schema.tests.SchemaTests.test_remove_constraints_capital_letters", diff --git a/django_mongodb/schema.py b/django_mongodb/schema.py index 6927d6ff6..5867a2365 100644 --- a/django_mongodb/schema.py +++ b/django_mongodb/schema.py @@ -38,7 +38,11 @@ def _alter_field( new_db_params, strict=False, ): - pass + # Have they renamed the column? + if old_field.column != new_field.column: + self.connection.database[model._meta.db_table].update_many( + {}, {"$rename": {old_field.column: new_field.column}} + ) def remove_field(self, model, field): # Remove implicit M2M tables.