Forráskód Böngészése

Add a separate hash field for content path on ReferenceIndex

MySQL doesn't allow long fields to be used in unique indexes.
Karl Hobley 2 éve
szülő
commit
b5596cd58d

+ 2 - 1
wagtail/migrations/0078_referenceindex.py

@@ -34,6 +34,7 @@ class Migration(migrations.Migration):
                 ),
                 ("model_path", models.TextField()),
                 ("content_path", models.TextField()),
+                ("content_path_hash", models.UUIDField()),
                 (
                     "base_content_type",
                     models.ForeignKey(
@@ -66,7 +67,7 @@ class Migration(migrations.Migration):
                         "object_id",
                         "to_content_type",
                         "to_object_id",
-                        "content_path",
+                        "content_path_hash",
                     )
                 },
             },

+ 12 - 1
wagtail/models/__init__.py

@@ -4695,6 +4695,10 @@ class ReferenceIndex(models.Model):
     model_path = models.TextField()
     content_path = models.TextField()
 
+    # We need a separate hash field for content_path in order to use it in a unique key because
+    # MySQL has a limit to the size of fields that are included in unique keys
+    content_path_hash = models.UUIDField()
+
     wagtail_reference_index_ignore = True
 
     class Meta:
@@ -4704,7 +4708,7 @@ class ReferenceIndex(models.Model):
                 "object_id",
                 "to_content_type",
                 "to_object_id",
-                "content_path",
+                "content_path_hash",
             )
         ]
 
@@ -4791,6 +4795,12 @@ class ReferenceIndex(models.Model):
                         )
                     )
 
+    @classmethod
+    def _get_content_path_hash(cls, content_path):
+        return uuid.uuid5(
+            uuid.UUID("bdc70d8b-e7a2-4c2a-bf43-2a3e3fcbbe86"), content_path
+        )
+
     @classmethod
     def create_or_update_for_object(cls, object):
         # Extract new references
@@ -4820,6 +4830,7 @@ class ReferenceIndex(models.Model):
                     to_object_id=to_object_id,
                     model_path=model_path,
                     content_path=content_path,
+                    content_path_hash=cls._get_content_path_hash(content_path),
                 )
                 for to_content_type_id, to_object_id, model_path, content_path in new_references
             ]

+ 2 - 0
wagtail/tests/test_reference_index.py

@@ -95,6 +95,7 @@ class TestCreateOrUpdateForObject(TestCase):
             to_object_id=self.test_feed_image.pk,
             model_path="feed_image",
             content_path="feed_image",
+            content_path_hash=ReferenceIndex._get_content_path_hash("feed_image"),
         )
         reference_to_remove = ReferenceIndex.objects.create(
             base_content_type=ReferenceIndex._get_base_content_type(self.event_page),
@@ -104,6 +105,7 @@ class TestCreateOrUpdateForObject(TestCase):
             to_object_id=self.test_image_1.pk,  # Image ID is not used in this field
             model_path="feed_image",
             content_path="feed_image",
+            content_path_hash=ReferenceIndex._get_content_path_hash("feed_image"),
         )
 
         ReferenceIndex.create_or_update_for_object(self.event_page)