0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Split remove_filter_model migration to prevent Postgres errors - fixes #2754

This commit is contained in:
Matt Westcott 2016-06-19 22:57:52 +01:00
parent ec55c5d73c
commit 87e9c63dbf
4 changed files with 84 additions and 56 deletions

View File

@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
def forwards_data_migration(apps, schema_editor):
Rendition = apps.get_model('wagtailimages', 'Rendition')
db_alias = schema_editor.connection.alias
# update doesn't work during migration, we have to do it one by one
for rendition in Rendition.objects.using(db_alias).select_related('filter').all():
rendition.filter2 = rendition.filter.spec
rendition.save()
def reverse_data_migration(apps, schema_editor):
Rendition = apps.get_model('wagtailimages', 'Rendition')
Filter = apps.get_model('wagtailimages', 'Filter')
db_alias = schema_editor.connection.alias
renditions = Rendition.objects.using(db_alias).all()
for rendition in renditions:
# Ensure we don't create the same Filter twice
rendition.filter, _ = Filter.objects.get_or_create(spec=rendition.filter2)
rendition.save()
class Migration(migrations.Migration):
dependencies = [
('wagtailimages', '0013_make_rendition_upload_callable'),
]
operations = [
# We create a new attribute to allow the data migration (filter -> filter2).
migrations.AddField(
model_name='Rendition',
name='filter2',
field=models.CharField(max_length=255, default=''),
preserve_default=False,
),
# Allow NULL for backward operation to work (no temporary default value possible as the `spec` attribute is not null but also unique).
migrations.AlterField('rendition', 'filter', models.ForeignKey(on_delete=models.CASCADE, related_name='+', to='wagtailimages.Filter', null=True)),
# Remove unique constraint to allow the removal of `filter` field.
migrations.AlterUniqueTogether('rendition', set([])),
# Migrate data.
migrations.RunPython(forwards_data_migration, reverse_data_migration),
# We can now delete Filter for good.
migrations.RemoveField(model_name='Rendition', name='filter'),
migrations.DeleteModel('Filter'),
# And we can finally set the new attribute as `filter`.
migrations.RenameField(model_name='Rendition', old_name='filter2', new_name='filter'),
# Bug with mysql and postgresql, we need to add the index after the renaming (https://code.djangoproject.com/ticket/25530).
migrations.AlterField('rendition', 'filter', models.CharField(max_length=255, db_index=True)),
# We can now set back the unique constraint.
migrations.AlterUniqueTogether('rendition', set([('image', 'filter', 'focal_point_key')])),
]

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wagtailimages', '0013_make_rendition_upload_callable'),
]
operations = [
# We create a new attribute to allow the data migration (filter -> filter2).
migrations.AddField(
model_name='Rendition',
name='filter2',
field=models.CharField(max_length=255, default=''),
preserve_default=False,
),
# Allow NULL for backward operation to work (no temporary default value possible as the `spec` attribute is not null but also unique).
migrations.AlterField('rendition', 'filter', models.ForeignKey(on_delete=models.CASCADE, related_name='+', to='wagtailimages.Filter', null=True)),
# Remove unique constraint to allow the removal of `filter` field.
migrations.AlterUniqueTogether('rendition', set([])),
]

View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def forwards_data_migration(apps, schema_editor):
Rendition = apps.get_model('wagtailimages', 'Rendition')
db_alias = schema_editor.connection.alias
# update doesn't work during migration, we have to do it one by one
for rendition in Rendition.objects.using(db_alias).select_related('filter').all():
rendition.filter2 = rendition.filter.spec
rendition.save()
def reverse_data_migration(apps, schema_editor):
Rendition = apps.get_model('wagtailimages', 'Rendition')
Filter = apps.get_model('wagtailimages', 'Filter')
db_alias = schema_editor.connection.alias
renditions = Rendition.objects.using(db_alias).all()
for rendition in renditions:
# Ensure we don't create the same Filter twice
rendition.filter, _ = Filter.objects.get_or_create(spec=rendition.filter2)
rendition.save()
class Migration(migrations.Migration):
dependencies = [
('wagtailimages', '0014_remove_filter_model_1'),
]
operations = [
# Migrate data.
migrations.RunPython(forwards_data_migration, reverse_data_migration),
]

View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wagtailimages', '0015_remove_filter_model_2'),
]
operations = [
# We can now delete Filter for good.
migrations.RemoveField(model_name='Rendition', name='filter'),
migrations.DeleteModel('Filter'),
# And we can finally set the new attribute as `filter`.
migrations.RenameField(model_name='Rendition', old_name='filter2', new_name='filter'),
# Bug with mysql and postgresql, we need to add the index after the renaming (https://code.djangoproject.com/ticket/25530).
migrations.AlterField('rendition', 'filter', models.CharField(max_length=255, db_index=True)),
# We can now set back the unique constraint.
migrations.AlterUniqueTogether('rendition', set([('image', 'filter', 'focal_point_key')])),
]