Skip to content

Commit 89fa2e1

Browse files
author
Lars van Rhijn
committed
[migrations] Added a custom migration for migrating old choices field to new choices field
1 parent 941981f commit 89fa2e1

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Generated by Django 5.2.7 on 2025-10-06 07:15
2+
3+
from django.db import migrations
4+
5+
def alter_choices_certificates(apps, schema_editor):
6+
"""Alter old digest choices to new digest choices (also mentioning the encryption algorithm)."""
7+
Ca = apps.get_model("django_x509", "Ca")
8+
Ca.objects.filter(digest="sha1").update(digest="sha1WithRSAEncryption")
9+
Ca.objects.filter(digest="sha224").update(digest="sha224WithRSAEncryption")
10+
Ca.objects.filter(digest="sha256").update(digest="sha256WithRSAEncryption")
11+
Ca.objects.filter(digest="sha384").update(digest="sha384WithRSAEncryption")
12+
Ca.objects.filter(digest="sha512").update(digest="sha512WithRSAEncryption")
13+
14+
Cert = apps.get_model("django_x509", "Cert")
15+
Cert.objects.filter(digest="sha1").update(digest="sha1WithRSAEncryption")
16+
Cert.objects.filter(digest="sha224").update(digest="sha224WithRSAEncryption")
17+
Cert.objects.filter(digest="sha256").update(digest="sha256WithRSAEncryption")
18+
Cert.objects.filter(digest="sha384").update(digest="sha384WithRSAEncryption")
19+
Cert.objects.filter(digest="sha512").update(digest="sha512WithRSAEncryption")
20+
21+
22+
def alter_choices_certificate_reverse(apps, schema_editor):
23+
"""Reverse the operations of alter_choices_certificates."""
24+
Ca = apps.get_model("django_x509", "Ca")
25+
Ca.objects.filter(digest="sha1WithRSAEncryption").update(digest="sha1")
26+
Ca.objects.filter(digest="sha224WithRSAEncryption").update(digest="sha224")
27+
Ca.objects.filter(digest="sha256WithRSAEncryption").update(digest="sha256")
28+
Ca.objects.filter(digest="sha384WithRSAEncryption").update(digest="sha384")
29+
Ca.objects.filter(digest="sha512WithRSAEncryption").update(digest="sha512")
30+
31+
Cert = apps.get_model("django_x509", "Cert")
32+
Cert.objects.filter(digest="sha1WithRSAEncryption").update(digest="sha1")
33+
Cert.objects.filter(digest="sha224WithRSAEncryption").update(digest="sha224")
34+
Cert.objects.filter(digest="sha256WithRSAEncryption").update(digest="sha256")
35+
Cert.objects.filter(digest="sha384WithRSAEncryption").update(digest="sha384")
36+
Cert.objects.filter(digest="sha512WithRSAEncryption").update(digest="sha512")
37+
38+
39+
class Migration(migrations.Migration):
40+
41+
dependencies = [
42+
("django_x509", "0010_alter_ca_digest_alter_cert_digest"),
43+
]
44+
45+
operations = [
46+
migrations.RunPython(
47+
alter_choices_certificates,
48+
reverse_code=alter_choices_certificate_reverse
49+
)
50+
]

0 commit comments

Comments
 (0)