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

Andy Doan andy.doan at linaro.org
Thu Mar 17 08:28:47 AEDT 2016


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?

> 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']
         ),


More information about the Patchwork mailing list