Replace global IntegerKeyValueSetting with per-view CatalogZone model

Track the catalog zone SOA serial per view instead of a single global value.
The new CatalogZone model has a 1:1 FK to netbox_dns.models.View and its own
soa_serial field; each view's catalog zone now increments independently. The
existing global serial is preserved during migration by copying it into every
view's new CatalogZone row.

Change CatalogZoneMemberIdentifier.name from globally unique to unique per
catalog zone (UniqueConstraint(name, catalog_zone)) and add a catalog_zone FK
to enforce it.

Remove the dns-settings management command, which was a generic interface over
the now-removed IntegerKeyValueSetting model.

Increment both catalog zone serials when a zone moves between views: the old
view's catalog zone loses a member and the new view's gains one, so both must
be bumped. Also refresh the member identifier's catalog_zone FK on a view move
so it does not go stale. The pre_save handler now captures _old_view_id and the
post_save handler detects view changes, increments the old view's serial inside
on_commit (before the new view's), and re-creates the identifier.

AI generated, code manually verified.
This commit is contained in:
sven committed 2026-08-17 10:02:59 +02:00
1 parent 4ad110ca53
commit 9f5e40ae48
17 files changed
+429 -240

No files matched your search

@@ -0,0 +1,134 @@
import django.db.models.deletion
from django.db import migrations, models
def copy_serials_and_link_identifiers(apps, schema_editor):
"""Seed one CatalogZone per view from the old global serial and link identifiers."""
IntegerKeyValueSetting = apps.get_model("netbox_dns_bridge", "IntegerKeyValueSetting")
CatalogZone = apps.get_model("netbox_dns_bridge", "CatalogZone")
CatalogZoneMemberIdentifier = apps.get_model(
"netbox_dns_bridge", "CatalogZoneMemberIdentifier"
)
# Preserve the current global serial; default to 1 when never initialized.
seed_serial = 1
kv = IntegerKeyValueSetting.objects.filter(key="catalog-zone-soa-serial").first()
if kv is not None:
seed_serial = kv.value
# Create a CatalogZone for every view that currently has at least one
# member identifier, seeding each with the old global serial.
view_ids = set(
CatalogZoneMemberIdentifier.objects.exclude(zone__view__isnull=True)
.values_list("zone__view_id", flat=True)
)
view_id_to_catalog_zone = {}
for view_id in view_ids:
cz, _ = CatalogZone.objects.get_or_create(
view_id=view_id, defaults={"soa_serial": seed_serial}
)
view_id_to_catalog_zone[view_id] = cz
# Link each identifier to the CatalogZone matching its zone's view.
for ident in CatalogZoneMemberIdentifier.objects.select_related("zone__view"):
cz = view_id_to_catalog_zone.get(ident.zone.view_id)
if cz is None:
# Defensive: identifier existed without a resolvable view. Create
# the CatalogZone on demand so the NOT NULL constraint can be set.
cz, _ = CatalogZone.objects.get_or_create(
view_id=ident.zone.view_id, defaults={"soa_serial": seed_serial}
)
view_id_to_catalog_zone[ident.zone.view_id] = cz
ident.catalog_zone = cz
ident.save(update_fields=["catalog_zone"])
def reverse_migration(apps, schema_editor):
"""Best-effort reverse: restore the KV row and null the FK."""
IntegerKeyValueSetting = apps.get_model("netbox_dns_bridge", "IntegerKeyValueSetting")
CatalogZone = apps.get_model("netbox_dns_bridge", "CatalogZone")
CatalogZoneMemberIdentifier = apps.get_model(
"netbox_dns_bridge", "CatalogZoneMemberIdentifier"
)
# Pick a representative serial (first catalog zone) to write back.
first = CatalogZone.objects.order_by("pk").first()
serial = first.soa_serial if first is not None else 1
IntegerKeyValueSetting.objects.update_or_create(
key="catalog-zone-soa-serial", defaults={"value": serial}
)
CatalogZoneMemberIdentifier.objects.update(catalog_zone=None)
class Migration(migrations.Migration):
dependencies = [
("netbox_dns", "0030_dnsseckeytemplate_comments_dnsseckeytemplate_owner_and_more"),
("netbox_dns_bridge", "0003_integerkeyvaluesetting_key_unique"),
]
operations = [
migrations.CreateModel(
name="CatalogZone",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False
),
),
("soa_serial", models.IntegerField(default=1)),
(
"view",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="catalog_zone",
to="netbox_dns.view",
),
),
],
options={
"ordering": ("view__name",),
},
),
migrations.AddField(
model_name="catalogzonememberidentifier",
name="catalog_zone",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="member_identifiers",
to="netbox_dns_bridge.catalogzone",
),
),
migrations.AlterField(
model_name="catalogzonememberidentifier",
name="name",
field=models.CharField(max_length=26),
),
migrations.RunPython(
copy_serials_and_link_identifiers,
reverse_code=reverse_migration,
),
migrations.AlterField(
model_name="catalogzonememberidentifier",
name="catalog_zone",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="member_identifiers",
to="netbox_dns_bridge.catalogzone",
),
),
migrations.AddConstraint(
model_name="catalogzonememberidentifier",
constraint=models.UniqueConstraint(
fields=("name", "catalog_zone"),
name="unique_name_per_catalog_zone",
),
),
migrations.DeleteModel(
name="IntegerKeyValueSetting",
),
]