-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
55 lines (45 loc) · 1.61 KB
/
Copy pathmigrate.js
File metadata and controls
55 lines (45 loc) · 1.61 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
45
46
47
48
49
50
51
52
53
54
55
const { db } = require("./lib/firebase");
async function migrate() {
console.log("Starting migration...");
try {
const statsSnap = await db.ref("stats").once("value");
const allStats = statsSnap.val();
if (!allStats) {
console.log("No stats found to migrate.");
process.exit(0);
}
const appIds = Object.keys(allStats);
console.log(`Found ${appIds.length} apps to migrate.`);
for (const appId of appIds) {
console.log(`\nMigrating app: ${appId}`);
const data = allStats[appId];
const newStats = {
totalPings: data.totalPings || 0,
uniquePings: data.uniqueIds || 0,
lastPing: data.lastPing || 0
};
if (Array.isArray(data.registeredIds) && data.registeredIds.length > 0) {
console.log(` - Converting ${data.registeredIds.length} unique IDs...`);
const ids = data.registeredIds;
const chunkSize = 500;
for (let i = 0; i < ids.length; i += chunkSize) {
const chunk = ids.slice(i, i + chunkSize);
const uniqueUpdates = {};
for (const id of chunk) {
uniqueUpdates[id] = true;
}
await db.ref(`unique_ips/${appId}`).update(uniqueUpdates);
console.log(` - Progress: ${Math.min(i + chunkSize, ids.length)}/${ids.length}`);
}
}
await db.ref(`stats/${appId}`).set(newStats);
console.log(`Migration for ${appId} complete.`);
}
console.log("\nAll migrations finished successfully!");
process.exit(0);
} catch (error) {
console.error("\nMigration failed:", error);
process.exit(1);
}
}
migrate();