Update migration and arrange for activation changes

This commit is contained in:
Daniel Sheppard 2025-09-14 11:11:15 -05:00
parent 42857b15b2
commit 0074199845
2 changed files with 25 additions and 3 deletions

View File

@ -3,6 +3,27 @@
from django.db import migrations, models
def get_active(apps, schema_editor):
from django.core.cache import cache
ConfigRevision = apps.get_model('core', 'ConfigRevision')
version = None
revision = None
try:
version = cache.get('config_version')
except Exception:
pass
if version:
revision = ConfigRevision.objects.filter(pk=version).first()
else:
revision = ConfigRevision.objects.order_by('-created').first()
if revision:
revision.active = True
revision.save()
class Migration(migrations.Migration):
dependencies = [
@ -15,10 +36,11 @@ class Migration(migrations.Migration):
name='active',
field=models.BooleanField(default=False),
),
migrations.RunPython(code=get_active, reverse_code=migrations.RunPython.noop),
migrations.AddConstraint(
model_name='configrevision',
constraint=models.UniqueConstraint(
condition=models.Q(('actvive', True)), fields=('active',), name='unique_active_config_revision'
condition=models.Q(('active', True)), fields=('active',), name='unique_active_config_revision'
),
),
]

View File

@ -41,7 +41,7 @@ class ConfigRevision(models.Model):
constraints = [
models.UniqueConstraint(
fields=('active',),
condition=models.Q(actvive=True),
condition=models.Q(active=True),
name='unique_active_config_revision',
)
]
@ -78,4 +78,4 @@ class ConfigRevision(models.Model):
@property
def is_active(self):
return cache.get('config_version') == self.pk
return self.active