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
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

View File

@ -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()

View File

@ -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)")