From 285bd02c921d3ba731c74a2f5eca278c7941ac5c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 5 Nov 2014 11:01:48 +0100 Subject: [PATCH] Show migration elapsed time when verbosity>1 This facilitates performance debugging related to migrations. --- django/core/management/commands/migrate.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index 33d7e44703..df2d0ce026 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals from collections import OrderedDict from importlib import import_module import itertools +import time import traceback import warnings @@ -216,22 +217,29 @@ class Command(BaseCommand): def migration_progress_callback(self, action, migration, fake=False): if self.verbosity >= 1: + compute_time = self.verbosity > 1 if action == "apply_start": + if compute_time: + self.start = time.time() self.stdout.write(" Applying %s..." % migration, ending="") self.stdout.flush() elif action == "apply_success": + elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else "" if fake: - self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED")) + self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED" + elapsed)) else: - self.stdout.write(self.style.MIGRATE_SUCCESS(" OK")) + self.stdout.write(self.style.MIGRATE_SUCCESS(" OK" + elapsed)) elif action == "unapply_start": + if compute_time: + self.start = time.time() self.stdout.write(" Unapplying %s..." % migration, ending="") self.stdout.flush() elif action == "unapply_success": + elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else "" if fake: - self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED")) + self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED" + elapsed)) else: - self.stdout.write(self.style.MIGRATE_SUCCESS(" OK")) + self.stdout.write(self.style.MIGRATE_SUCCESS(" OK" + elapsed)) def sync_apps(self, connection, app_labels): "Runs the old syncdb-style operation on a list of app_labels."