Skip to content

Commit

Permalink
fix: track_deletes only deletes records from previous jobs
Browse files Browse the repository at this point in the history
If records were added via a webhook while the full sync is running we
don't want to delete those records.
This commit ensures that only records from job older than the current
one are deleted when track_deletes=true
  • Loading branch information
TBonnin committed Jan 8, 2025
1 parent c2089b1 commit d77305e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/jobs/lib/execution/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export async function handleSyncSuccess({ nangoProps }: { nangoProps: NangoProps
for (const model of nangoProps.syncConfig.models) {
let deletedKeys: string[] = [];
if (nangoProps.syncConfig.track_deletes) {
deletedKeys = await records.markNonCurrentGenerationRecordsAsDeleted({
deletedKeys = await records.markPreviousGenerationRecordsAsDeleted({
connectionId: nangoProps.nangoConnectionId,
model,
syncId: nangoProps.syncId,
Expand Down
8 changes: 3 additions & 5 deletions packages/records/lib/models/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ export async function deleteRecordCount({ connectionId, environmentId, model }:
await db.from(RECORD_COUNTS_TABLE).where({ connection_id: connectionId, environment_id: environmentId, model }).del();
}

// Mark all non-deleted records that don't belong to currentGeneration as deleted
// Mark all non-deleted records from previous generations as deleted
// returns the ids of records being deleted
export async function markNonCurrentGenerationRecordsAsDeleted({
export async function markPreviousGenerationRecordsAsDeleted({
connectionId,
model,
syncId,
Expand All @@ -471,9 +471,7 @@ export async function markNonCurrentGenerationRecordsAsDeleted({
sync_id: syncId,
deleted_at: null
})
.whereNot({
sync_job_id: generation
})
.where('sync_job_id', '<', generation)
.update({
deleted_at: now,
updated_at: now,
Expand Down

0 comments on commit d77305e

Please sign in to comment.