26 lines
852 B
TypeScript
26 lines
852 B
TypeScript
import { pool } from '../src/db/pool';
|
|
|
|
async function cleanupDuplicates() {
|
|
console.log('Cleaning up duplicate tenant batches...');
|
|
|
|
// 1. Delete stock movements referencing duplicate user batches
|
|
await pool.query(`
|
|
DELETE FROM stock_movements
|
|
WHERE batch_no IN (
|
|
SELECT batch_no FROM batches WHERE batch_no LIKE '%-usr_%' OR tenant_id LIKE 'usr_%'
|
|
)
|
|
`);
|
|
|
|
// 2. Delete duplicate user batches
|
|
const res1 = await pool.query(`DELETE FROM batches WHERE batch_no LIKE '%-usr_%' OR tenant_id LIKE 'usr_%'`);
|
|
console.log('Deleted duplicate user batches count:', res1.rowCount);
|
|
|
|
// 3. Count remaining batches in DB
|
|
const countRes = await pool.query(`SELECT COUNT(*) FROM batches`);
|
|
console.log('Remaining total batches in DB:', countRes.rows[0].count);
|
|
|
|
await pool.end();
|
|
}
|
|
|
|
cleanupDuplicates().catch(console.error);
|