129 lines
4.9 KiB
JavaScript
129 lines
4.9 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const mysql = require('mysql2/promise');
|
|
const dotenv = require('dotenv');
|
|
|
|
dotenv.config({ path: path.join(__dirname, '../.env') });
|
|
|
|
const dbConfig = {
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME,
|
|
multipleStatements: true
|
|
};
|
|
|
|
const SNAPSHOTS_DIR = path.join(__dirname, '../snapshots');
|
|
|
|
async function restoreSnapshot() {
|
|
console.log('Restoring test database from snapshot...');
|
|
const pool = mysql.createPool(dbConfig);
|
|
|
|
try {
|
|
// Read snapshot
|
|
const snapshotPath = path.join(SNAPSHOTS_DIR, 'test_snapshot.json');
|
|
if (!fs.existsSync(snapshotPath)) {
|
|
throw new Error('Snapshot file not found. Run create-test-snapshot.js first.');
|
|
}
|
|
|
|
const snapshot = JSON.parse(fs.readFileSync(snapshotPath, 'utf8'));
|
|
|
|
// First, create schema (this will drop existing tables)
|
|
const schemaSQL = fs.readFileSync(path.join(__dirname, '../db/schema.sql'), 'utf8');
|
|
await pool.query(schemaSQL);
|
|
|
|
const connection = await pool.getConnection();
|
|
try {
|
|
await connection.beginTransaction();
|
|
|
|
// Insert categories first (they're referenced by product_categories)
|
|
if (snapshot.categories.length > 0) {
|
|
const categoryValues = snapshot.categories.map(c => [c.id, c.name, c.created_at]);
|
|
await connection.query(
|
|
'INSERT INTO categories (id, name, created_at) VALUES ?',
|
|
[categoryValues]
|
|
);
|
|
}
|
|
|
|
// Insert products
|
|
if (snapshot.products.length > 0) {
|
|
const productValues = snapshot.products.map(p => [
|
|
p.product_id, p.title, p.SKU, p.created_at, p.stock_quantity,
|
|
p.price, p.regular_price, p.cost_price, p.landing_cost_price,
|
|
p.barcode, p.updated_at, p.visible, p.managing_stock,
|
|
p.replenishable, p.vendor, p.vendor_reference, p.permalink,
|
|
p.categories, p.image, p.brand, p.options, p.tags, p.moq, p.uom
|
|
]);
|
|
await connection.query(
|
|
'INSERT INTO products VALUES ?',
|
|
[productValues]
|
|
);
|
|
}
|
|
|
|
// Insert product_categories relationships
|
|
if (snapshot.product_categories.length > 0) {
|
|
const pcValues = snapshot.product_categories.map(pc => [
|
|
pc.product_id, pc.category_id
|
|
]);
|
|
await connection.query(
|
|
'INSERT INTO product_categories (product_id, category_id) VALUES ?',
|
|
[pcValues]
|
|
);
|
|
}
|
|
|
|
// Insert orders
|
|
if (snapshot.orders.length > 0) {
|
|
const orderValues = snapshot.orders.map(o => [
|
|
o.id, o.order_number, o.product_id, o.SKU, o.date,
|
|
o.price, o.quantity, o.discount, o.tax, o.tax_included,
|
|
o.shipping, o.customer, o.status, o.payment_method,
|
|
o.shipping_method, o.shipping_address, o.billing_address,
|
|
o.canceled
|
|
]);
|
|
await connection.query(
|
|
'INSERT INTO orders VALUES ?',
|
|
[orderValues]
|
|
);
|
|
}
|
|
|
|
// Insert purchase orders
|
|
if (snapshot.purchase_orders.length > 0) {
|
|
const poValues = snapshot.purchase_orders.map(po => [
|
|
po.id, po.po_id, po.vendor, po.date, po.expected_date,
|
|
po.product_id, po.sku, po.cost_price, po.status, po.notes,
|
|
po.ordered, po.received, po.received_date
|
|
]);
|
|
await connection.query(
|
|
'INSERT INTO purchase_orders VALUES ?',
|
|
[poValues]
|
|
);
|
|
}
|
|
|
|
await connection.commit();
|
|
|
|
console.log('Snapshot restored successfully:');
|
|
console.log('Products:', snapshot.products.length);
|
|
console.log('Orders:', snapshot.orders.length);
|
|
console.log('Purchase Orders:', snapshot.purchase_orders.length);
|
|
console.log('Categories:', snapshot.categories.length);
|
|
|
|
} catch (error) {
|
|
await connection.rollback();
|
|
throw error;
|
|
} finally {
|
|
connection.release();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error restoring snapshot:', error);
|
|
throw error;
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
// Restore snapshot if run directly
|
|
if (require.main === module) {
|
|
restoreSnapshot().catch(console.error);
|
|
}
|
|
|
|
module.exports = { restoreSnapshot };
|