From 2b738f4129e39f4d097b3caa2086af345c5397dc Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Mon, 15 Sep 2025 16:31:04 -0500 Subject: [PATCH] Revise config initialization, remove unneeded cache setting, update migration logic and add comments * Revise config initialization to be more explicit * Remove old gating of cache.clear() * Add comments to migration * Clean up migration --- netbox/core/apps.py | 7 ------- netbox/core/migrations/0019_configrevision_active.py | 8 +++++--- netbox/netbox/config/__init__.py | 9 ++++++--- 3 files changed, 11 insertions(+), 13 deletions(-) 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)")