diff --git a/netbox/core/migrations/0016_configrevision_active.py b/netbox/core/migrations/0016_configrevision_active.py index 1290728982..a97bae761c 100644 --- a/netbox/core/migrations/0016_configrevision_active.py +++ b/netbox/core/migrations/0016_configrevision_active.py @@ -3,6 +3,27 @@ from django.db import migrations, models +def get_active(apps, schema_editor): + from django.core.cache import cache + ConfigRevision = apps.get_model('core', 'ConfigRevision') + version = None + revision = None + + try: + version = cache.get('config_version') + except Exception: + pass + + if version: + revision = ConfigRevision.objects.filter(pk=version).first() + else: + revision = ConfigRevision.objects.order_by('-created').first() + + if revision: + revision.active = True + revision.save() + + class Migration(migrations.Migration): dependencies = [ @@ -15,6 +36,7 @@ class Migration(migrations.Migration): name='active', field=models.BooleanField(default=False), ), + migrations.RunPython(code=get_active, reverse_code=migrations.RunPython.noop), migrations.AddConstraint( model_name='configrevision', constraint=models.UniqueConstraint( diff --git a/netbox/core/models/config.py b/netbox/core/models/config.py index 6a0f922aa5..4a779e7673 100644 --- a/netbox/core/models/config.py +++ b/netbox/core/models/config.py @@ -78,4 +78,4 @@ class ConfigRevision(models.Model): @property def is_active(self): - return cache.get('config_version') == self.pk + return self.active