Set up change tracking in core tables
This commit is contained in:
@@ -136,6 +136,21 @@ async function importCategories(prodConnection, localConnection) {
|
||||
total: totalInserted,
|
||||
elapsed: formatElapsedTime((Date.now() - startTime) / 1000),
|
||||
});
|
||||
|
||||
// Mark all products in these categories for recalculation
|
||||
if (categoriesToInsert.length > 0) {
|
||||
const affectedCatIds = categoriesToInsert.map(c => c.cat_id);
|
||||
|
||||
await localConnection.query(`
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
SELECT DISTINCT pc.pid, TRUE
|
||||
FROM product_categories pc
|
||||
WHERE pc.cat_id IN (?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`, [affectedCatIds]);
|
||||
}
|
||||
}
|
||||
|
||||
// After all imports, if we skipped any categories, throw an error
|
||||
|
||||
@@ -472,7 +472,19 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
|
||||
recordsAdded += inserts;
|
||||
recordsUpdated += updates;
|
||||
importedCount += processedOrderItems.size; // Count unique order items processed
|
||||
importedCount += processedOrderItems.size;
|
||||
|
||||
// Mark affected products for recalculation
|
||||
const affectedPids = [...new Set(validOrders.map(o => o.pid))];
|
||||
if (affectedPids.length > 0) {
|
||||
await localConnection.query(`
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES ${affectedPids.map(() => '(?, TRUE)').join(',')}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`, affectedPids);
|
||||
}
|
||||
}
|
||||
|
||||
// Update progress based on unique orders processed
|
||||
@@ -585,6 +597,18 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
recordsAdded += inserts;
|
||||
recordsUpdated += updates;
|
||||
importedCount += retryOrderItems.size;
|
||||
|
||||
// Mark affected products for recalculation
|
||||
const affectedPids = [...new Set(validOrders.map(o => o.pid))];
|
||||
if (affectedPids.length > 0) {
|
||||
await localConnection.query(`
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES ${affectedPids.map(() => '(?, TRUE)').join(',')}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`, affectedPids);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Warning: Failed to retry skipped orders:', error.message);
|
||||
|
||||
@@ -468,6 +468,21 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate
|
||||
recordsUpdated += insertsAndUpdates.updates.length;
|
||||
}
|
||||
|
||||
if (insertsAndUpdates.updates.length > 0 || insertsAndUpdates.inserts.length > 0) {
|
||||
const affectedPids = [
|
||||
...insertsAndUpdates.updates.map(p => p.pid),
|
||||
...insertsAndUpdates.inserts.map(p => p.pid)
|
||||
];
|
||||
|
||||
await localConnection.query(`
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES ${affectedPids.map(() => '(?, TRUE)').join(',')}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`, affectedPids);
|
||||
}
|
||||
|
||||
// Process category relationships
|
||||
if (batch.some(p => p.category_ids)) {
|
||||
// First get all valid categories
|
||||
|
||||
@@ -474,6 +474,18 @@ async function importPurchaseOrders(prodConnection, localConnection, incremental
|
||||
recordsAdded += inserts;
|
||||
recordsUpdated += Math.floor(updates); // Ensure we never have fractional updates
|
||||
processed += batchProcessed;
|
||||
|
||||
// Mark affected products for recalculation
|
||||
const affectedPids = [...new Set(productBatch.map(p => p.pid))];
|
||||
if (affectedPids.length > 0) {
|
||||
await localConnection.query(`
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES ${affectedPids.map(() => '(?, TRUE)').join(',')}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`, affectedPids);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle updates - now we know these actually have changes
|
||||
@@ -499,6 +511,18 @@ async function importPurchaseOrders(prodConnection, localConnection, incremental
|
||||
|
||||
recordsUpdated += Math.floor(updates); // Ensure we never have fractional updates
|
||||
processed += batchProcessed;
|
||||
|
||||
// Mark affected products for recalculation
|
||||
const affectedPids = [...new Set(productBatch.map(p => p.pid))];
|
||||
if (affectedPids.length > 0) {
|
||||
await localConnection.query(`
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES ${affectedPids.map(() => '(?, TRUE)').join(',')}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
`, affectedPids);
|
||||
}
|
||||
}
|
||||
|
||||
// Update progress based on time interval
|
||||
|
||||
Reference in New Issue
Block a user