From 42857b15b270974c1f4046797c0bf6047b489204 Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Tue, 9 Sep 2025 11:49:13 -0500 Subject: [PATCH] Add active field on ConfigRevision model. --- .../migrations/0016_configrevision_active.py | 24 +++++++++++++++++++ netbox/core/models/config.py | 15 ++++++++++++ netbox/netbox/config/__init__.py | 3 ++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 netbox/core/migrations/0016_configrevision_active.py diff --git a/netbox/core/migrations/0016_configrevision_active.py b/netbox/core/migrations/0016_configrevision_active.py new file mode 100644 index 00000000000..12907289824 --- /dev/null +++ b/netbox/core/migrations/0016_configrevision_active.py @@ -0,0 +1,24 @@ +# Generated by Django 5.2.5 on 2025-09-09 16:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0015_remove_redundant_indexes'), + ] + + operations = [ + migrations.AddField( + model_name='configrevision', + name='active', + field=models.BooleanField(default=False), + ), + migrations.AddConstraint( + model_name='configrevision', + constraint=models.UniqueConstraint( + condition=models.Q(('actvive', True)), fields=('active',), name='unique_active_config_revision' + ), + ), + ] diff --git a/netbox/core/models/config.py b/netbox/core/models/config.py index b2381ae401d..6a0f922aa56 100644 --- a/netbox/core/models/config.py +++ b/netbox/core/models/config.py @@ -14,6 +14,9 @@ class ConfigRevision(models.Model): """ An atomic revision of NetBox's configuration. """ + active = models.BooleanField( + default=False + ) created = models.DateTimeField( verbose_name=_('created'), auto_now_add=True @@ -35,6 +38,13 @@ class ConfigRevision(models.Model): ordering = ['-created'] verbose_name = _('config revision') verbose_name_plural = _('config revisions') + constraints = [ + models.UniqueConstraint( + fields=('active',), + condition=models.Q(actvive=True), + name='unique_active_config_revision', + ) + ] def __str__(self): if not self.pk: @@ -59,6 +69,11 @@ class ConfigRevision(models.Model): """ cache.set('config', self.data, None) cache.set('config_version', self.pk, None) + + # Set all instances of ConfigRevision to false and set this instance to true + self.objects.all().update(active=True) + self.objects.get(pk=self.pk).update(active=True) + activate.alters_data = True @property diff --git a/netbox/netbox/config/__init__.py b/netbox/netbox/config/__init__.py index 0e6ee5b2d67..0fd416ddcea 100644 --- a/netbox/netbox/config/__init__.py +++ b/netbox/netbox/config/__init__.py @@ -79,7 +79,8 @@ class Config: try: # Enforce the creation date as the ordering parameter - revision = ConfigRevision.objects.order_by('-created').first() + if not (revision := ConfigRevision.objects.filter(active=True).first()): + revision = ConfigRevision.objects.order_by('-created').first() if revision is None: logger.debug("No previous configuration found in database; proceeding with default values") return