fix(mail-ingest): let the pause notice job keep its cursor row

The CHECK on mail_ingest_cursor.transport did not allow 'notice', so the
hourly job that tells address holders the import is paused failed on its
lease insert every run. A new migration rebuilds the table with 'notice'
allowed, keeping every reader's position and lease.
This commit is contained in:
Maurice
2026-11-16 10:45:22 +01:00
parent f02c4c57c3
commit 89f7908359
4 changed files with 116 additions and 0 deletions
@@ -110,6 +110,7 @@ const UNNUMBERED = [
'Migration20200101046400_a_booked_flight_shows_where_it_is',
'Migration20200101046410_a_forwarded_booking_mail_lands_in_its_trip',
'Migration20200101046420_a_visit_keeps_its_own_stay',
'Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -0,0 +1,63 @@
/**
* `Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders`:
* `mail_ingest_cursor` is rebuilt so the pause notice job can keep its row
* (`transport = 'notice'`), without losing a reader's position or lease; an
* unknown transport is still refused and a reader stays unique per mailbox.
*/
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
import type { MikroORM } from '@mikro-orm/sqlite';
import { describe, expect, it } from 'vitest';
const TARGET = 'Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders';
async function ormBeforeTarget(): Promise<MikroORM> {
const orm = await createMigrationOrm();
const names = await pendingNames(orm);
const index = names.indexOf(TARGET);
expect(index).toBeGreaterThan(0);
await migrateTo(orm, names[index - 1]);
return orm;
}
describe('Mail ingest cursor notice migration', () => {
it('MICURMIG-001: keeps every reader as it was and lets the notice job take its row', async () => {
const orm = await ormBeforeTarget();
try {
await rawExec(
orm,
`INSERT INTO mail_ingest_cursor
(id, transport, mailbox, uid_validity, last_uid, last_ok_at, last_error, lease_owner, lease_until)
VALUES (4, 'imap', 'INBOX', 7, 42, '2026-11-01 10:00:00', 'timeout', 'a', '2026-11-01 10:05:00')`,
);
await expect(
rawExec(orm, "INSERT INTO mail_ingest_cursor (transport, mailbox) VALUES ('notice', 'paused')"),
).rejects.toThrow(/CHECK/);
await migrateTo(orm, TARGET);
expect(await rawQuery(orm, 'SELECT * FROM mail_ingest_cursor')).toEqual([
{
id: 4,
transport: 'imap',
mailbox: 'INBOX',
uid_validity: 7,
last_uid: 42,
last_ok_at: '2026-11-01 10:00:00',
last_error: 'timeout',
lease_owner: 'a',
lease_until: '2026-11-01 10:05:00',
},
]);
await rawExec(orm, "INSERT INTO mail_ingest_cursor (transport, mailbox) VALUES ('notice', 'paused')");
await expect(
rawExec(orm, "INSERT INTO mail_ingest_cursor (transport, mailbox) VALUES ('notice', 'paused')"),
).rejects.toThrow(/UNIQUE/);
await expect(
rawExec(orm, "INSERT INTO mail_ingest_cursor (transport, mailbox) VALUES ('webhook', '')"),
).rejects.toThrow(/CHECK/);
} finally {
await orm.close(true);
}
}, 30000);
});
@@ -63,4 +63,12 @@ describe('MailIngestCursorRepository', () => {
expect(await cursor.findCursor('imap', 'INBOX')).toMatchObject({ uid_validity: 7, last_ok_at: null });
expect(await cursor.findCursor('cloud', '')).toBeNull();
});
it('keeps the pause notice as a reader of its own, as the schema allows', async () => {
// The pause job takes this lease before it tells anyone; the insert behind
// it once failed the transport CHECK, so nobody was ever told.
expect(await cursor.acquireLease('notice', 'paused', 'a', T1, T0)).toBe(true);
await cursor.recordOk('notice', 'paused', T0);
expect(await cursor.findCursor('notice', 'paused')).toMatchObject({ last_ok_at: T0, lease_owner: 'a' });
});
});