From a39a058a019b2658283e2ac298bd9fb6f5557a06 Mon Sep 17 00:00:00 2001
From: Robert Sparks <rjsparks@nostrum.com>
Date: Fri, 15 Sep 2023 17:33:05 -0500
Subject: [PATCH] fix: populate rfc_number on DocHistory with type_id rfc

---
 ietf/doc/migrations/0017_move_dochistory.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/ietf/doc/migrations/0017_move_dochistory.py b/ietf/doc/migrations/0017_move_dochistory.py
index da3a902e6..e218b0aea 100644
--- a/ietf/doc/migrations/0017_move_dochistory.py
+++ b/ietf/doc/migrations/0017_move_dochistory.py
@@ -8,6 +8,7 @@ from django.db.models import Subquery, OuterRef, F
 def forward(apps, schema_editor):
     DocHistory = apps.get_model("doc", "DocHistory")
     RelatedDocument = apps.get_model("doc", "RelatedDocument")
+    Document = apps.get_model("doc", "Document")
     DocHistory.objects.filter(type_id="draft", doc__type_id="rfc").update(type_id="rfc")
     DocHistory.objects.filter(
         type_id="draft", doc__type_id="draft", name__startswith="rfc"
@@ -20,9 +21,19 @@ def forward(apps, schema_editor):
     ).update(
         doc_id=F("rfc_id"), type_id="rfc"
     )
+    DocHistory.objects.filter(type_id="rfc").annotate(
+        rfcno=Subquery(
+            Document.objects.filter(pk=OuterRef("doc_id")).values_list(
+                "rfc_number", flat=True
+            )[:1]
+        )
+    ).update(rfc_number=F("rfcno"))
     assert not DocHistory.objects.filter(
         name__startswith="rfc", type_id="draft"
     ).exists()
+    assert not DocHistory.objects.filter(
+        type_id="rfc", rfc_number__isnull=True
+    ).exists()
 
 
 class Migration(migrations.Migration):