Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(platform): account for in-flight deliveries during retry migration

Timothy Yin065d4cb491fe
diff --git a/packages/platform/migrations/0021_mail_retry_budget.sql b/packages/platform/migrations/0021_mail_retry_budget.sql
index 2511cd77..9cf0e32d 100644
--- a/packages/platform/migrations/0021_mail_retry_budget.sql
+++ b/packages/platform/migrations/0021_mail_retry_budget.sql
@@ -16,12 +16,12 @@ CREATE TABLE platform_mail_outbox_next (
);
INSERT INTO platform_mail_outbox_next
SELECT id,user_id,kind,cycle,to_email,payload_json,available_at,
- attempts+CASE WHEN state='sent' THEN 1 ELSE 0 END,
- CASE WHEN state='pending' AND attempts>=3 THEN 'failed' ELSE state END,
- CASE WHEN state='pending' AND attempts>=3 THEN NULL ELSE lease_token END,
- CASE WHEN state='pending' AND attempts>=3 THEN NULL ELSE lease_until END,
- CASE WHEN state='pending' AND attempts>=3 THEN 'email_delivery_exhausted' ELSE error END
- FROM platform_mail_outbox;
+ total_attempts,
+ CASE WHEN state='pending' AND total_attempts>=3 THEN 'failed' ELSE state END,
+ CASE WHEN state='pending' AND total_attempts>=3 THEN NULL ELSE lease_token END,
+ CASE WHEN state='pending' AND total_attempts>=3 THEN NULL ELSE lease_until END,
+ CASE WHEN state='pending' AND total_attempts>=3 THEN 'email_delivery_exhausted' ELSE error END
+ FROM (SELECT *,attempts+CASE WHEN state='sent' OR (state='pending' AND lease_token IS NOT NULL) THEN 1 ELSE 0 END AS total_attempts FROM platform_mail_outbox);
DROP TABLE platform_mail_outbox;
ALTER TABLE platform_mail_outbox_next RENAME TO platform_mail_outbox;
CREATE INDEX platform_mail_pending ON platform_mail_outbox(state,available_at);
diff --git a/tests/account-lifecycle-mail.test.ts b/tests/account-lifecycle-mail.test.ts
index fd9c1ede..290bacdb 100644
--- a/tests/account-lifecycle-mail.test.ts
+++ b/tests/account-lifecycle-mail.test.ts
@@ -1421,12 +1421,18 @@ test('Retry-budget migration preserves delivery history and terminates already-e
)
.filter((f) => f.endsWith('.sql'))
.sort()) {
- if (name.startsWith('0021'))
+ if (name.startsWith('0021')) {
await db
.prepare(
"INSERT INTO platform_mail_outbox(id,user_id,kind,cycle,to_email,payload_json,available_at,attempts,state) VALUES('old-pending',1,'account-deleted','a','[email protected]','{}',0,5,'pending'),('old-sent',1,'account-deleted','b','[email protected]','{}',0,2,'sent'),('old-retry',1,'account-deleted','c','[email protected]','{}',0,2,'pending')",
)
.run()
+ await db
+ .prepare(
+ "INSERT INTO platform_mail_outbox(id,user_id,kind,cycle,to_email,payload_json,available_at,attempts,state,lease_token,lease_until) VALUES('old-inflight',1,'account-deleted','d','[email protected]','{}',0,2,'pending','worker',9999999999999)",
+ )
+ .run()
+ }
for (const sql of (
await readFile(
new URL('../packages/platform/migrations/' + name, import.meta.url),
@@ -1462,6 +1468,14 @@ test('Retry-budget migration preserves delivery history and terminates already-e
.first(),
{ state: 'pending', attempts: 2 },
)
+ assert.deepEqual(
+ await db
+ .prepare(
+ "SELECT state,attempts,lease_token FROM platform_mail_outbox WHERE id='old-inflight'",
+ )
+ .first(),
+ { state: 'failed', attempts: 3, lease_token: null },
+ )
} finally {
await mf.dispose()
}