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 = [
# 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
""" # 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'),
]