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
This commit is contained in:
Daniel Sheppard 2025-09-15 16:31:04 -05:00
parent 3fb8e8f254
commit 2b738f4129
3 changed files with 11 additions and 13 deletions

View File

@ -42,13 +42,6 @@ class CoreConfig(AppConfig):
# Clear Redis cache on startup in development mode # Clear Redis cache on startup in development mode
if settings.DEBUG: if settings.DEBUG:
try: try:
config = cache.get('config')
config_version = cache.get('config_version')
cache.clear() 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: except Exception:
pass pass

View File

@ -9,16 +9,18 @@ def get_active(apps, schema_editor):
version = None version = None
revision = None revision = None
# Try and get the latest version from cache
try: try:
version = cache.get('config_version') version = cache.get('config_version')
except Exception: except Exception:
pass pass
if version: # If there is a version in cache, attempt to set revision to the current version from cache
revision = ConfigRevision.objects.filter(pk=version).first() # If the version in cache does not exist or there is no version, try the lastest revision in the database
else: if not version or (version and not (revision := ConfigRevision.objects.filter(pk=version).first())):
revision = ConfigRevision.objects.order_by('-created').first() revision = ConfigRevision.objects.order_by('-created').first()
# If there is a revision set, set the active revision
if revision: if revision:
revision.active = True revision.active = True
revision.save() revision.save()

View File

@ -79,12 +79,15 @@ class Config:
try: try:
# Enforce the creation date as the ordering parameter # Enforce the creation date as the ordering parameter
if not (revision := ConfigRevision.objects.filter(active=True).first()): revision = ConfigRevision.objects.get(active=True)
revision = ConfigRevision.objects.order_by('-created').first() 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: 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
logger.debug("Loaded configuration data from database") logger.debug(f"Using fallback configuration revision #{revision.pk}")
except DatabaseError: except DatabaseError:
# The database may not be available yet (e.g. when running a management command) # The database may not be available yet (e.g. when running a management command)
logger.warning("Skipping config initialization (database unavailable)") logger.warning("Skipping config initialization (database unavailable)")