edumanagerpro2/manager/scratch/delete_orphaned_absences2.cjs

28 lines
743 B
JavaScript

const { Pool } = require('pg');
const pool = new Pool({
connectionString: 'postgresql://edumanager:EduManager2026!Seguro@150.230.87.131:5432/edumanager'
});
async function run() {
const client = await pool.connect();
try {
const res = await client.query(`
DELETE FROM frequencias a
WHERE a.tipo = 'absence' AND a.id LIKE 'auto-abs-%'
AND NOT EXISTS (
SELECT 1 FROM school_data sd
WHERE jsonb_path_exists(sd.data, ('$.attendance[*] ? (@.id == "' || a.id || '")')::jsonpath)
) RETURNING id;
`);
console.log(`Deleted ${res.rowCount} absences that are in SQL but NOT in JSON.`);
} catch (err) {
console.error(err);
} finally {
client.release();
pool.end();
}
}
run();