mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
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:
+44
@@ -0,0 +1,44 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* The hourly job that tells every forwarding address holder once that the mail
|
||||
* import is paused keeps its "already told" mark and its lease in
|
||||
* `mail_ingest_cursor`, as the `notice` reader. The CHECK from
|
||||
* Migration20200101046410 only knew `imap`, `cloud` and `pipeline`, so taking
|
||||
* that lease failed on the insert (an `ON CONFLICT DO NOTHING` does not cover a
|
||||
* CHECK) and nobody was ever told.
|
||||
*
|
||||
* SQLite cannot change a CHECK in place, so the table is rebuilt with `notice`
|
||||
* added. No table points at `mail_ingest_cursor` and it has no index of its own
|
||||
* beyond the `UNIQUE(transport, mailbox)`, so it is copied, dropped and renamed
|
||||
* on its own. Every row keeps its id, position and lease.
|
||||
*/
|
||||
export class Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders extends Migration {
|
||||
override name = 'Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(`
|
||||
CREATE TABLE mail_ingest_cursor_new (
|
||||
id INTEGER PRIMARY KEY,
|
||||
transport TEXT NOT NULL CHECK(transport IN ('imap', 'cloud', 'pipeline', 'notice')),
|
||||
mailbox TEXT NOT NULL,
|
||||
uid_validity INTEGER,
|
||||
last_uid INTEGER,
|
||||
last_ok_at DATETIME,
|
||||
last_error TEXT,
|
||||
lease_owner TEXT,
|
||||
lease_until DATETIME,
|
||||
UNIQUE(transport, mailbox)
|
||||
)
|
||||
`);
|
||||
this.addSql(`
|
||||
INSERT INTO mail_ingest_cursor_new (
|
||||
id, transport, mailbox, uid_validity, last_uid, last_ok_at, last_error, lease_owner, lease_until
|
||||
)
|
||||
SELECT id, transport, mailbox, uid_validity, last_uid, last_ok_at, last_error, lease_owner, lease_until
|
||||
FROM mail_ingest_cursor
|
||||
`);
|
||||
this.addSql('DROP TABLE mail_ingest_cursor');
|
||||
this.addSql('ALTER TABLE mail_ingest_cursor_new RENAME TO mail_ingest_cursor');
|
||||
}
|
||||
}
|
||||
@@ -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' });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user