mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-23 12:46:23 +00:00
Add active field on ConfigRevision model.
This commit is contained in:
parent
9854c66194
commit
42857b15b2
24
netbox/core/migrations/0016_configrevision_active.py
Normal file
24
netbox/core/migrations/0016_configrevision_active.py
Normal 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'
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -14,6 +14,9 @@ class ConfigRevision(models.Model):
|
|||||||
"""
|
"""
|
||||||
An atomic revision of NetBox's configuration.
|
An atomic revision of NetBox's configuration.
|
||||||
"""
|
"""
|
||||||
|
active = models.BooleanField(
|
||||||
|
default=False
|
||||||
|
)
|
||||||
created = models.DateTimeField(
|
created = models.DateTimeField(
|
||||||
verbose_name=_('created'),
|
verbose_name=_('created'),
|
||||||
auto_now_add=True
|
auto_now_add=True
|
||||||
@ -35,6 +38,13 @@ class ConfigRevision(models.Model):
|
|||||||
ordering = ['-created']
|
ordering = ['-created']
|
||||||
verbose_name = _('config revision')
|
verbose_name = _('config revision')
|
||||||
verbose_name_plural = _('config revisions')
|
verbose_name_plural = _('config revisions')
|
||||||
|
constraints = [
|
||||||
|
models.UniqueConstraint(
|
||||||
|
fields=('active',),
|
||||||
|
condition=models.Q(actvive=True),
|
||||||
|
name='unique_active_config_revision',
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if not self.pk:
|
if not self.pk:
|
||||||
@ -59,6 +69,11 @@ class ConfigRevision(models.Model):
|
|||||||
"""
|
"""
|
||||||
cache.set('config', self.data, None)
|
cache.set('config', self.data, None)
|
||||||
cache.set('config_version', self.pk, 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
|
activate.alters_data = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@ -79,7 +79,8 @@ class Config:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Enforce the creation date as the ordering parameter
|
# 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:
|
if revision is None:
|
||||||
logger.debug("No previous configuration found in database; proceeding with default values")
|
logger.debug("No previous configuration found in database; proceeding with default values")
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user