Add incremental import support and tracking for database synchronization
This commit is contained in:
@@ -12,6 +12,12 @@ async function importOrders(prodConnection, localConnection) {
|
||||
const missingProducts = new Set(); // Store products that need to be imported
|
||||
|
||||
try {
|
||||
// Get last sync info
|
||||
const [syncInfo] = await localConnection.query(
|
||||
"SELECT last_sync_timestamp FROM sync_status WHERE table_name = 'orders'"
|
||||
);
|
||||
const lastSyncTime = syncInfo?.[0]?.last_sync_timestamp || '1970-01-01';
|
||||
|
||||
// First get the column names from the table structure
|
||||
const [columns] = await localConnection.query(`
|
||||
SELECT COLUMN_NAME
|
||||
@@ -24,14 +30,16 @@ async function importOrders(prodConnection, localConnection) {
|
||||
.map((col) => col.COLUMN_NAME)
|
||||
.filter((name) => name !== "id"); // Skip auto-increment ID
|
||||
|
||||
// Get total count first for progress indication
|
||||
// Get total count first for progress indication - modified for incremental
|
||||
const [countResult] = await prodConnection.query(`
|
||||
SELECT COUNT(*) as total
|
||||
FROM order_items oi FORCE INDEX (PRIMARY)
|
||||
JOIN _order o FORCE INDEX (PRIMARY) ON oi.order_id = o.order_id
|
||||
WHERE o.order_status >= 15
|
||||
AND o.date_placed_onlydate >= DATE_SUB(CURRENT_DATE, INTERVAL 2 YEAR)
|
||||
`);
|
||||
AND (o.date_placed_onlydate > ?
|
||||
OR o.stamp > ?)
|
||||
`, [lastSyncTime, lastSyncTime]);
|
||||
|
||||
const totalOrders = countResult[0].total;
|
||||
|
||||
outputProgress({
|
||||
@@ -81,9 +89,10 @@ async function importOrders(prodConnection, localConnection) {
|
||||
FROM order_items oi
|
||||
JOIN _order o ON oi.order_id = o.order_id
|
||||
WHERE o.order_status >= 15
|
||||
AND o.date_placed_onlydate >= DATE_SUB(CURRENT_DATE, INTERVAL 2 YEAR)
|
||||
AND (o.date_placed_onlydate > ?
|
||||
OR o.stamp > ?)
|
||||
LIMIT ? OFFSET ?
|
||||
`, [batchSize, offset]);
|
||||
`, [lastSyncTime, lastSyncTime, batchSize, offset]);
|
||||
|
||||
// Check if all products exist before inserting orders
|
||||
const orderProductPids = [...new Set(orders.map((o) => o.pid))];
|
||||
@@ -213,6 +222,13 @@ async function importOrders(prodConnection, localConnection) {
|
||||
}
|
||||
}
|
||||
|
||||
// After successful import, update the sync status
|
||||
await localConnection.query(`
|
||||
INSERT INTO sync_status (table_name, last_sync_timestamp)
|
||||
VALUES ('orders', NOW())
|
||||
ON DUPLICATE KEY UPDATE last_sync_timestamp = NOW()
|
||||
`);
|
||||
|
||||
outputProgress({
|
||||
status: "complete",
|
||||
operation: "Orders import completed",
|
||||
@@ -225,7 +241,9 @@ async function importOrders(prodConnection, localConnection) {
|
||||
status: "complete",
|
||||
totalImported: total,
|
||||
missingProducts: missingProducts.size,
|
||||
retriedOrders: skippedOrders.size
|
||||
retriedOrders: skippedOrders.size,
|
||||
incrementalUpdate: true,
|
||||
lastSyncTime
|
||||
};
|
||||
} catch (error) {
|
||||
outputProgress({
|
||||
|
||||
Reference in New Issue
Block a user