]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
migrations: Resolve issues with other DB backends for 0042, 0043
authorStephen Finucane <stephen@that.guru>
Sun, 26 Apr 2020 13:17:57 +0000 (14:17 +0100)
committerStephen Finucane <stephen@that.guru>
Sun, 26 Apr 2020 15:06:45 +0000 (16:06 +0100)
0042 was using MySQL-specific SQL to delete entries in the
'patchwork_comment' table that were associated with entries in the
'patchwork_coverletter' table, while 0043 only considered MySQL and
PostgrSQL when attempting to copy fields from 'patchwork_patch' to
'patchwork_submission'. Both issues are resolved.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/migrations/0042_add_cover_model.py
patchwork/migrations/0043_merge_patch_submission.py

index 53196c18bc3c89eba33678880fc04bac54bd093a..3f4e03407a96121935749ccfae06ce9bc6247512 100644 (file)
@@ -1,10 +1,36 @@
 import datetime
 
-from django.db import migrations, models
+from django.db import connection, migrations, models
 import django.db.models.deletion
 import patchwork.models
 
 
+def delete_coverletter_comments(apps, schema_editor):
+    if connection.vendor == 'mysql':
+        schema_editor.execute(
+            """
+            DELETE patchwork_comment FROM
+                patchwork_comment
+            INNER JOIN patchwork_coverletter
+                ON patchwork_coverletter.submission_ptr_id = patchwork_comment.submission_id
+            """,  # noqa
+        )
+    elif connection.vendor == 'postgresql':
+        schema_editor.execute(
+            """
+            DELETE
+            FROM patchwork_comment
+                USING patchwork_coverletter
+            WHERE patchwork_coverletter.submission_ptr_id = patchwork_comment.submission_id
+            """,  # noqa
+        )
+    else:
+        CoverLetter = apps.get_model('patchwork', 'CoverLetter')
+
+        for cover in CoverLetter.objects.all():
+            cover.comments.all().delete()
+
+
 class Migration(migrations.Migration):
 
     dependencies = [
@@ -175,15 +201,7 @@ class Migration(migrations.Migration):
         # remove all the old 'CoverLetter'-related entries from the 'Comment'
         # table
 
-        migrations.RunSQL(
-            """
-            DELETE patchwork_comment FROM
-                patchwork_comment
-            INNER JOIN patchwork_coverletter
-                ON patchwork_coverletter.submission_ptr_id = patchwork_comment.submission_id
-            """,  # noqa
-            None
-        ),
+        migrations.RunPython(delete_coverletter_comments, None, atomic=False),
 
         # delete the old 'CoverLetter' model
 
index 25e741d6e32bd2f367beffd4046366cce762e069..d351892ef7f21f6868615540e3ce0f4b26c29c38 100644 (file)
@@ -42,11 +42,41 @@ def migrate_data(apps, schema_editor):
             """  # noqa
         )
     else:
-        raise Exception('DB not supported')
+        schema_editor.execute(
+            """
+            UPDATE patchwork_submission
+              SET (
+                archived, commit_ref, delegate_id, diff, hash, number,
+                pull_url, related_id, series_id, state_id
+              ) = (
+                SELECT
+                  patchwork_patch.archived2,
+                  patchwork_patch.commit_ref2,
+                  patchwork_patch.delegate2_id,
+                  patchwork_patch.diff2,
+                  patchwork_patch.hash2,
+                  patchwork_patch.number2,
+                  patchwork_patch.pull_url2,
+                  patchwork_patch.related2_id,
+                  patchwork_patch.series2_id,
+                  patchwork_patch.state2_id
+                FROM patchwork_patch
+                WHERE patchwork_patch.submission_ptr_id = patchwork_submission.id
+              )
+            WHERE
+              EXISTS (
+                SELECT *
+                FROM patchwork_patch
+                WHERE patchwork_patch.submission_ptr_id = patchwork_submission.id
+              )
+            """  # noqa
+        )
 
 
 class Migration(migrations.Migration):
 
+    atomic = False
+
     dependencies = [
         ('patchwork', '0042_add_cover_model'),
     ]