From fbd1cfdef7e8f788f111686cc096f8fa184ed21b Mon Sep 17 00:00:00 2001 From: Ilya Mukhortov Date: Fri, 9 Oct 2026 23:51:39 +1000 Subject: [PATCH] Collapse notify migrations into a single initial. New installs apply the final schema in one step, including delivery targets and encrypted secrets. Co-authored-by: Cursor --- notify/migrations/0001_initial.py | 16 ++--- notify/migrations/0002_delivery_target.py | 65 ------------------- .../0003_encrypt_delivery_secrets.py | 56 ---------------- 3 files changed, 8 insertions(+), 129 deletions(-) delete mode 100644 notify/migrations/0002_delivery_target.py delete mode 100644 notify/migrations/0003_encrypt_delivery_secrets.py diff --git a/notify/migrations/0001_initial.py b/notify/migrations/0001_initial.py index 9f23d18..e0a120a 100644 --- a/notify/migrations/0001_initial.py +++ b/notify/migrations/0001_initial.py @@ -1,6 +1,7 @@ -# Generated by Django 5.0.6 on 2026-10-09 01:15 +# Generated by Django 5.0.6 on 2026-10-09 13:49 import django.db.models.deletion +import notify.fields import notify.models import uuid from django.conf import settings @@ -81,8 +82,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.BigAutoField(primary_key=True, serialize=False)), ('delivery_channel', models.CharField(choices=[('email', 'Email'), ('telegram', 'Telegram'), ('browser', 'Browser'), ('max', 'Max')], max_length=20)), - ('delivery_address', models.CharField(blank=True, max_length=255, null=True)), - ('delivery_data', models.JSONField(blank=True, default=None, null=True)), + ('delivery_target', models.JSONField(blank=True, default=None, null=True)), ('delivery_status', models.CharField(choices=[('UNSENT', 'Unsent'), ('SENT', 'Sent'), ('FAILED', 'Failed')], default='UNSENT', max_length=10)), ('sent_at', models.DateTimeField(blank=True, null=True)), ('attempts', models.PositiveSmallIntegerField(default=0)), @@ -124,8 +124,8 @@ class Migration(migrations.Migration): ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), ('purpose', models.CharField(max_length=100)), ('channel', models.CharField(choices=[('email', 'Email'), ('telegram', 'Telegram'), ('browser', 'Browser'), ('max', 'Max')], max_length=20)), - ('target', models.CharField(max_length=255)), - ('delivery_data', models.JSONField(blank=True, default=None, null=True)), + ('target_key', models.CharField(max_length=255)), + ('delivery_target', models.JSONField(blank=True, default=None, null=True)), ('code_hash', models.CharField(max_length=128)), ('expires_at', models.DateTimeField(db_index=True)), ('attempts', models.PositiveSmallIntegerField(default=0)), @@ -144,7 +144,7 @@ class Migration(migrations.Migration): ('host', models.CharField(max_length=100)), ('port', models.IntegerField(default=587)), ('username', models.CharField(blank=True, default='', max_length=100)), - ('password', models.CharField(blank=True, default='', max_length=100)), + ('password', notify.fields.EncryptedSecretField(blank=True, default='', max_length=100)), ('from_email', models.CharField(blank=True, default='', max_length=100)), ('test_email', models.EmailField(blank=True, default='', max_length=100)), ('use_ssl', models.BooleanField(default=False)), @@ -156,7 +156,7 @@ class Migration(migrations.Migration): name='DeliveryTokenConfig', fields=[ ('deliveryconfig_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='notify.deliveryconfig')), - ('token', models.CharField(blank=True, max_length=500)), + ('token', notify.fields.EncryptedSecretField(blank=True, max_length=500)), ], bases=('notify.deliveryconfig',), ), @@ -231,6 +231,6 @@ class Migration(migrations.Migration): ), migrations.AddIndex( model_name='verificationcode', - index=models.Index(fields=['purpose', 'channel', 'target', 'created_at'], name='idx_code_lookup'), + index=models.Index(fields=['purpose', 'channel', 'target_key', 'created_at'], name='idx_code_lookup'), ), ] diff --git a/notify/migrations/0002_delivery_target.py b/notify/migrations/0002_delivery_target.py deleted file mode 100644 index c55025a..0000000 --- a/notify/migrations/0002_delivery_target.py +++ /dev/null @@ -1,65 +0,0 @@ -from django.db import migrations, models - - -def forwards(apps, schema_editor): - NotificationRecipient = apps.get_model("notify", "NotificationRecipient") - for recipient in NotificationRecipient.objects.all().iterator(): - if recipient.delivery_channel == "email": - target = {"email": recipient.delivery_address} if recipient.delivery_address else None - else: - target = recipient.delivery_data - if target is not None: - recipient.delivery_target = target - recipient.save(update_fields=["delivery_target"]) - - -def backwards(apps, schema_editor): - NotificationRecipient = apps.get_model("notify", "NotificationRecipient") - for recipient in NotificationRecipient.objects.exclude(delivery_target=None).iterator(): - if recipient.delivery_channel == "email": - recipient.delivery_address = recipient.delivery_target.get("email") - else: - recipient.delivery_data = recipient.delivery_target - recipient.save(update_fields=["delivery_address", "delivery_data"]) - - -class Migration(migrations.Migration): - - dependencies = [ - ("notify", "0001_initial"), - ] - - operations = [ - migrations.AddField( - model_name="notificationrecipient", - name="delivery_target", - field=models.JSONField(blank=True, default=None, null=True), - ), - migrations.RunPython(forwards, backwards), - migrations.RemoveField( - model_name="notificationrecipient", - name="delivery_address", - ), - migrations.RemoveField( - model_name="notificationrecipient", - name="delivery_data", - ), - migrations.RemoveIndex( - model_name="verificationcode", - name="idx_code_lookup", - ), - migrations.RenameField( - model_name="verificationcode", - old_name="target", - new_name="target_key", - ), - migrations.RenameField( - model_name="verificationcode", - old_name="delivery_data", - new_name="delivery_target", - ), - migrations.AddIndex( - model_name="verificationcode", - index=models.Index(fields=["purpose", "channel", "target_key", "created_at"], name="idx_code_lookup"), - ), - ] diff --git a/notify/migrations/0003_encrypt_delivery_secrets.py b/notify/migrations/0003_encrypt_delivery_secrets.py deleted file mode 100644 index 82ada47..0000000 --- a/notify/migrations/0003_encrypt_delivery_secrets.py +++ /dev/null @@ -1,56 +0,0 @@ -# Generated by Django 5.0.6 on 2026-10-09 13:43 - -import notify.fields -from django.db import migrations - - -def encrypt_secrets(apps, schema_editor): - email_config = apps.get_model("notify", "DeliveryEmailConfig") - token_config = apps.get_model("notify", "DeliveryTokenConfig") - for config in email_config.objects.all(): - config.save(update_fields=["password"]) - for config in token_config.objects.all(): - config.save(update_fields=["token"]) - - -def decrypt_secrets(apps, schema_editor): - email_config = apps.get_model("notify", "DeliveryEmailConfig") - token_config = apps.get_model("notify", "DeliveryTokenConfig") - email_table = email_config._meta.db_table - token_table = token_config._meta.db_table - email_pk = email_config._meta.pk.column - token_pk = token_config._meta.pk.column - with schema_editor.connection.cursor() as cursor: - for config in email_config.objects.all(): - prepared = email_config._meta.pk.get_db_prep_value(config.pk, schema_editor.connection) - cursor.execute( - f"UPDATE {email_table} SET password = %s WHERE {email_pk} = %s", - [config.password or "", prepared], - ) - for config in token_config.objects.all(): - prepared = token_config._meta.pk.get_db_prep_value(config.pk, schema_editor.connection) - cursor.execute( - f"UPDATE {token_table} SET token = %s WHERE {token_pk} = %s", - [config.token or "", prepared], - ) - - -class Migration(migrations.Migration): - - dependencies = [ - ('notify', '0002_delivery_target'), - ] - - operations = [ - migrations.AlterField( - model_name='deliveryemailconfig', - name='password', - field=notify.fields.EncryptedSecretField(blank=True, default='', max_length=100), - ), - migrations.AlterField( - model_name='deliverytokenconfig', - name='token', - field=notify.fields.EncryptedSecretField(blank=True, max_length=500), - ), - migrations.RunPython(encrypt_secrets, decrypt_secrets), - ]