From 90a3813418b15c0d62336768a6647ddec9d8eea6 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Tue, 13 Dec 2022 15:50:26 +0000 Subject: [PATCH] Add migration to populate `WorkflowState`'s `content_type` and `base_content_type` fields --- ...081_populate_workflowstate_content_type.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 wagtail/migrations/0081_populate_workflowstate_content_type.py diff --git a/wagtail/migrations/0081_populate_workflowstate_content_type.py b/wagtail/migrations/0081_populate_workflowstate_content_type.py new file mode 100644 index 0000000000..0be4d286e3 --- /dev/null +++ b/wagtail/migrations/0081_populate_workflowstate_content_type.py @@ -0,0 +1,41 @@ +# Generated by Django 4.2.dev20221212151407 on 2022-12-12 15:16 + +from django.db import migrations, models +from django.db.models.functions import Cast + + +def populate_workflowstate_content_type(apps, schema_editor): + ContentType = apps.get_model("contenttypes.ContentType") + WorkflowState = apps.get_model("wagtailcore.WorkflowState") + Page = apps.get_model("wagtailcore.Page") + + page_type = ContentType.objects.get(app_label="wagtailcore", model="page") + content_type_id = models.Subquery( + Page.objects.filter( + pk=Cast(models.OuterRef("object_id"), models.PositiveIntegerField()) + ).values("content_type_id") + ) + + WorkflowState.objects.all().update( + base_content_type=page_type, + content_type_id=content_type_id, + ) + + +def empty_workflowstate_content_type(apps, schema_editor): + WorkflowState = apps.get_model("wagtailcore.WorkflowState") + WorkflowState.objects.all().update(base_content_type=None, content_type=None) + + +class Migration(migrations.Migration): + + dependencies = [ + ("wagtailcore", "0080_generic_workflowstate"), + ] + + operations = [ + migrations.RunPython( + populate_workflowstate_content_type, + empty_workflowstate_content_type, + ) + ]