Skip to content

[OU-FIX] mail: merge duplicate chats before member_indices unique index - #8

Merged
cursor[bot] merged 1 commit into
20.0from
cursor/ou-20-02-channel-member-indices-3e8f
Nov 12, 2026
Merged

cursor[bot] merged 1 commit into
20.0from
cursor/ou-20-02-channel-member-indices-3e8f

Conversation

@adekock11

Copy link
Copy Markdown
Owner

Symptom

Creating the partial unique index discuss_channel_member_indices_unique fails:

duplicate key value violates unique constraint

Odoo 20 also adds check constraints discuss_channel_member_indices_chat_check and discuss_channel_member_indices_channel_check. Older versions allowed several DM chats between the same members, so more than one chat would receive the same member_indices.

Root cause

discuss.channel.member_indices is a new stored precomputed char (upgrade_analysis.txt: NEW isfunction: function, stored). Odoo 20 DiscussChannel._member_indices builds:

",".join(
    [f"p{pid}" for pid in sorted(partners.ids)]
    + [f"g{gid}" for gid in sorted(guests.ids)]
)

Partner ids are sorted numerically, then guest ids. The unique index is UNIQUE (member_indices) WHERE channel_type = 'chat'. Chats must have a non-empty value. Every other channel type must have NULL.

Change

openupgrade_scripts/scripts/mail/20.0.1.19/pre-migration.py, before the model is initialised:

  1. openupgrade.add_columns adds member_indices so the compute does not run.
  2. SQL fills chats in that exact format (string_agg ... ORDER BY sort_group, sort_id, partners then guests, numeric id). Non-chats are set back to NULL.
  3. A chat with no partner and no guest cannot satisfy the chat check ("" is rejected). Those rows become channel_type = 'group' with NULL member_indices. Their messages stay. A group is private and the chat constraints do not apply.
  4. Duplicate chats (same non-empty member_indices) keep the oldest (create_date, then id). The others are merged into it:
    • mail_message with model = 'discuss.channel' (res_id moved, rows not deleted)
    • discuss_channel_member: one row per partner/guest; seen_message_id and, when the column still exists, fetched_message_id keep the greater message id
    • mail_followers, including subtype links, then the duplicate follower is dropped
    • ir_attachment with res_model = 'discuss.channel'
    • other foreign keys to discuss.channel are re-pointed (a self-parent that would point at itself is cleared)
  5. The duplicate channels are deleted.
  6. The script counts mail_message rows with model = 'discuss.channel' before and after and raises if the count changed.

The analysis lines for member_indices and the three new constraints are marked DONE.

Fork 20.0 does not contain the mail migration yet (fork PR #4 / 20.0-integration does). This branch therefore carries that pre-migration plus this step. On a tree that already has PR #4, the only addition is _merge_duplicate_chats and its helpers, called at the start of migrate().

Verification

The format was checked against Odoo 20 addons/mail/models/discuss/discuss_channel.py (_member_indices, the unique index and both checks). A small Python assertion locks the string form (partners [10, 2] and guests [3, 1] → p2,p10,g1,g3). ruff is clean. No database upgrade was run.

No OCA pull request fills or deduplicates member_indices.

Upstream

Generic. Submit to OCA 20.0 on top of the mail migration. Not applicable to 18.0 or 19.0.

Limitations

  • "Latest" seen/fetched message is the greater mail_message.id. That matches creation order. A message inserted with an explicit lower id would be treated as older.
  • Chats with no members are retyped as groups rather than deleted, so an empty chat is no longer a chat. Messages are kept.
  • A foreign key that cannot be re-pointed because of a unique violation is logged and left. The following DELETE then fails the upgrade instead of dropping the related rows.
  • Only model = 'discuss.channel' is moved. Rows still stored as mail.channel from a pre-16 database that never had that column rewritten are not touched. By 19.0 the model name is discuss.channel.
Open in Web Open in Cursor 

Replayed onto 20.0 after the mail migration landed. Member-less chats
become groups, duplicate chats fold onto the oldest, and the
discuss.channel message count is checked.

Co-authored-by: André de Kock <[email protected]>
@cursor
cursor Bot force-pushed the cursor/ou-20-02-channel-member-indices-3e8f branch from e8c2a84 to 7a58831 Compare November 12, 2026 08:45
@cursor
cursor Bot merged commit c55aecd into 20.0 Nov 12, 2026
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants