Add active field on ConfigRevision model.

This commit is contained in:
Daniel Sheppard 2025-09-09 11:49:13 -05:00
parent 9854c66194
commit 42857b15b2
3 changed files with 41 additions and 1 deletions

View File

@ -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'
),
),
]

View File

@ -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

View File

@ -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