diff options
author | Stephen Finucane <stephen.finucane@intel.com> | 2016-03-25 17:29:29 +0000 |
---|---|---|
committer | Stephen Finucane <stephen.finucane@intel.com> | 2016-04-01 15:51:54 +0100 |
commit | 86172ccc161b44d68fcbc734f642644c8e1be84a (patch) | |
tree | 482923c6321068b1dfe5637970d359e25c7a0081 /patchwork/migrations | |
parent | 6702a0c92b161ba8520857f1259faa83488c7100 (diff) | |
download | patchwork-86172ccc161b44d68fcbc734f642644c8e1be84a.tar patchwork-86172ccc161b44d68fcbc734f642644c8e1be84a.tar.gz |
models: Split Patch into two models
There are a lot of similarities between cover letters and patches: so
many, in fact, that it would be helpful to occasionally treat them as
the same thing. Achieve this by extracting out the fields that would be
shared between a Patch and a hypothetical cover letter into a "sub
model". This allows us to do cool stuff like assigning comments to both
patches and cover letters or listing both patches and cover letters on
the main screen in a natural way.
The migrations for this are really the only complicated part. There are
three, broken up into schema and data migrations per Django customs,
and they works as follows:
* Rename the 'Patch' model to 'Submission', then create a subclass
called 'Patch' that includes duplicates of the patch-specific fields
of Submission (with changed names to prevent conflicts). Rename
non-patch specific references to the renamed 'Submission' model
as necessary.
* Duplicate the contents of the patch-specific fields from 'Submission'
to 'Patch'
* Remove the patch-specific fields from 'Submission', renaming the
'Patch' model to take their place. Update the patch-specific
references to point the new 'Patch' model, rather than 'Submission'.
This comes at the cost of an additional JOIN per item on the main
screen, but this seems a small price to pay for the additional
functionality gained. To minimise this, however, caching will be added.
Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
Signed-off-by: Andy Doan <andy.doan@linaro.org>
Diffstat (limited to 'patchwork/migrations')
3 files changed, 231 insertions, 0 deletions
diff --git a/patchwork/migrations/0009_add_submission_model.py b/patchwork/migrations/0009_add_submission_model.py new file mode 100644 index 0000000..6bb68fb --- /dev/null +++ b/patchwork/migrations/0009_add_submission_model.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models + +import patchwork.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('patchwork', '0008_add_email_mixin'), + ] + + operations = [ + # Rename the 'Patch' to 'Submission' + migrations.RenameModel( + old_name='Patch', + new_name='Submission' + ), + migrations.AlterModelOptions( + name='submission', + options={'ordering': ['date']}, + ), + + # Rename the non-Patch specific references to point to Submission + migrations.RenameField( + model_name='comment', + old_name='patch', + new_name='submission', + ), + migrations.AlterUniqueTogether( + name='comment', + unique_together=set([('msgid', 'submission')]), + ), + migrations.RenameField( + model_name='userprofile', + old_name='patches_per_page', + new_name='items_per_page', + ), + migrations.AlterField( + model_name='userprofile', + name='items_per_page', + field=models.PositiveIntegerField( + default=100, + help_text=b'Number of items to display per page'), + ), + + # Recreate the 'Patch' model as a subclass of 'Submission'. Each field + # is given a unique name to prevent it conflicting with the same field + # found in the 'Submission' "super model". We will fix this later. + migrations.CreateModel( + name='Patch', + fields=[ + ('submission_ptr', models.OneToOneField( + parent_link=True, auto_created=True, primary_key=True, + serialize=False, to='patchwork.Submission')), + ('diff2', models.TextField(null=True, blank=True)), + ('commit_ref2', models.CharField( + max_length=255, null=True, blank=True)), + ('pull_url2', models.CharField( + max_length=255, null=True, blank=True)), + # we won't migrate the data of this, seeing as it's + # automatically recreated every time we save a Patch + ('tags2', models.ManyToManyField( + to='patchwork.Tag', through='patchwork.PatchTag')), + ('delegate2', models.ForeignKey( + blank=True, to=settings.AUTH_USER_MODEL, null=True)), + ('state2', models.ForeignKey(to='patchwork.State')), + ('archived2', models.BooleanField(default=False)), + ('hash2', patchwork.models.HashField( + max_length=40, null=True, blank=True)), + ], + options={ + 'verbose_name_plural': 'Patches', + }, + bases=('patchwork.submission',), + ), + ] diff --git a/patchwork/migrations/0010_migrate_data_from_submission_to_patch.py b/patchwork/migrations/0010_migrate_data_from_submission_to_patch.py new file mode 100644 index 0000000..1d4b6e1 --- /dev/null +++ b/patchwork/migrations/0010_migrate_data_from_submission_to_patch.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('patchwork', '0009_add_submission_model'), + ] + + operations = [ + 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 + '''], + ['''UPDATE patchwork_submission SET + diff=diff2, commit_ref=commit_ref2, pull_url=pull_url2, + delegate_id=delegate2_id, state_id=state2_id, + archived=archived2, hash=hash2 + FROM patchwork_patch WHERE + patchwork_submission.id = patchwork_patch.submission_ptr_id + '''] + ), + ] diff --git a/patchwork/migrations/0011_remove_temp_fields.py b/patchwork/migrations/0011_remove_temp_fields.py new file mode 100644 index 0000000..6b159c5 --- /dev/null +++ b/patchwork/migrations/0011_remove_temp_fields.py @@ -0,0 +1,121 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('patchwork', '0010_migrate_data_from_submission_to_patch'), + ] + + operations = [ + # Remove duplicate fields from 'Submission' and rename 'Patch' version + migrations.RemoveField( + model_name='submission', + name='diff', + ), + migrations.RenameField( + model_name='patch', + old_name='diff2', + new_name='diff', + ), + migrations.RemoveField( + model_name='submission', + name='commit_ref', + ), + migrations.RenameField( + model_name='patch', + old_name='commit_ref2', + new_name='commit_ref', + ), + migrations.RemoveField( + model_name='submission', + name='pull_url', + ), + migrations.RenameField( + model_name='patch', + old_name='pull_url2', + new_name='pull_url', + ), + migrations.RemoveField( + model_name='submission', + name='tags', + ), + migrations.RenameField( + model_name='patch', + old_name='tags2', + new_name='tags', + ), + migrations.RemoveField( + model_name='submission', + name='delegate', + ), + migrations.RenameField( + model_name='patch', + old_name='delegate2', + new_name='delegate', + ), + migrations.RemoveField( + model_name='submission', + name='state', + ), + migrations.RenameField( + model_name='patch', + old_name='state2', + new_name='state', + ), + migrations.RemoveField( + model_name='submission', + name='archived', + ), + migrations.RenameField( + model_name='patch', + old_name='archived2', + new_name='archived', + ), + migrations.RemoveField( + model_name='submission', + name='hash', + ), + migrations.RenameField( + model_name='patch', + old_name='hash2', + new_name='hash', + ), + # Update any many-to-many fields to point to Patch now + migrations.AlterField( + model_name='bundle', + name='patches', + field=models.ManyToManyField(to='patchwork.Patch', + through='patchwork.BundlePatch'), + ), + migrations.AlterField( + model_name='bundlepatch', + name='patch', + field=models.ForeignKey(to='patchwork.Patch'), + ), + migrations.AlterField( + model_name='check', + name='patch', + field=models.ForeignKey(to='patchwork.Patch'), + ), + migrations.AlterField( + model_name='patch', + name='state', + field=models.ForeignKey(to='patchwork.State', null=True), + ), + migrations.AlterField( + model_name='patchchangenotification', + name='patch', + field=models.OneToOneField(primary_key=True, + serialize=False, + to='patchwork.Patch'), + ), + migrations.AlterField( + model_name='patchtag', + name='patch', + field=models.ForeignKey(to='patchwork.Patch'), + ), + ] |