diff --git a/netbox/core/apps.py b/netbox/core/apps.py index aeb41bfe3c..c081fb0644 100644 --- a/netbox/core/apps.py +++ b/netbox/core/apps.py @@ -42,13 +42,6 @@ class CoreConfig(AppConfig): # Clear Redis cache on startup in development mode if settings.DEBUG: try: - config = cache.get('config') - config_version = cache.get('config_version') cache.clear() - if config_version: - # Activate the current config revision - # Do not query DB due to apps still initializing - cache.set('config', config, None) - cache.set('config_version', config_version, None) except Exception: pass diff --git a/netbox/core/migrations/0019_configrevision_active.py b/netbox/core/migrations/0019_configrevision_active.py index 5bf25ed5c2..b911aaa53c 100644 --- a/netbox/core/migrations/0019_configrevision_active.py +++ b/netbox/core/migrations/0019_configrevision_active.py @@ -9,16 +9,18 @@ def get_active(apps, schema_editor): version = None revision = None + # Try and get the latest version from cache try: version = cache.get('config_version') except Exception: pass - if version: - revision = ConfigRevision.objects.filter(pk=version).first() - else: + # If there is a version in cache, attempt to set revision to the current version from cache + # If the version in cache does not exist or there is no version, try the lastest revision in the database + if not version or (version and not (revision := ConfigRevision.objects.filter(pk=version).first())): revision = ConfigRevision.objects.order_by('-created').first() + # If there is a revision set, set the active revision if revision: revision.active = True revision.save() diff --git a/netbox/netbox/config/__init__.py b/netbox/netbox/config/__init__.py index 0fd416ddce..f2fdecb338 100644 --- a/netbox/netbox/config/__init__.py +++ b/netbox/netbox/config/__init__.py @@ -79,12 +79,15 @@ class Config: try: # Enforce the creation date as the ordering parameter - if not (revision := ConfigRevision.objects.filter(active=True).first()): - revision = ConfigRevision.objects.order_by('-created').first() + revision = ConfigRevision.objects.get(active=True) + logger.debug(f"Loaded active configuration revision #{revision.pk}") + except (ConfigRevision.DoesNotExist, ConfigRevision.MultipleObjectsReturned): + logger.warning("No active configuration revision found - falling back to most recent") + revision = ConfigRevision.objects.order_by('-created').first() if revision is None: logger.debug("No previous configuration found in database; proceeding with default values") return - logger.debug("Loaded configuration data from database") + logger.debug(f"Using fallback configuration revision #{revision.pk}") except DatabaseError: # The database may not be available yet (e.g. when running a management command) logger.warning("Skipping config initialization (database unavailable)")