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

+63 -24
View File
@@ -12,7 +12,7 @@ from django.test.testcases import TestCase as _DjangoTestCase
captureOnCommitCallbacks = _DjangoTestCase.captureOnCommitCallbacks
from netbox_dns_bridge.models import (
IntegerKeyValueSetting,
CatalogZone,
CatalogZoneMemberIdentifier,
)
@@ -42,13 +42,13 @@ class TestZonePostSaveCreate:
def test_increments_soa_serial_on_commit(self, make_zone, catz_serial):
with captureOnCommitCallbacks(execute=True):
zone = make_zone(name="serial-inc.test")
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial + 1
obj = CatalogZone.objects.get(view=zone.view)
assert obj.soa_serial == catz_serial + 1
def test_does_not_increment_without_on_commit(self, make_zone, catz_serial):
make_zone(name="no-commit.test")
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial
zone = make_zone(name="no-commit.test")
obj = CatalogZone.objects.get(view=zone.view)
assert obj.soa_serial == catz_serial
def test_schedule_client_notify_called_when_enabled(self, make_zone, patch_settings):
patch_settings({"notify_clients": True})
@@ -114,24 +114,63 @@ class TestZonePostSaveUpdate:
with captureOnCommitCallbacks(execute=True):
zone.soa_serial = zone.soa_serial + 1
zone.save()
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial + 1
obj = CatalogZone.objects.get(view=zone.view)
assert obj.soa_serial == catz_serial + 1
@pytest.mark.django_db
class TestZonePostDelete:
def test_increments_soa_serial_on_commit_on_delete(self, make_zone, catz_serial):
zone = make_zone(name="delete-serial.test")
view = zone.view
with captureOnCommitCallbacks(execute=True):
zone.delete()
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial + 1
obj = CatalogZone.objects.get(view=view)
assert obj.soa_serial == catz_serial + 1
def test_does_not_increment_without_on_commit(self, make_zone, catz_serial):
zone = make_zone(name="delete-no-commit.test")
view = zone.view
zone.delete()
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial
obj = CatalogZone.objects.get(view=view)
assert obj.soa_serial == catz_serial
@pytest.mark.django_db
class TestZoneViewChange:
def test_increments_both_catalog_zone_serials(self, make_zone, make_view, catz_serial):
view_a = make_view(name="view-a")
view_b = make_view(name="view-b")
# view_b has no zones yet, so no CatalogZone was auto-created for it.
CatalogZone.objects.create(view=view_b, soa_serial=5)
zone = make_zone(name="move.test", view=view_a)
serial_a_before = CatalogZone.objects.get(view=view_a).soa_serial
serial_b_before = CatalogZone.objects.get(view=view_b).soa_serial
with captureOnCommitCallbacks(execute=True):
zone.view = view_b
zone.save()
serial_a_after = CatalogZone.objects.get(view=view_a).soa_serial
serial_b_after = CatalogZone.objects.get(view=view_b).soa_serial
assert serial_a_after == serial_a_before + 1
assert serial_b_after == serial_b_before + 1
def test_updates_identifier_catalog_zone_fk(self, make_zone, make_view):
view_a = make_view(name="fk-view-a")
view_b = make_view(name="fk-view-b")
zone = make_zone(name="fk-move.test", view=view_a)
ident = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert ident.catalog_zone == CatalogZone.objects.get(view=view_a)
zone.view = view_b
zone.save()
ident.refresh_from_db()
assert ident.catalog_zone == CatalogZone.objects.get(view=view_b)
@pytest.mark.django_db
@@ -141,9 +180,9 @@ class TestSignalDedup:
from django.contrib.auth import get_user_model
zone = make_zone(name="dedup.test")
original_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
original_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
User = get_user_model()
user = User(username="dedup-user", is_superuser=True)
@@ -163,16 +202,16 @@ class TestSignalDedup:
zone.soa_serial = zone.soa_serial + 2
zone.save()
new_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
new_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
assert new_serial == original_serial + 1
def test_no_dedup_without_request(self, make_zone):
zone = make_zone(name="nodedup.test")
original_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
original_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
with captureOnCommitCallbacks(execute=True):
zone.soa_serial = zone.soa_serial + 1
@@ -181,9 +220,9 @@ class TestSignalDedup:
zone.soa_serial = zone.soa_serial + 2
zone.save()
new_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
new_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
assert new_serial == original_serial + 2