Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kubov committed Jul 4, 2024
1 parent 53880e9 commit 3d84f5a
Showing 1 changed file with 106 additions and 20 deletions.
126 changes: 106 additions & 20 deletions pymess/migrations/0030_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
),
]

0 comments on commit 3d84f5a

Please sign in to comment.