[PATCH v2 2/3] models: Split Patch into two models

Finucane, Stephen stephen.finucane at intel.com
Tue Mar 22 05:12:58 AEDT 2016


On 16 Mar 16:28, Andy Doan wrote:
> On 03/13/2016 05:55 PM, Stephen Finucane wrote:
> 
> >  create mode 100644 patchwork/migrations/0009_add_submission_model.py
> >  create mode 100644 patchwork/migrations/0010_migrate_data_from_submission_to_patch.py
> >  create mode 100644 patchwork/migrations/0011_remove_temp_fields.py
> 
> I see how having these split up makes the code more readable, but
> aren't they sort of all-or-nothing?

They are, but I was going on the advice of the Django documentation
and breaking up data migrations from schema migrations [1]. In a
similar vein, the few guides I found on the subject recommended
splitting things up, though these were targeting South. TBH, I'm
happy to merge them, but I also don't see any disadvantages to
keeping them separate.

> >diff --git a/patchwork/migrations/0010_migrate_data_from_submission_to_patch.py b/patchwork/migrations/0010_migrate_data_from_submission_to_patch.py
> 
> >+def create_patch_instances(apps, schema_editor):
> >+    Submission = apps.get_model('patchwork', 'Submission')
> >+    Patch = apps.get_model('patchwork', 'Patch')
> >+
> >+    for submission in Submission.objects.all():
> >+        # NOTE(sfinucan) We copy every field _except_ tags, which is
> >+        # autogenerated anyway
> >+        patch = Patch(submission_ptr=submission,
> >+                      diff2=submission.diff,
> >+                      commit_ref2=submission.commit_ref,
> >+                      pull_url2=submission.pull_url,
> >+                      delegate2=submission.delegate,
> >+                      state2=submission.state,
> >+                      archived2=submission.archived,
> >+                      hash2=submission.hash)
> >+        patch.__dict__.update(submission.__dict__)
> >+        patch.save()
> 
> This logic is very time-consuming. It took 20 minutes on my
> instance. I just hacked this together in pure SQL to run in about 8
> seconds (not really tested):
> 
> migrations.RunSQL(
>             ['''INSERT INTO patchwork_patch
>                   (submission_ptr_id, diff2, commit_ref2, pull_url2,
>                    delegate2_id, state2_id, archived2, hash2)
>                 SELECT id, diff, commit_ref, pull_url, delegate_id,
>                        state_id, archived, hash
>                 FROM patchwork_submission
>                 '''],
>             ['TODO THE reverse migration i haven't figured out yet']
>         ),

Good idea. I'll send an update patchset with this included shortly.

Stephen

[1] https://docs.djangoproject.com/en/1.9/topics/migrations/#data-migrations


More information about the Patchwork mailing list