|
| 1 | +# Partly generated by Django 4.2.20 on 2025-05-25 15:24 together with manual RunSQL |
| 2 | + |
| 3 | +from django.db import migrations, models |
| 4 | +import django.db.models.deletion |
| 5 | +import pgtrigger.compiler |
| 6 | +import pgtrigger.migrations |
| 7 | + |
| 8 | + |
| 9 | +class Migration(migrations.Migration): |
| 10 | + dependencies = [ |
| 11 | + ("checks", "0018_domaintesttls_caa_records"), |
| 12 | + ] |
| 13 | + |
| 14 | + operations = [ |
| 15 | + # Note db_index is False on the ForeignKey to prevent extra indices that are not needed |
| 16 | + # AutoField has to be primary key in Django, to solve this manually alter SQL this field |
| 17 | + # see https://github.com/django/django/blob/787f3130f751283140fe2be8188eb5299552232d/django/db/models/fields/__init__.py#L2801 |
| 18 | + # The primary key on domain is added in SQL to prevent a _like index |
| 19 | + migrations.CreateModel( |
| 20 | + name="Fame", |
| 21 | + fields=[ |
| 22 | + ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), |
| 23 | + ("domain", models.CharField(max_length=255)), |
| 24 | + ( |
| 25 | + "site_report", |
| 26 | + models.ForeignKey( |
| 27 | + db_index=False, |
| 28 | + null=True, |
| 29 | + on_delete=django.db.models.deletion.CASCADE, |
| 30 | + to="checks.domaintestreport", |
| 31 | + ), |
| 32 | + ), |
| 33 | + ("site_report_timestamp", models.DateTimeField(null=True)), |
| 34 | + ( |
| 35 | + "mail_report", |
| 36 | + models.ForeignKey( |
| 37 | + db_index=False, |
| 38 | + null=True, |
| 39 | + on_delete=django.db.models.deletion.CASCADE, |
| 40 | + to="checks.mailtestreport", |
| 41 | + ), |
| 42 | + ), |
| 43 | + ("mail_report_timestamp", models.DateTimeField(null=True)), |
| 44 | + ], |
| 45 | + ), |
| 46 | + pgtrigger.migrations.AddTrigger( |
| 47 | + model_name="domaintestreport", |
| 48 | + trigger=pgtrigger.compiler.Trigger( |
| 49 | + name="update_fame_on_site_report", |
| 50 | + sql=pgtrigger.compiler.UpsertTriggerSql( |
| 51 | + func=""" |
| 52 | +IF NEW.score IS NULL THEN |
| 53 | + -- DO NOTHING |
| 54 | +ELSIF NEW.score = 100 THEN |
| 55 | + INSERT INTO checks_fame (domain, site_report_id, site_report_timestamp, mail_report_id, mail_report_timestamp) |
| 56 | + VALUES (NEW.domain, NEW.id, NEW.timestamp, NULL, NULL) |
| 57 | + ON CONFLICT (domain) |
| 58 | + DO UPDATE SET site_report_id = NEW.id, site_report_timestamp = NEW.timestamp; |
| 59 | +ELSE |
| 60 | + MERGE INTO ONLY checks_fame c1 |
| 61 | + USING checks_fame c2 ON c1.domain = c2.domain AND c1.domain = NEW.domain |
| 62 | + WHEN NOT MATCHED THEN |
| 63 | + DO NOTHING |
| 64 | + WHEN MATCHED AND c1.mail_report_id IS NOT NULL THEN |
| 65 | + UPDATE SET site_report_id = NULL, site_report_timestamp = NULL |
| 66 | + WHEN MATCHED AND c1.mail_report_id IS NULL THEN |
| 67 | + DELETE; |
| 68 | + END IF; |
| 69 | +RETURN NEW; |
| 70 | +""", |
| 71 | + hash="b4f792b06123914de71b57669c202a19b04e9e9c", |
| 72 | + operation='INSERT OR UPDATE OF "score"', |
| 73 | + pgid="pgtrigger_update_fame_on_site_report_e4fdc", |
| 74 | + table="checks_domaintestreport", |
| 75 | + when="AFTER", |
| 76 | + ), |
| 77 | + ), |
| 78 | + ), |
| 79 | + pgtrigger.migrations.AddTrigger( |
| 80 | + model_name="mailtestreport", |
| 81 | + trigger=pgtrigger.compiler.Trigger( |
| 82 | + name="update_fame_on_mail_report", |
| 83 | + sql=pgtrigger.compiler.UpsertTriggerSql( |
| 84 | + func=""" |
| 85 | +IF NEW.score IS NULL THEN |
| 86 | + -- DO NOTHING |
| 87 | +ELSIF NEW.score = 100 THEN |
| 88 | + INSERT INTO checks_fame (domain, site_report_id, site_report_timestamp, mail_report_id, mail_report_timestamp) |
| 89 | + VALUES (NEW.domain, NULL, NULL, NEW.id, NEW.timestamp) |
| 90 | + ON CONFLICT (domain) |
| 91 | + DO UPDATE SET mail_report_id = NEW.id, mail_report_timestamp = NEW.timestamp; |
| 92 | +ELSE |
| 93 | + MERGE INTO ONLY checks_fame c1 |
| 94 | + USING checks_fame c2 ON c1.domain = c2.domain AND c1.domain = NEW.domain |
| 95 | + WHEN NOT MATCHED THEN |
| 96 | + DO NOTHING |
| 97 | + WHEN MATCHED AND c1.site_report_id IS NOT NULL THEN |
| 98 | + UPDATE SET mail_report_id = NULL, mail_report_timestamp = NULL |
| 99 | + WHEN MATCHED AND c1.site_report_id IS NULL THEN |
| 100 | + DELETE; |
| 101 | + END IF; |
| 102 | +RETURN NEW; |
| 103 | +""", |
| 104 | + hash="707aefc7a83dd041dd815511f1d1cf7e8f84f944", |
| 105 | + operation='INSERT OR UPDATE OF "score"', |
| 106 | + pgid="pgtrigger_update_fame_on_mail_report_b3a27", |
| 107 | + table="checks_mailtestreport", |
| 108 | + when="AFTER", |
| 109 | + ), |
| 110 | + ), |
| 111 | + ), |
| 112 | + migrations.RunSQL( |
| 113 | + sql=[ |
| 114 | + 'ALTER TABLE "checks_fame" DROP CONSTRAINT "checks_fame_pkey";', |
| 115 | + 'ALTER TABLE "checks_fame" ADD PRIMARY KEY ("domain");', |
| 116 | + """ |
| 117 | +WITH |
| 118 | + site_fame AS ( |
| 119 | + SELECT domain, id AS site_report_id, timestamp AS site_report_timestamp FROM ( |
| 120 | + SELECT domain, score, id, timestamp, rank() OVER (PARTITION BY domain ORDER BY id DESC) FROM checks_domaintestreport |
| 121 | + ) alias WHERE rank = 1 AND score = 100), |
| 122 | + mail_fame AS ( |
| 123 | + SELECT domain, id AS mail_report_id, timestamp AS mail_report_timestamp FROM ( |
| 124 | + SELECT domain, score, id, timestamp, rank() OVER (PARTITION BY domain ORDER BY id DESC) FROM checks_mailtestreport |
| 125 | + ) alias WHERE rank = 1 AND score = 100) |
| 126 | +INSERT INTO checks_fame (domain, site_report_id, site_report_timestamp, mail_report_id, mail_report_timestamp) |
| 127 | + SELECT * FROM site_fame FULL OUTER JOIN mail_fame USING (domain); |
| 128 | +""", |
| 129 | + ], |
| 130 | + reverse_sql=[ |
| 131 | + 'DELETE FROM "checks_fame";', |
| 132 | + 'ALTER TABLE "checks_fame" DROP CONSTRAINT "checks_fame_pkey";', |
| 133 | + 'ALTER TABLE "checks_fame" ADD PRIMARY KEY ("id");', |
| 134 | + ], |
| 135 | + state_operations=[ |
| 136 | + migrations.AddConstraint("Fame", models.UniqueConstraint(fields=["domain"], name="checks_fame_pkey")), |
| 137 | + migrations.AddIndex("Fame", models.Index(fields=["domain"], name="checks_fame_pkey")), |
| 138 | + ], |
| 139 | + ), |
| 140 | + ] |
0 commit comments