From 71873dfb3bd642e3d6e825b1ad5f9232bcfc4a6e Mon Sep 17 00:00:00 2001 From: root Date: Sat, 24 Jan 2026 08:47:52 -0500 Subject: [PATCH] fix: correct picklist sync statistics to properly track added vs updated Previously, all picklist syncs (Statuses, Issue Types, Sub-Issue Types, Work Types) incorrectly reported all records as 'added' even on subsequent syncs. The issue was that bulkUpsert returns total rowCount without distinguishing between inserts and updates. Fix: - Query existing values before upserting - Calculate recordsAdded = new values not in existing set - Calculate recordsUpdated = values already in existing set - Applied to all 4 picklist sync methods This resolves the confusing sync history where Sub-Issue Types showed +805 on every sync instead of ~0 added, ~805 updated. --- lib/services/entity-sync.ts | 64 +++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/lib/services/entity-sync.ts b/lib/services/entity-sync.ts index b791572..ff6d274 100644 --- a/lib/services/entity-sync.ts +++ b/lib/services/entity-sync.ts @@ -727,13 +727,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.STATUSES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map((r: any) => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, }; @@ -772,13 +780,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.ISSUE_TYPES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map((r: any) => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, }; @@ -834,13 +850,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.SUB_ISSUE_TYPES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map(r => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, }; @@ -879,13 +903,21 @@ export class EntitySyncService { picklistLogger.info('Found picklist values', { recordCount: records.length }); - // Upsert to database + // Get existing values to calculate added vs updated const tableName = getTableName(EntityType.WORK_TYPES); - const upsertedCount = await postgresClient.bulkUpsert(tableName, records, ['value']); + const existingQuery = `SELECT value FROM ${tableName}`; + const existingResult = await postgresClient.query<{ value: number }>(existingQuery); + const existingValues = new Set(existingResult.rows.map((r: any) => r.value)); + + const recordsAdded = records.filter((r: any) => !existingValues.has(r.value)).length; + const recordsUpdated = records.filter((r: any) => existingValues.has(r.value)).length; + + // Upsert to database + await postgresClient.bulkUpsert(tableName, records, ['value']); const stats: EntitySyncStats = { - recordsAdded: upsertedCount, - recordsUpdated: 0, + recordsAdded, + recordsUpdated, recordsDeleted: 0, };