fix: database upgrade deletes the messages of chats with a broken message list (#34995)

The database upgrade that moves chat messages out of the chat record deleted every message of a chat whose saved message list was null or in an unexpected shape, so those messages were lost for good. The upgrade now keeps them, so such chats open with all their messages afterwards, and a single broken entry in the list no longer removes that message. Chats from older versions that also carry a plain list of their messages no longer get their first question added a second time.

Fixes #34991
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-25 03:25:39 +04:00
1 parent 12fce1edfa
commit b87c99f44f
1 file changed
+26 -8
@@ -84,7 +84,7 @@ def _history_messages(chat: dict) -> dict:
for index, message in enumerate(chat['messages']):
if isinstance(message, dict):
message_id = str(message.get('id') or index)
messages[message_id] = {**message, 'id': message_id, 'parentId': message.get('parentId') or parent_id}
messages[message_id] = {**message, 'parentId': message.get('parentId') or parent_id}
parent_id = message_id
return {message_id: message for message_id, message in (messages or {}).items() if isinstance(message, dict)}
@@ -154,20 +154,39 @@ def _move_chat_history(
existing = {
row.id: row
for row in conn.execute(
sa.select(columns.id, columns.user_id, columns.output, columns.usage, columns.created_at).where(
columns.chat_id == chat_id
)
sa.select(
columns.id,
columns.user_id,
columns.role,
columns.content,
columns.output,
columns.usage,
columns.created_at,
).where(columns.chat_id == chat_id)
)
}
history = chat.get('history') if isinstance(chat.get('history'), dict) else {}
# The web client used to delete messages by saving the chat without them, which left their rows.
deleted_ids = existing.keys() - {f'{chat_id}-{message_id}' for message_id in messages}
if deleted_ids:
conn.execute(sa.delete(chat_message_table).where(columns.id.in_(deleted_ids)))
if isinstance(history.get('messages'), dict):
deleted_ids = existing.keys() - {f'{chat_id}-{message_id}' for message_id in history['messages']}
if deleted_ids:
conn.execute(sa.delete(chat_message_table).where(columns.id.in_(deleted_ids)))
now = int(time.time())
updates, inserts = [], []
for message_id, message in messages.items():
row_id = f'{chat_id}-{message_id}'
# An old list entry without an id of its own is already saved when a row has its role and content.
if (
row_id not in existing
and not isinstance(history.get('messages'), dict)
and 'id' not in message
and any(
stored.role == message.get('role') and stored.content == message.get('content')
for stored in existing.values()
)
):
continue
created_at = _recorded_timestamp(message.get('timestamp'))
author = message.get('user') if isinstance(message.get('user'), dict) else {}
author_id = message.get('user_id') or author.get('id')
@@ -216,7 +235,6 @@ def _move_chat_history(
if inserts:
conn.execute(sa.insert(chat_message_table), inserts)
history = chat.get('history') if isinstance(chat.get('history'), dict) else {}
conn.execute(
sa.update(chat_table)
.where(chat_table.c.id == chat_id)