from pathlib import Path from django.conf import settings from django.db import migrations def backfill(apps, schema_editor): Artifact = apps.get_model("artifacts", "Artifact") User = apps.get_model("auth", "User") UserProfile = apps.get_model("artifacts", "UserProfile") root = Path(settings.ARTIFACTS_ROOT) for artifact in Artifact.objects.all(): total = 0 primary = root / artifact.path if primary.exists(): total += primary.stat().st_size if artifact.kind == "markdown": sidecar = primary.with_suffix(".md") if sidecar.exists(): total += sidecar.stat().st_size if artifact.size_bytes != total: artifact.size_bytes = total artifact.save(update_fields=["size_bytes"]) default_quota = 1024 * 1024 * 1024 # 1 GB for user in User.objects.all(): UserProfile.objects.get_or_create(user=user, defaults={"quota_bytes": default_quota}) def noop_reverse(apps, schema_editor): pass class Migration(migrations.Migration): dependencies = [ ("artifacts", "0009_quota_and_keep"), ] operations = [ migrations.RunPython(backfill, noop_reverse), ]