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.
This commit is contained in:
root 2026-01-24 08:47:52 -05:00
parent 8b50cae71c
commit 71873dfb3b

View file

@ -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,
};