This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathremove-dups.ts
79 lines (71 loc) · 2.15 KB
/
remove-dups.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import config from "./config";
const args = require("commander");
const process = require("process");
const { Client } = require("pg");
let Config;
async function main() {
args
.option("--config <path>", "Configuration file", "./config/app.js")
.option("--delete", "Actually delete objects (dry run by default)")
.parse(process.argv);
Config = {
...config,
...config.loadConfig(args.config)
};
const client = await pgConnect();
console.log("deleting duplicate recordings");
// Duplicate recordings are identified by (DeviceId,
// recordingDateTime). The recording with the lowest id will be
// kept. Recordings with an updatedAt timestamp within the last 30
// mins are ignored.
//
// Recordings with a NULL recordingDateTime are also ignored as
// these are typically recent uploads that are awaiting processing.
await client.query("BEGIN");
const res = await client.query(
`DELETE FROM "Recordings"
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY "DeviceId", "recordingDateTime" ORDER BY id) as rownum
FROM "Recordings"
WHERE "updatedAt" < now() - INTERVAL '30 minutes'
AND "recordingDateTime" IS NOT NULL
) d
WHERE d.rownum > 1
) RETURNING id, "DeviceId", "type", "recordingDateTime"
`
);
for (const row of res.rows) {
const ts = row.recordingDateTime!.toISOString();
console.log(
`deleted ${row.id}: device=${row.DeviceId} type=${row.type} ts=${ts}`
);
}
if (args.delete) {
await client.query("COMMIT");
console.log(`deleted ${res.rows.length} duplicate recording(s)`);
} else {
await client.query("ROLLBACK");
console.log(
`${res.rows.length} duplicate recording(s) would be deleted (pass --delete to remove)`
);
}
}
async function pgConnect() {
const dbconf = Config.database;
const client = new Client({
host: dbconf.host,
port: dbconf.port,
user: dbconf.username,
password: dbconf.password,
database: dbconf.database
});
await client.connect();
return client;
}
main()
.catch(console.log)
.then(() => {
process.exit(0);
});