/
cleanDatabase.js
118 lines (83 loc) · 3.71 KB
/
cleanDatabase.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const mongoose = require("../backend/db/mongooseServerUtils");
const conn = mongoose.connection;
const ObjectID = require('mongodb').ObjectID
const prompts = require("prompts");
const cliProgress = require('cli-progress');
const createTempDirectory = require("./createTempDirectory");
const waitForDatabase = () => {
return new Promise((resolve, reject) => {
if (conn.readyState !== 1) {
conn.once("open", () => {
resolve();
})
} else {
resolve();
}
})
}
const moveFileChunks = async(fileID, oldDatabaseChunks, newDatabaseChunks) => {
const listChunkCursor = await conn.db.collection(oldDatabaseChunks).find({files_id: ObjectID(fileID)});
for await (const currentChunk of listChunkCursor) {
await conn.db.collection(newDatabaseChunks).insertOne(currentChunk);
}
}
const findFiles = async(oldDatabaseList, oldDatabaseChunks, newDatabaseChunks) => {
const listCursor = await conn.db.collection(oldDatabaseList).find({});
const listCount = await conn.db.collection(oldDatabaseList).find({}).count();
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(listCount, 0);
for await (const currentFile of listCursor) {
const fileID = currentFile._id;
await moveFileChunks(fileID, oldDatabaseChunks, newDatabaseChunks);
progressBar.increment();
}
progressBar.stop();
}
const cleanDatabase = async() => {
console.log("Waiting For Database Connection...");
await waitForDatabase();
console.log("Connected To Database\n");
const userConfimation = await prompts({
type: 'text',
message: "Warning: This will automatically run Backup-Database,\n" +
"overwriting the current Backup. And will also clear all file chunks\n" +
"other than the Data Backup. Then it will move only used file chunks\n" +
"over to the Main Database. If this process fails AFTER the Automatic Backup\n" +
"use the Restore-Database feature. \n" +
"Would you like to continue? (Yes/No)",
name: "value"
})
if (!userConfimation.value || userConfimation.value.toLowerCase() !== "yes") {
console.log("Exiting...")
process.exit()
return;
}
console.log("Creating Temporary Collection...\n");
await createTempDirectory();
console.log("Temporary Collection Completed\n")
console.log("Created New Backup Sucessfully\n")
console.log("Deleting Current Chunks Collection...");
try {
await conn.db.collection("fs.chunks").drop();
} catch (e) {}
console.log("Current Chunk Collection Deleted\n");
console.log("Moving Used Files...");
await findFiles("temp-fs.files", "temp-fs.chunks", "fs.chunks");
console.log("Moved All Used Files\n");
console.log("Creating File Chunks Index...");
await conn.db.collection("fs.chunks").createIndex({ files_id: 1, n: 1 }, { unique: true });
console.log("Created File Chunks Index\n");
console.log("Deleteing Current Transcoded Video Chunks Collection...");
try {
await conn.db.collection("videos.chunks").drop();
} catch (e) {}
console.log("Deleted Current Transcoded Video Chunks Collection\n")
console.log("Moving Used Video Files...");
await findFiles("temp-videos.files", "temp-videos.chunks", "videos.chunks");
console.log("Moved All Used Video Files\n")
console.log("Creating Transcoded Video Chunks Index...");
await conn.db.collection("videos.chunks").createIndex({ files_id: 1, n: 1 }, { unique: true });
console.log("Created Transcoded Video Chunks Index")
process.exit();
}
cleanDatabase();