Files
mesh-catalog/modules/mesh-mail-reader/cmd/mesh-mail-reader/migrations/0002-one-entry-per-message.sql
T
jochen 92dc8e313c issue 1849: keep one mail entry per message, by its Message-ID, else its folder and UID
Gmail files one message in INBOX and All Mail, each with its own UID; the reader kept an entry per folder.
waiting_mail and mail_entry are now unique per account and message key (migration 0002 and schema.sql), a
mail that already has its entry no longer waits again, and the test double keeps PgStore's own keys
(issue 1548). A schema model test proves schema.sql equals the migrations in order.
2026-10-22 19:36:19 +02:00

23 lines
1.6 KiB
SQL

-- Issue 1849: one mail entry per message. Gmail files one message in several folders (INBOX and All Mail),
-- each with its own UID, and mesh-mail says an added event per folder; a mail is now kept once per account
-- and message key: its Message-ID ('mid:' and the id) where it has one, else its folder, UIDVALIDITY and UID
-- ('uid:' folder/uidvalidity/uid; MessageKey, store.go). The copies already kept twice are dropped, the
-- oldest kept.
alter table waiting_mail add column message_key text not null default '';
update waiting_mail set message_key = case when btrim(message_id) <> '' then 'mid:' || btrim(message_id)
else 'uid:' || folder || '/' || uidvalidity || '/' || uid end;
delete from waiting_mail w using waiting_mail o
where w.account = o.account and w.message_key = o.message_key and w.id > o.id;
alter table waiting_mail add constraint waiting_mail_one_per_message unique (account, message_key);
-- An entry kept before this knows no UID: one without a Message-ID keeps a key of its own ('entry:' and its
-- id), so none of those is merged with another.
alter table mail_entry add column uidvalidity bigint not null default 0;
alter table mail_entry add column uid bigint not null default 0;
alter table mail_entry add column message_key text not null default '';
update mail_entry set message_key = case when btrim(message_id) <> '' then 'mid:' || btrim(message_id)
else 'entry:' || id end;
delete from mail_entry e using mail_entry o
where e.account = o.account and e.message_key = o.message_key and e.id > o.id;
alter table mail_entry add constraint mail_entry_one_per_message unique (account, message_key);