topic: Fix history order for topic moves.

5c96f94206 mistakenly appended, rather than prepended, the edit to
the history.  This caused AssertionErrors when attempting to view the
history of moved messages, which check that the `last_edit_time`
matches the timestamp of the first edit in the list.

Fix the ordering, and update the `edit_history` for messages that were
affected.  We limit to only messages edited since the commit was
merged, since that helps bound the affected messages somewhat.
This commit is contained in:
Alex Vandiver 2024-02-21 03:37:54 +00:00 committed by Tim Abbott
parent e891c57d0e
commit b747ea285f
2 changed files with 39 additions and 4 deletions

View File

@ -194,6 +194,10 @@ def update_messages_for_topic_edit(
# )::text # )::text
"edit_history": Cast( "edit_history": Cast(
Func( Func(
Cast(
Value(orjson.dumps([edit_history_event]).decode()),
JSONField(),
),
Cast( Cast(
Func( Func(
F("edit_history"), F("edit_history"),
@ -202,10 +206,6 @@ def update_messages_for_topic_edit(
), ),
JSONField(), JSONField(),
), ),
Cast(
Value(orjson.dumps([edit_history_event]).decode()),
JSONField(),
),
function="", function="",
arg_joiner=" || ", arg_joiner=" || ",
), ),

View File

@ -0,0 +1,35 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("zerver", "0496_alter_scheduledmessage_read_by_sender"),
]
operations = [
migrations.RunSQL(
# Update with properly-sorted history for messages changed
# after 5c96f942060e was merged.
"""
WITH sorted_history AS (
SELECT zerver_message.id AS rowid,
JSONB_AGG(value ORDER BY (value->>'timestamp')::NUMERIC desc) AS updated_history
FROM zerver_message
CROSS JOIN JSONB_ARRAY_ELEMENTS(zerver_message.edit_history::jsonb)
WHERE zerver_message.edit_history IS NOT NULL
AND zerver_message.last_edit_time > '2024-02-14'
AND JSONB_ARRAY_LENGTH(zerver_message.edit_history::jsonb) > 1
GROUP BY zerver_message.id
ORDER BY zerver_message.id
)
UPDATE zerver_message
SET edit_history = sorted_history.updated_history::text
FROM sorted_history
WHERE zerver_message.id = sorted_history.rowid
AND zerver_message.edit_history::jsonb != sorted_history.updated_history
""",
reverse_sql="",
elidable=True,
)
]