115 lines
4.0 KiB
JavaScript
115 lines
4.0 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 createSnapshot() {
|
|
console.log('Creating test database snapshot...');
|
|
const pool = mysql.createPool(dbConfig);
|
|
|
|
try {
|
|
const connection = await pool.getConnection();
|
|
try {
|
|
// Create snapshots directory if it doesn't exist
|
|
if (!fs.existsSync(SNAPSHOTS_DIR)) {
|
|
fs.mkdirSync(SNAPSHOTS_DIR, { recursive: true });
|
|
}
|
|
|
|
// Get categories
|
|
const [categories] = await connection.query(`
|
|
SELECT id, name, created_at
|
|
FROM categories
|
|
LIMIT 10
|
|
`);
|
|
|
|
// Get a diverse set of ~100 products
|
|
const [products] = await connection.query(`
|
|
SELECT p.*
|
|
FROM products p
|
|
WHERE EXISTS (
|
|
SELECT 1 FROM orders o WHERE o.product_id = p.product_id
|
|
UNION
|
|
SELECT 1 FROM purchase_orders po WHERE po.product_id = p.product_id
|
|
)
|
|
ORDER BY RAND()
|
|
LIMIT 100
|
|
`);
|
|
|
|
// Get product_categories for selected products
|
|
const [product_categories] = await connection.query(`
|
|
SELECT pc.product_id, pc.category_id
|
|
FROM product_categories pc
|
|
WHERE pc.product_id IN (?)
|
|
`, [products.map(p => p.product_id)]);
|
|
|
|
// Get orders for selected products (last 6 months)
|
|
const [orders] = await connection.query(`
|
|
SELECT o.*
|
|
FROM orders o
|
|
WHERE o.product_id IN (?)
|
|
AND o.date >= DATE_SUB(NOW(), INTERVAL 6 MONTH)
|
|
ORDER BY o.date DESC
|
|
`, [products.map(p => p.product_id)]);
|
|
|
|
// Get purchase orders for selected products (last 6 months)
|
|
const [purchase_orders] = await connection.query(`
|
|
SELECT po.*
|
|
FROM purchase_orders po
|
|
WHERE po.product_id IN (?)
|
|
AND po.date >= DATE_SUB(NOW(), INTERVAL 6 MONTH)
|
|
ORDER BY po.date DESC
|
|
`, [products.map(p => p.product_id)]);
|
|
|
|
// Create snapshot object
|
|
const snapshot = {
|
|
metadata: {
|
|
created_at: new Date().toISOString(),
|
|
description: 'Test snapshot with ~100 diverse products and their related data'
|
|
},
|
|
categories,
|
|
products,
|
|
product_categories,
|
|
orders,
|
|
purchase_orders
|
|
};
|
|
|
|
// Save snapshot
|
|
const snapshotPath = path.join(SNAPSHOTS_DIR, 'test_snapshot.json');
|
|
fs.writeFileSync(snapshotPath, JSON.stringify(snapshot, null, 2));
|
|
|
|
console.log('Snapshot created successfully:');
|
|
console.log('Products:', products.length);
|
|
console.log('Orders:', orders.length);
|
|
console.log('Purchase Orders:', purchase_orders.length);
|
|
console.log('Categories:', categories.length);
|
|
console.log('Saved to:', snapshotPath);
|
|
|
|
} finally {
|
|
connection.release();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error creating snapshot:', error);
|
|
throw error;
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
// Create snapshot if run directly
|
|
if (require.main === module) {
|
|
createSnapshot().catch(console.error);
|
|
}
|
|
|
|
module.exports = { createSnapshot };
|