Merging two wagtail page types into one

Let's say we end up with two page types ContentPage and Folder that do the same job and we want to consolidate them into one. Basically we want to merge Folder into ContentPage.

I came up with next solution but want to be sure that I am not missing anything.

  1. migration:
class Migration(migrations.Migration):

    dependencies = [
        ...
    ]

    operations = [
        # copy all `folder` specific fields into `contentpage` table
        migrations.RunSQL(
            """
            INSERT INTO app_contentpage (
                page_ptr_id,
                ...,
                body,
                ...
            ) SELECT
                ...,
                body,
                ...
            FROM app_folderpage;
        """
        ),

        # update all tables that have references to `django_content_type` table
        # folder.content_type_id = 12
        # contentpage.content_type_id = 11
        migrations.RunSQL(
            """UPDATE wagtailcore_page SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_modellogentry SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_pagelogentry SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_revision SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_taskstate SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_workflowstate SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailsearch_indexentry SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_workflowcontenttype SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL(
            """UPDATE wagtailcore_task SET content_type_id = 11 WHERE content_type_id = 12;"""
        ),
        migrations.RunSQL("""DELETE FROM app_folderpage;"""),
    ]
  1. run python manage.py rebuild_references_index. Also it might be possible to update wagtailcore_referenceindex with SQL but I have feeling that running rebuild_references_index is more reliable

  2. remove Folder model from code and run python manage.py makemigrations

  3. manually clean up (or create migration) 'auth_permission' and django_admin_log where content_type_id is equal folder.content_type_id

  4. remove app_folder* records from django_content_type

I would appreciate any thoughts and comments.

Back to Top