| 465 | } |
| 466 | |
| 467 | async function rememberRemoteTombstone( |
| 468 | db: Awaited<ReturnType<typeof getDB>>, |
| 469 | tableName: string, |
| 470 | id: string, |
| 471 | deletedAt: number, |
| 472 | deviceId: string, |
| 473 | ): Promise<void> { |
| 474 | try { |
| 475 | await db.execute( |
| 476 | `INSERT INTO sync_tombstones (id, table_name, deleted_at, device_id) |
| 477 | VALUES (?, ?, ?, ?) |
| 478 | ON CONFLICT(id, table_name) DO UPDATE SET |
| 479 | deleted_at = CASE |
| 480 | WHEN excluded.deleted_at > sync_tombstones.deleted_at |
| 481 | THEN excluded.deleted_at |
| 482 | ELSE sync_tombstones.deleted_at |
| 483 | END, |
| 484 | device_id = CASE |
| 485 | WHEN excluded.deleted_at > sync_tombstones.deleted_at |
| 486 | THEN excluded.device_id |
| 487 | ELSE sync_tombstones.device_id |
| 488 | END`, |
| 489 | [id, tableName, deletedAt, deviceId], |
| 490 | ); |
| 491 | } catch { |
| 492 | // sync_tombstones may not exist on older schema variants. |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | async function loadExistingRecordStates( |
| 497 | db: Awaited<ReturnType<typeof getDB>>, |