More import script updates and fixes, better import_history tracking
This commit is contained in:
@@ -73,6 +73,7 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
`);
|
||||
|
||||
// Get base order items first
|
||||
console.log('Last sync time:', lastSyncTime);
|
||||
const [orderItems] = await prodConnection.query(`
|
||||
SELECT
|
||||
oi.order_id,
|
||||
@@ -89,15 +90,13 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
AND o.date_placed_onlydate IS NOT NULL
|
||||
${incrementalUpdate ? `
|
||||
AND (
|
||||
o.stamp > ?
|
||||
o.stamp > ?
|
||||
OR oi.stamp > ?
|
||||
OR o.date_placed > ?
|
||||
OR o.date_shipped > ?
|
||||
OR o.date_cancelled > ?
|
||||
OR o.date_updated > ?
|
||||
)
|
||||
` : ''}
|
||||
`, incrementalUpdate ? [lastSyncTime, lastSyncTime, lastSyncTime, lastSyncTime, lastSyncTime, lastSyncTime] : []);
|
||||
`, incrementalUpdate ? [lastSyncTime, lastSyncTime] : []);
|
||||
|
||||
console.log('Found', orderItems.length, 'orders to process');
|
||||
|
||||
const totalOrders = orderItems.length;
|
||||
let processed = 0;
|
||||
@@ -111,7 +110,13 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
]);
|
||||
|
||||
await localConnection.query(`
|
||||
INSERT INTO temp_order_items VALUES ${placeholders}
|
||||
INSERT INTO temp_order_items (order_id, pid, SKU, price, quantity, base_discount)
|
||||
VALUES ${placeholders}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
SKU = VALUES(SKU),
|
||||
price = VALUES(price),
|
||||
quantity = VALUES(quantity),
|
||||
base_discount = VALUES(base_discount)
|
||||
`, values);
|
||||
|
||||
processed += batch.length;
|
||||
@@ -279,12 +284,26 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
INSERT INTO orders (${columnNames.join(",")})
|
||||
VALUES ${placeholders}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
${columnNames.map(col => `${col} = VALUES(${col})`).join(",")}
|
||||
SKU = VALUES(SKU),
|
||||
date = VALUES(date),
|
||||
price = VALUES(price),
|
||||
quantity = VALUES(quantity),
|
||||
discount = VALUES(discount),
|
||||
tax = VALUES(tax),
|
||||
tax_included = VALUES(tax_included),
|
||||
shipping = VALUES(shipping),
|
||||
customer = VALUES(customer),
|
||||
customer_name = VALUES(customer_name),
|
||||
status = VALUES(status),
|
||||
canceled = VALUES(canceled)
|
||||
`;
|
||||
|
||||
const result = await localConnection.query(query, values.flat());
|
||||
recordsAdded += result.affectedRows - result.changedRows;
|
||||
recordsUpdated += result.changedRows;
|
||||
const result = await localConnection.query(query, values);
|
||||
// For INSERT ... ON DUPLICATE KEY UPDATE:
|
||||
// - affectedRows is 1 for each inserted row and 2 for each updated row
|
||||
// - changedRows is 1 for each row that was actually changed during update
|
||||
recordsAdded += result[0].affectedRows - (2 * result[0].changedRows); // New rows
|
||||
recordsUpdated += result[0].changedRows; // Actually changed rows
|
||||
|
||||
importedCount += validOrders.length;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user