Fixes #21011: Avoid updating database when loading active ConfigRevision

This commit is contained in:
Jeremy Stretch
2025-12-22 11:00:04 -05:00
parent f7219e0672
commit f67cc47def
2 changed files with 13 additions and 10 deletions

View File

@@ -63,16 +63,20 @@ class ConfigRevision(models.Model):
return reverse('core:config') # Default config view return reverse('core:config') # Default config view
return reverse('core:configrevision', args=[self.pk]) return reverse('core:configrevision', args=[self.pk])
def activate(self): def activate(self, update_db=True):
""" """
Cache the configuration data. Cache the configuration data.
Parameters:
update_db: Mark the ConfigRevision as active in the database (default: True)
""" """
cache.set('config', self.data, None) cache.set('config', self.data, None)
cache.set('config_version', self.pk, None) cache.set('config_version', self.pk, None)
# Set all instances of ConfigRevision to false and set this instance to true if update_db:
ConfigRevision.objects.all().update(active=False) # Set all instances of ConfigRevision to false and set this instance to true
ConfigRevision.objects.filter(pk=self.pk).update(active=True) ConfigRevision.objects.all().update(active=False)
ConfigRevision.objects.filter(pk=self.pk).update(active=True)
activate.alters_data = True activate.alters_data = True

View File

@@ -80,22 +80,21 @@ class Config:
try: try:
# Enforce the creation date as the ordering parameter # Enforce the creation date as the ordering parameter
revision = ConfigRevision.objects.get(active=True) revision = ConfigRevision.objects.get(active=True)
logger.debug(f"Loaded active configuration revision #{revision.pk}") logger.debug(f"Loaded active configuration revision (#{revision.pk})")
except (ConfigRevision.DoesNotExist, ConfigRevision.MultipleObjectsReturned): except (ConfigRevision.DoesNotExist, ConfigRevision.MultipleObjectsReturned):
logger.debug("No active configuration revision found - falling back to most recent")
revision = ConfigRevision.objects.order_by('-created').first() 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 configuration found in database; proceeding with default values")
return return
logger.debug(f"Using fallback configuration revision #{revision.pk}") logger.debug(f"No active configuration revision found; falling back to most recent (#{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)")
return return
revision.activate() revision.activate(update_db=False)
logger.debug("Filled cache with data from latest ConfigRevision")
self._populate_from_cache() self._populate_from_cache()
logger.debug("Filled cache with data from latest ConfigRevision")
class ConfigItem: class ConfigItem: