summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2016-03-24 13:20:43 -0500
committerStephen Finucane <stephen.finucane@intel.com>2016-03-25 11:00:44 +0000
commit1836576a199952130822b2b9490032116b068a5c (patch)
treed206e76f60a3968170c02d67de24c7103e5f0dee
parent34ccee4a2b2b332c4b0967e5fcb691f72a44478e (diff)
downloadpatchwork-1836576a199952130822b2b9490032116b068a5c.tar
patchwork-1836576a199952130822b2b9490032116b068a5c.tar.gz
migrations: Optimize 0007 migration
By handling comment copying/deletion via SQL we can make migration take < 3 minutes rather than > 30 minutes for big instances. The SQL used is vendor specific, but covers the two DBs supported by Patchwork. Signed-off-by: Andy Doan <andy.doan@linaro.org> Reviewed-by: Stephen Finucane <stephen.finucane@intel.com>
-rw-r--r--patchwork/migrations/0007_move_comment_content_to_patch_content.py94
1 files changed, 61 insertions, 33 deletions
diff --git a/patchwork/migrations/0007_move_comment_content_to_patch_content.py b/patchwork/migrations/0007_move_comment_content_to_patch_content.py
index 63d57ba..43c135e 100644
--- a/patchwork/migrations/0007_move_comment_content_to_patch_content.py
+++ b/patchwork/migrations/0007_move_comment_content_to_patch_content.py
@@ -1,47 +1,75 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
-from django.db import migrations
+from django.db import connection, migrations
def copy_comment_field(apps, schema_editor):
- Comment = apps.get_model('patchwork', 'Comment')
- Patch = apps.get_model('patchwork', 'Patch')
+ if connection.vendor == 'postgresql':
+ schema_editor.execute('''
+ UPDATE patchwork_patch
+ SET content = patchwork_comment.content
+ FROM patchwork_comment
+ WHERE patchwork_patch.id=patchwork_comment.patch_id
+ AND patchwork_patch.msgid=patchwork_comment.msgid
+ ''')
+ elif connection.vendor == 'mysql':
+ schema_editor.execute('''
+ UPDATE patchwork_patch, patchwork_comment
+ SET patchwork_patch.content = patchwork_comment.content
+ WHERE patchwork_patch.id=patchwork_comment.patch_id
+ AND patchwork_patch.msgid=patchwork_comment.msgid
+ ''')
+ else:
+ Comment = apps.get_model('patchwork', 'Comment')
+ Patch = apps.get_model('patchwork', 'Patch')
+
+ for patch in Patch.objects.all():
+ try:
+ # when available, this can only return one entry due to the
+ # unique_together constraint
+ comment = Comment.objects.get(patch=patch, msgid=patch.msgid)
+ except Comment.DoesNotExist:
+ # though there's no requirement to actually have a comment
+ continue
+
+ patch.content = comment.content
+ patch.save()
- for patch in Patch.objects.all():
- try:
- # when available, this can only return one entry due to the
- # unique_together constraint
- comment = Comment.objects.get(patch=patch, msgid=patch.msgid)
- except Comment.DoesNotExist:
- # though there's no requirement to actually have a comment
- continue
- patch.content = comment.content
- patch.save()
+def remove_duplicate_comments(apps, schema_editor):
+ if connection.vendor == 'postgresql':
+ schema_editor.execute('''
+ DELETE FROM patchwork_comment
+ USING patchwork_patch
+ WHERE patchwork_patch.id=patchwork_comment.patch_id
+ AND patchwork_patch.msgid=patchwork_comment.msgid
+ ''')
+ elif connection.vendor == 'mysql':
+ schema_editor.execute('''
+ DELETE FROM patchwork_comment
+ USING patchwork_patch, patchwork_comment
+ WHERE patchwork_patch.id=patchwork_comment.patch_id
+ AND patchwork_patch.msgid=patchwork_comment.msgid
+ ''')
+ else:
+ Comment = apps.get_model('patchwork', 'Comment')
+ Patch = apps.get_model('patchwork', 'Patch')
+
+ for patch in Patch.objects.all():
+ try:
+ # when available, this can only return one entry due to the
+ # unique_together constraint
+ comment = Comment.objects.get(patch=patch, msgid=patch.msgid)
+ comment.delete()
+ except Comment.DoesNotExist:
+ # though there's no requirement to actually have a comment
+ continue
def uncopy_comment_field(apps, schema_editor):
- Patch = apps.get_model('patchwork', 'Patch')
-
- for patch in Patch.objects.all():
- patch.content = None
- patch.save()
-
-
-def remove_duplicate_comments(apps, schema_editor):
- Comment = apps.get_model('patchwork', 'Comment')
- Patch = apps.get_model('patchwork', 'Patch')
-
- for patch in Patch.objects.all():
- try:
- # when available, this can only return one entry due to the
- # unique_together constraint
- comment = Comment.objects.get(patch=patch, msgid=patch.msgid)
- comment.delete()
- except Comment.DoesNotExist:
- # though there's no requirement to actually have a comment
- continue
+ # This is no-op because the column is being deleted
+ pass
def recreate_comments(apps, schema_editor):