prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Revision model rename breaks Page revision foreign key on SQLite
  ### Issue Summary
  
  #8441 renamed the PageRevision model to Revision, which included a migration with [a `RenameModel` step](https://github.com/wagtail/wagtail/blob/1f43d8ef51e455b92e42447fdc190d5ec83ec53c/wagtail/migrations/0070_rename_pagerevision_revision.py#L15-L18).
  
  On my local machine, running against SQLite in the default configuration, this renamed the table but didn't update the foreign key from the Page model. Looking at the SQL for the migration starts with:
  
  ```
  % ./manage.py sqlmigrate wagtailcore 0070
  BEGIN;
  --
  -- Rename model PageRevision to Revision
  --
  ALTER TABLE "wagtailcore_pagerevision" RENAME TO "wagtailcore_revision";
  ...
  ```
  
  But if I then check the `live_revision_id` foreign key on the Page model in SQLite, it hasn't been updated, and still points to the now-renamed `wagtailcore_pagerevision` table.
  
  ```
  % sqlite3 db.sqlite3 
  SQLite version 3.32.3 2020-06-18 14:16:19
  Enter ".help" for usage hints.
  sqlite> PRAGMA foreign_key_list('wagtailcore_page');
  ...
  3|0|wagtailcore_pagerevision|live_revision_id|id|NO ACTION|NO ACTION|NONE
  ...
  ```
  
  It looks like I'm getting hit by the `ALTER TABLE RENAME` issue described in the SQLite docs [here](https://www.sqlite.org/draft/lang_altertable.html#alter_table_rename):
  
  > Beginning with version 3.26.0, FOREIGN KEY constraints are always converted when a table is renamed, unless the [PRAGMA legacy_alter_table=ON](https://www.sqlite.org/draft/pragma.html#pragma_legacy_alter_table) setting is engaged.
  
  My `PRAGMA`s are defined thusly (the default values, apparently, for this version of SQLite on MacOS):
  
  ```
  sqlite> PRAGMA legacy_alter_table;
  1
  sqlite> PRAGMA foreign_keys;
  0
  ```
  
  I note [this commit](https://github.com/django/django/commit/063cf98d3a6839f40c423cbd845def429c5cf0ce) that just went into Django (the dev version for 4.1) that explicitly disables `legacy_alter_table`; I wonder if I am hitting some edge case that this would fix.
  
  Wagtail seems to have only [one other instance of `RenameModel`](https://github.com/wagtail/wagtail/blob/716bf92c2dc2da2aca5e8f5aa6768b5b087cd4b0/wagtail/contrib/search_promotions/migrations/0001_initial.py#L65) for the SearchPromotion model, but I don't think we have any foreign keys pointing to that. So this might be the first time this combination has hit Wagtail.
  
  ### Steps to Reproduce
  
  1. Start a new project with `wagtail start myproject`
  2. `cd myproject`
  3. `./manage.py migrate`
  4. `./manage.py createsuperuser` and create an admin user.
  5. `./manage.py runserver`
  6. Vist http://localhost:8000/admin/pages/add/home/homepage/3/ to create a new page (you'll be asked to log in first)
  7. Fill in the title field with something like "Test". Click "Save Draft". You'll get an error: "OperationalError at /admin/pages/add/home/homepage/3/, no such table: main.wagtailcore_pagerevision"
  
  -   I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes
  
  ### Technical details
  
  - Mac OS version: Big Sur 11.6.5
  - SQLite3 version: 3.32.3 2020-06-18 14:16:19 02c344aceaea0d177dd42e62c8541e3cab4a26c757ba33b3a31a43ccc7d4aapl
  -   Python version: 3.10.4
  -   Django version: 4.0.5, also happens on 3.2
  -   Wagtail version: main (1f43d8ef51e455b92e42447fdc190d5ec83ec53c)
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `wagtail/migrations/0070_rename_pagerevision_revision.py`
  Content:
  ```
  1 # Generated by Django 4.0.3 on 2022-04-26 12:31
  2 
  3 from django.conf import settings
  4 from django.db import migrations, models
  5 
  6 
  7 class Migration(migrations.Migration):
  8 
  9     dependencies = [
  10         migrations.swappable_dependency(settings.AUTH_USER_MODEL),
  11         ("wagtailcore", "0069_log_entry_jsonfield"),
  12     ]
  13 
  14     operations = [
  15         migrations.RenameModel(
  16             old_name="PageRevision",
  17             new_name="Revision",
  18         ),
  19         migrations.AlterModelOptions(
  20             name="revision",
  21             options={"verbose_name": "revision", "verbose_name_plural": "revisions"},
  22         ),
  23         migrations.AlterField(
  24             model_name="revision",
  25             name="page",
  26             field=models.CharField(max_length=255, verbose_name="object id"),
  27         ),
  28         migrations.RenameField(
  29             model_name="revision",
  30             old_name="page",
  31             new_name="object_id",
  32         ),
  33         migrations.AddField(
  34             model_name="revision",
  35             name="content_type",
  36             field=models.ForeignKey(
  37                 null=True,
  38                 on_delete=models.CASCADE,
  39                 related_name="+",
  40                 to="contenttypes.contenttype",
  41             ),
  42         ),
  43         migrations.AddField(
  44             model_name="revision",
  45             name="base_content_type",
  46             field=models.ForeignKey(
  47                 null=True,
  48                 on_delete=models.CASCADE,
  49                 related_name="+",
  50                 to="contenttypes.contenttype",
  51             ),
  52         ),
  53         migrations.AddIndex(
  54             model_name="revision",
  55             index=models.Index(
  56                 fields=["content_type", "object_id"],
  57                 name="content_object_idx",
  58             ),
  59         ),
  60         migrations.AddIndex(
  61             model_name="revision",
  62             index=models.Index(
  63                 fields=["base_content_type", "object_id"],
  64                 name="base_content_object_idx",
  65             ),
  66         ),
  67     ]
  68
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/wagtail/migrations/0070_rename_pagerevision_revision.py b/wagtail/migrations/0070_rename_pagerevision_revision.py
  --- a/wagtail/migrations/0070_rename_pagerevision_revision.py
  +++ b/wagtail/migrations/0070_rename_pagerevision_revision.py
  @@ -4,6 +4,12 @@
   from django.db import migrations, models
   
   
  +def disable_sqlite_legacy_alter_table(apps, schema_editor):
  +    # Fix for https://github.com/wagtail/wagtail/issues/8635
  +    if schema_editor.connection.vendor == "sqlite":
  +        schema_editor.execute("PRAGMA legacy_alter_table = OFF")
  +
  +
   class Migration(migrations.Migration):
   
       dependencies = [
  @@ -12,6 +18,10 @@
       ]
   
       operations = [
  +        migrations.RunPython(
  +            disable_sqlite_legacy_alter_table,
  +            migrations.RunPython.noop,
  +        ),
           migrations.RenameModel(
               old_name="PageRevision",
               new_name="Revision",
  
verification_info: {"golden_diff": "diff --git a/wagtail/migrations/0070_rename_pagerevision_revision.py b/wagtail/migrations/0070_rename_pagerevision_revision.py\n--- a/wagtail/migrations/0070_rename_pagerevision_revision.py\n+++ b/wagtail/migrations/0070_rename_pagerevision_revision.py\n@@ -4,6 +4,12 @@\n from django.db import migrations, models\n \n \n+def disable_sqlite_legacy_alter_table(apps, schema_editor):\n+    # Fix for https://github.com/wagtail/wagtail/issues/8635\n+    if schema_editor.connection.vendor == \"sqlite\":\n+        schema_editor.execute(\"PRAGMA legacy_alter_table = OFF\")\n+\n+\n class Migration(migrations.Migration):\n \n     dependencies = [\n@@ -12,6 +18,10 @@\n     ]\n \n     operations = [\n+        migrations.RunPython(\n+            disable_sqlite_legacy_alter_table,\n+            migrations.RunPython.noop,\n+        ),\n         migrations.RenameModel(\n             old_name=\"PageRevision\",\n             new_name=\"Revision\",\n", "issue": "Revision model rename breaks Page revision foreign key on SQLite\n### Issue Summary\r\n\r\n#8441 renamed the PageRevision model to Revision, which included a migration with [a `RenameModel` step](https://github.com/wagtail/wagtail/blob/1f43d8ef51e455b92e42447fdc190d5ec83ec53c/wagtail/migrations/0070_rename_pagerevision_revision.py#L15-L18).\r\n\r\nOn my local machine, running against SQLite in the default configuration, this renamed the table but didn't update the foreign key from the Page model. Looking at the SQL for the migration starts with:\r\n\r\n```\r\n% ./manage.py sqlmigrate wagtailcore 0070\r\nBEGIN;\r\n--\r\n-- Rename model PageRevision to Revision\r\n--\r\nALTER TABLE \"wagtailcore_pagerevision\" RENAME TO \"wagtailcore_revision\";\r\n...\r\n```\r\n\r\nBut if I then check the `live_revision_id` foreign key on the Page model in SQLite, it hasn't been updated, and still points to the now-renamed `wagtailcore_pagerevision` table.\r\n\r\n```\r\n% sqlite3 db.sqlite3 \r\nSQLite version 3.32.3 2020-06-18 14:16:19\r\nEnter \".help\" for usage hints.\r\nsqlite> PRAGMA foreign_key_list('wagtailcore_page');\r\n...\r\n3|0|wagtailcore_pagerevision|live_revision_id|id|NO ACTION|NO ACTION|NONE\r\n...\r\n```\r\n\r\nIt looks like I'm getting hit by the `ALTER TABLE RENAME` issue described in the SQLite docs [here](https://www.sqlite.org/draft/lang_altertable.html#alter_table_rename):\r\n\r\n> Beginning with version 3.26.0, FOREIGN KEY constraints are always converted when a table is renamed, unless the [PRAGMA legacy_alter_table=ON](https://www.sqlite.org/draft/pragma.html#pragma_legacy_alter_table) setting is engaged.\r\n\r\nMy `PRAGMA`s are defined thusly (the default values, apparently, for this version of SQLite on MacOS):\r\n\r\n```\r\nsqlite> PRAGMA legacy_alter_table;\r\n1\r\nsqlite> PRAGMA foreign_keys;\r\n0\r\n```\r\n\r\nI note [this commit](https://github.com/django/django/commit/063cf98d3a6839f40c423cbd845def429c5cf0ce) that just went into Django (the dev version for 4.1) that explicitly disables `legacy_alter_table`; I wonder if I am hitting some edge case that this would fix.\r\n\r\nWagtail seems to have only [one other instance of `RenameModel`](https://github.com/wagtail/wagtail/blob/716bf92c2dc2da2aca5e8f5aa6768b5b087cd4b0/wagtail/contrib/search_promotions/migrations/0001_initial.py#L65) for the SearchPromotion model, but I don't think we have any foreign keys pointing to that. So this might be the first time this combination has hit Wagtail.\r\n\r\n### Steps to Reproduce\r\n\r\n1. Start a new project with `wagtail start myproject`\r\n2. `cd myproject`\r\n3. `./manage.py migrate`\r\n4. `./manage.py createsuperuser` and create an admin user.\r\n5. `./manage.py runserver`\r\n6. Vist http://localhost:8000/admin/pages/add/home/homepage/3/ to create a new page (you'll be asked to log in first)\r\n7. Fill in the title field with something like \"Test\". Click \"Save Draft\". You'll get an error: \"OperationalError at /admin/pages/add/home/homepage/3/, no such table: main.wagtailcore_pagerevision\"\r\n\r\n-   I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes\r\n\r\n### Technical details\r\n\r\n- Mac OS version: Big Sur 11.6.5\r\n- SQLite3 version: 3.32.3 2020-06-18 14:16:19 02c344aceaea0d177dd42e62c8541e3cab4a26c757ba33b3a31a43ccc7d4aapl\r\n-   Python version: 3.10.4\r\n-   Django version: 4.0.5, also happens on 3.2\r\n-   Wagtail version: main (1f43d8ef51e455b92e42447fdc190d5ec83ec53c)\r\n\n", "before_files": [{"content": "# Generated by Django 4.0.3 on 2022-04-26 12:31\n\nfrom django.conf import settings\nfrom django.db import migrations, models\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        migrations.swappable_dependency(settings.AUTH_USER_MODEL),\n        (\"wagtailcore\", \"0069_log_entry_jsonfield\"),\n    ]\n\n    operations = [\n        migrations.RenameModel(\n            old_name=\"PageRevision\",\n            new_name=\"Revision\",\n        ),\n        migrations.AlterModelOptions(\n            name=\"revision\",\n            options={\"verbose_name\": \"revision\", \"verbose_name_plural\": \"revisions\"},\n        ),\n        migrations.AlterField(\n            model_name=\"revision\",\n            name=\"page\",\n            field=models.CharField(max_length=255, verbose_name=\"object id\"),\n        ),\n        migrations.RenameField(\n            model_name=\"revision\",\n            old_name=\"page\",\n            new_name=\"object_id\",\n        ),\n        migrations.AddField(\n            model_name=\"revision\",\n            name=\"content_type\",\n            field=models.ForeignKey(\n                null=True,\n                on_delete=models.CASCADE,\n                related_name=\"+\",\n                to=\"contenttypes.contenttype\",\n            ),\n        ),\n        migrations.AddField(\n            model_name=\"revision\",\n            name=\"base_content_type\",\n            field=models.ForeignKey(\n                null=True,\n                on_delete=models.CASCADE,\n                related_name=\"+\",\n                to=\"contenttypes.contenttype\",\n            ),\n        ),\n        migrations.AddIndex(\n            model_name=\"revision\",\n            index=models.Index(\n                fields=[\"content_type\", \"object_id\"],\n                name=\"content_object_idx\",\n            ),\n        ),\n        migrations.AddIndex(\n            model_name=\"revision\",\n            index=models.Index(\n                fields=[\"base_content_type\", \"object_id\"],\n                name=\"base_content_object_idx\",\n            ),\n        ),\n    ]\n", "path": "wagtail/migrations/0070_rename_pagerevision_revision.py"}], "after_files": [{"content": "# Generated by Django 4.0.3 on 2022-04-26 12:31\n\nfrom django.conf import settings\nfrom django.db import migrations, models\n\n\ndef disable_sqlite_legacy_alter_table(apps, schema_editor):\n    # Fix for https://github.com/wagtail/wagtail/issues/8635\n    if schema_editor.connection.vendor == \"sqlite\":\n        schema_editor.execute(\"PRAGMA legacy_alter_table = OFF\")\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        migrations.swappable_dependency(settings.AUTH_USER_MODEL),\n        (\"wagtailcore\", \"0069_log_entry_jsonfield\"),\n    ]\n\n    operations = [\n        migrations.RunPython(\n            disable_sqlite_legacy_alter_table,\n            migrations.RunPython.noop,\n        ),\n        migrations.RenameModel(\n            old_name=\"PageRevision\",\n            new_name=\"Revision\",\n        ),\n        migrations.AlterModelOptions(\n            name=\"revision\",\n            options={\"verbose_name\": \"revision\", \"verbose_name_plural\": \"revisions\"},\n        ),\n        migrations.AlterField(\n            model_name=\"revision\",\n            name=\"page\",\n            field=models.CharField(max_length=255, verbose_name=\"object id\"),\n        ),\n        migrations.RenameField(\n            model_name=\"revision\",\n            old_name=\"page\",\n            new_name=\"object_id\",\n        ),\n        migrations.AddField(\n            model_name=\"revision\",\n            name=\"content_type\",\n            field=models.ForeignKey(\n                null=True,\n                on_delete=models.CASCADE,\n                related_name=\"+\",\n                to=\"contenttypes.contenttype\",\n            ),\n        ),\n        migrations.AddField(\n            model_name=\"revision\",\n            name=\"base_content_type\",\n            field=models.ForeignKey(\n                null=True,\n                on_delete=models.CASCADE,\n                related_name=\"+\",\n                to=\"contenttypes.contenttype\",\n            ),\n        ),\n        migrations.AddIndex(\n            model_name=\"revision\",\n            index=models.Index(\n                fields=[\"content_type\", \"object_id\"],\n                name=\"content_object_idx\",\n            ),\n        ),\n        migrations.AddIndex(\n            model_name=\"revision\",\n            index=models.Index(\n                fields=[\"base_content_type\", \"object_id\"],\n                name=\"base_content_object_idx\",\n            ),\n        ),\n    ]\n", "path": "wagtail/migrations/0070_rename_pagerevision_revision.py"}]}