From a6f744a11f1548abab9a276999d4a2862dce4f30 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 4 Dec 2012 17:50:02 -0500 Subject: [PATCH] populate_db: Fix inefficient pointer update at end of replay. This cuts a 30s operation down to about 2s on my machine. And also move the code to run before we print the "done" message and have logging for how long it is taking. (imported from commit 2f20f8ca3fee714735a50fe6c6cfd630df452768) --- zephyr/management/commands/populate_db.py | 30 +++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/zephyr/management/commands/populate_db.py b/zephyr/management/commands/populate_db.py index 6a34ef80e8..4b9dbc5e4b 100644 --- a/zephyr/management/commands/populate_db.py +++ b/zephyr/management/commands/populate_db.py @@ -232,17 +232,6 @@ class Command(BaseCommand): self.stdout.write("Successfully populated test database.\n") if options["replay_old_messages"]: restore_saved_messages() - site = Site.objects.get_current() - site.domain = 'humbughq.com' - site.save() - - # Set restored pointers to the very latest messages - for user_profile in UserProfile.objects.all(): - ids = [u.message_id for u in UserMessage.objects.filter(user_profile = user_profile)] - if ids != []: - user_profile.pointer = max(ids) - user_profile.save() - recipient_hash = {} def get_recipient_by_id(rid): @@ -626,8 +615,23 @@ def restore_saved_messages(): print datetime.datetime.now(), "Finished importing %s messages (%s usermessages)" % \ (len(all_messages), tot_user_messages) - # Clear the remainder of the line, using a terminal control code. - sys.stderr.write('\rDone replaying old messages.\x1B[0K\n') + site = Site.objects.get_current() + site.domain = 'humbughq.com' + site.save() + + print datetime.datetime.now(), "Filling in user pointers..." + + # Set restored pointers to the very latest messages + with transaction.commit_on_success(): + for up in UserProfile.objects.all(): + try: + top = UserMessage.objects.filter(user_profile_id=up.id).order_by("-message")[0] + user_profile.pointer = top.message_id + except IndexError: + user_profile.pointer = -1 + user_profile.save() + + print datetime.datetime.now(), "Done replaying old messages" # Create some test messages, including: # - multiple streams