Add modelcounts command; useful in the context of housekeeping when servers are down

This commit is contained in:
Klaas van Schelven
2026-01-06 20:41:43 +01:00
parent e6c163c674
commit 0d0b9b509f
2 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
from django.core.management.base import BaseCommand
from django import apps
from bugsink.transaction import durable_atomic
class Command(BaseCommand):
@durable_atomic
def handle(self, *args, **options):
# Copy/paste from bugsink/views's 'def counts' with removal of the limit_runtime / CachedModelCount parts.
interesting_apps = [
# "admin",
# "auth",
"bsmain",
# "contenttypes",
"events",
"files",
"ingest",
"issues",
# "phonehome",
"projects",
"releases",
# "sessions",
"snappea",
"tags",
"teams",
"users",
]
counts = {}
for app_label in interesting_apps:
counts[app_label] = {}
app_config = apps.apps.get_app_config(app_label)
for model in app_config.get_models():
if model.__name__ == "CachedModelCount":
continue # skip the CachedModelCount model itself
model_name = f"{app_label}.{model.__name__}"
print(f"{model_name:<30}{model.objects.count():>12}")

View File

@@ -173,6 +173,7 @@ def settings_view(request):
@user_passes_test(lambda u: u.is_superuser) @user_passes_test(lambda u: u.is_superuser)
@atomic_for_request_method # get a consistent view (barring cached, which are marked as such) @atomic_for_request_method # get a consistent view (barring cached, which are marked as such)
def counts(request): def counts(request):
# See also: modelcounts management command.
interesting_apps = [ interesting_apps = [
# "admin", # "admin",
# "auth", # "auth",