migrations: Improve has_link migration.

This fixes a few minor issues with the migration:

* Skips messages with empty rendered_content, fixing an exception that
  affected 4 messages on chat.zulip.org.
* Accesses messages in order.
* Provides some basic output on the progress made.

This should make life substantially better for any organizations that
run into trouble with this migration, either due to it taking a long
time to run or due to any new exceptions.
This commit is contained in:
Tim Abbott 2019-12-12 13:59:22 -08:00
parent 9f223bb7c2
commit cd3535ad0c
1 changed files with 16 additions and 1 deletions

View File

@ -10,7 +10,22 @@ import lxml
def fix_has_link(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None: def fix_has_link(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
Message = apps.get_model('zerver', 'Message') Message = apps.get_model('zerver', 'Message')
for message in Message.objects.all(): total_messages = Message.objects.count()
print(" Processing messages...")
for message in Message.objects.order_by("id"):
if message.rendered_content == "":
# There have been bugs in the past that made it possible
# for a message to have "" as its rendered_content; we
# need to skip those because lxml won't process them.
#
# They should safely already have the correct state
# has_link=has_image=has_attachment=False.
continue
if message.id % 1000 == 0:
print("Processed %s / %s" % (message.id, total_messages))
# Because we maintain the Attachment table, this should be as # Because we maintain the Attachment table, this should be as
# simple as just just checking if there's any Attachment # simple as just just checking if there's any Attachment
# objects associated with this message. # objects associated with this message.