Skip to content

Commit e175528

Browse files
committed
feat(migrations): Add prefix assignment migration for ACL rules
Introduces a data migration to copy source and destination prefix IDs to the newly added GenericForeignKey fields in ACLStandardRule and ACLExtendedRule. Ensures existing prefix assignments are preserved during schema updates.
1 parent 57eb10b commit e175528

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

netbox_acls/migrations/0005_acl_rule_source_and_destination_objects.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
from django.db import migrations, models
33

44

5+
def copy_prefix_assignments(apps, schema_editor):
6+
"""
7+
Copy Source and Destination Prefix ForeignKey IDs to the GenericForeignKey
8+
fields.
9+
"""
10+
ContentType = apps.get_model("contenttypes", "ContentType")
11+
Prefix = apps.get_model("ipam", "Prefix")
12+
ACLStandardRule = apps.get_model("netbox_acls", "ACLStandardRule")
13+
ACLExtendedRule = apps.get_model("netbox_acls", "ACLExtendedRule")
14+
15+
ACLStandardRule.objects.filter(_source_prefix__isnull=False).update(
16+
source_type=ContentType.objects.get_for_model(Prefix),
17+
source_id=models.F("_source_prefix_id"),
18+
)
19+
ACLExtendedRule.objects.filter(_source_prefix__isnull=False).filter(_destination_prefix__isnull=False).update(
20+
source_type=ContentType.objects.get_for_model(Prefix),
21+
source_id=models.F("_source_prefix_id"),
22+
destination_type=ContentType.objects.get_for_model(Prefix),
23+
destination_id=models.F("_destination_prefix_id"),
24+
)
25+
26+
527
class Migration(migrations.Migration):
628
dependencies = [
729
("contenttypes", "0002_remove_content_type_name"),
@@ -226,4 +248,6 @@ class Migration(migrations.Migration):
226248
model_name="aclstandardrule",
227249
index=models.Index(fields=["source_type", "source_id"], name="netbox_acls_source__01d2fa_idx"),
228250
),
251+
# Copy over existing Prefix assignments
252+
migrations.RunPython(code=copy_prefix_assignments, reverse_code=migrations.RunPython.noop),
229253
]

0 commit comments

Comments
 (0)