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.

Cherry picked from commit b747ea285f.
Because we have already merged other migrations, this migration is
renumbered to 0517, which will appear as a no-op migration on `main`.
This commit is contained in:
Alex Vandiver
2024-02-21 03:37:54 +00:00
committed by Tim Abbott
parent 32dbcc3489
commit a8b5398013
2 changed files with 38 additions and 4 deletions

View File

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

View File

@@ -0,0 +1,34 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("zerver", "0501_delete_dangling_usermessages"),
]
operations = [
migrations.RunSQL(
# Update with properly-sorted history for messages changed
# after 210c9aaf1cc1 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,
)
]