-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-links.mjs
More file actions
44 lines (35 loc) · 1.5 KB
/
cleanup-links.mjs
File metadata and controls
44 lines (35 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env node
import Database from 'better-sqlite3';
const db = new Database('netmap.db');
console.log('=== CLEANUP LINK DATABASE ===\n');
// Statistiche prima
const before = {
total: db.prepare('SELECT COUNT(*) as cnt FROM links').get().cnt,
nullIfname: db.prepare("SELECT COUNT(*) as cnt FROM links WHERE local_ifname IS NULL").get().cnt,
nullRemote: db.prepare("SELECT COUNT(*) as cnt FROM links WHERE remote_sysname IS NULL").get().cnt,
};
console.log('PRIMA:');
console.log(' Link totali:', before.total);
console.log(' Link con ifname NULL:', before.nullIfname);
console.log(' Link con remote NULL:', before.nullRemote);
// 1. Elimina link con ifname NULL
const del1 = db.prepare("DELETE FROM links WHERE local_ifname IS NULL").run();
console.log('\nEliminati link con ifname NULL:', del1.changes);
// 2. Elimina link con remote_sysname NULL
const del2 = db.prepare("DELETE FROM links WHERE remote_sysname IS NULL").run();
console.log('Eliminati link con remote NULL:', del2.changes);
// 3. Elimina duplicati (tiene il primo)
const del3 = db.prepare(`
DELETE FROM links WHERE id NOT IN (
SELECT MIN(id) FROM links
GROUP BY device_id, local_ifname, remote_sysname
)
`).run();
console.log('Eliminati duplicati:', del3.changes);
// Statistiche dopo
const after = db.prepare('SELECT COUNT(*) as cnt FROM links').get().cnt;
console.log('\nDOPO:');
console.log(' Link totali:', after);
console.log(' Eliminati:', before.total - after);
db.close();
console.log('\n=== CLEANUP COMPLETATO ===');