0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Add a separate hash field for content path on ReferenceIndex

MySQL doesn't allow long fields to be used in unique indexes.
This commit is contained in:
Karl Hobley 2022-09-09 07:47:37 +00:00 committed by Matt Westcott
parent a9db3e966b
commit b5596cd58d
3 changed files with 16 additions and 2 deletions

View File

@ -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",
)
},
},

View File

@ -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
]

View File

@ -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)