From 3d84f5a441d08444ab61b5ff709bc5b87c3c37a2 Mon Sep 17 00:00:00 2001 From: Peter Kubov Date: Thu, 4 Jul 2024 13:04:49 +0200 Subject: [PATCH] tmp --- pymess/migrations/0030_migration.py | 126 +++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 20 deletions(-) diff --git a/pymess/migrations/0030_migration.py b/pymess/migrations/0030_migration.py index 8f1ad45..57b6d42 100644 --- a/pymess/migrations/0030_migration.py +++ b/pymess/migrations/0030_migration.py @@ -24,7 +24,7 @@ def create_template_ids_from_slugs(apps, schema_editor): def migrate_back_template(template, relation): for obj in template.objects.all(): - query = relation.objects.filter(template_slug=obj.slug) + query = relation.objects.filter(template_old=obj.slug) migration_print(f"Migrating {relation}.template={obj.id} from slug={obj.slug} ({query.count()} objects)") query.update(template=obj.id) @@ -48,42 +48,93 @@ class Migration(migrations.Migration): ] operations = [ - # 1. Drop the foreign key - we can recreate it from `template_slug`. + # 1. Drop the foreign key constraint. - migrations.RemoveField( + migrations.AlterField( + model_name='dialermessage', + name='template', + field=models.SlugField(blank=True, max_length=100, null=True), + ), + migrations.AlterField( model_name='dialertemplatedisallowedobject', name='template', + field=models.SlugField(blank=True, max_length=100, null=True), ), - migrations.RemoveField( + migrations.AlterField( + model_name='emailmessage', + name='template', + field=models.SlugField(blank=True, max_length=100, null=True), + ), + migrations.AlterField( model_name='emailtemplateattachment', name='template', + field=models.SlugField(blank=True, max_length=100, null=True), ), - migrations.RemoveField( + migrations.AlterField( model_name='emailtemplatedisallowedobject', name='template', + field=models.SlugField(blank=True, max_length=100, null=True), ), - migrations.RemoveField( + migrations.AlterField( + model_name='outputsmsmessage', + name='template', + field=models.SlugField(blank=True, max_length=100, null=True), + ), + migrations.AlterField( + model_name='pushnotificationmessage', + name='template', + field=models.SlugField(blank=True, max_length=100, null=True), + ), + migrations.AlterField( model_name='smstemplatedisallowedobject', name='template', + field=models.SlugField(blank=True, max_length=100, null=True), ), - migrations.RemoveField( + + # 2. Rename 'template' -> 'template_old' + + migrations.RenameField( model_name='dialermessage', - name='template', + old_name='template', + new_name='template_old', ), - migrations.RemoveField( + migrations.RenameField( + model_name='dialertemplatedisallowedobject', + old_name='template', + new_name='template_old', + ), + migrations.RenameField( model_name='emailmessage', - name='template', + old_name='template', + new_name='template_old', ), - migrations.RemoveField( + migrations.RenameField( + model_name='emailtemplateattachment', + old_name='template', + new_name='template_old', + ), + migrations.RenameField( + model_name='emailtemplatedisallowedobject', + old_name='template', + new_name='template_old', + ), + migrations.RenameField( model_name='outputsmsmessage', - name='template', + old_name='template', + new_name='template_old', ), - migrations.RemoveField( + migrations.RenameField( model_name='pushnotificationmessage', - name='template', + old_name='template', + new_name='template_old', + ), + migrations.RenameField( + model_name='smstemplatedisallowedobject', + old_name='template', + new_name='template_old', ), - # 2. Slug is no longer a primary key. + # 3. Slug is no longer a primary key. migrations.AlterField( model_name='dialertemplate', @@ -106,7 +157,7 @@ class Migration(migrations.Migration): field=models.SlugField(editable=False, max_length=100), ), - # 3. We create a new primary key -> AutoField + # 4. We create a new primary key -> AutoField migrations.AddField( model_name='dialertemplate', @@ -133,7 +184,7 @@ class Migration(migrations.Migration): preserve_default=False, ), - # 4. We create a new locale field + # 5. We create a new locale field migrations.AddField( model_name='dialertemplate', @@ -156,7 +207,7 @@ class Migration(migrations.Migration): field=models.CharField(blank=True, editable=False, max_length=10, null=True), ), - # 4. slug + locale are unique. + # 5. slug + locale are unique. migrations.AlterUniqueTogether( name='dialertemplate', @@ -175,7 +226,7 @@ class Migration(migrations.Migration): unique_together={('slug', 'locale')}, ), - # 5. We need can model `template` relationship with new ids. + # 6. We need to create removed `template` fields and return the foreign key constraint. migrations.AddField( model_name='dialermessage', @@ -218,7 +269,42 @@ class Migration(migrations.Migration): field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='disallowed_objects', to='pymess.smstemplate'), ), - # 6. We need to update `template` field with newly created primary keys. + # 7. We need to update `template` field with newly created primary keys. migrations.RunPython(create_template_ids_from_slugs), + + # 8. We can safely remove old `template_old` field. + + migrations.RemoveField( + model_name='dialertemplatedisallowedobject', + name='template_old', + ), + migrations.RemoveField( + model_name='emailtemplateattachment', + name='template_old', + ), + migrations.RemoveField( + model_name='emailtemplatedisallowedobject', + name='template_old', + ), + migrations.RemoveField( + model_name='smstemplatedisallowedobject', + name='template_old', + ), + migrations.RemoveField( + model_name='dialermessage', + name='template_old', + ), + migrations.RemoveField( + model_name='emailmessage', + name='template_old', + ), + migrations.RemoveField( + model_name='outputsmsmessage', + name='template_old', + ), + migrations.RemoveField( + model_name='pushnotificationmessage', + name='template_old', + ), ]