-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from manipalutsav/feature-db-check
Added db check
- Loading branch information
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const dotenv = require("dotenv"); | ||
const mongoose = require("mongoose"); | ||
const readline = require('node:readline'); | ||
const chalk = require("chalk"); | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const ask = async (question) => { | ||
return new Promise((res) => { | ||
rl.question(question, res); | ||
}); | ||
} | ||
|
||
|
||
dotenv.config(); | ||
|
||
const GAP = (n = 1) => console.log("\n".repeat(n)); | ||
|
||
//Models | ||
const EventModel = require("../src/models/Event"); | ||
const RoundModel = require("../src/models/Round"); | ||
|
||
const ip = process.env.MONGODB_IP || "127.0.0.1"; | ||
const port = process.env.MONGODB_PORT; | ||
const host = ip + ":" + port; | ||
const uri = "mongodb://" + host; | ||
const options = { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
dbName: process.env.MONGODB_DATABASE, | ||
}; | ||
|
||
(async () => { | ||
console.log(`Connecting to ${uri}`) | ||
await mongoose.connect(uri, options); | ||
console.log("Connected!") | ||
|
||
//Add your check functions to this list | ||
checkFns = [ | ||
removeMissingRoundIds | ||
]; | ||
|
||
for (let i = 0; i < checkFns.length; i++) { | ||
GAP(4); | ||
console.log(`${i + 1}/${checkFns.length}`); | ||
await checkFns[i](); | ||
} | ||
|
||
GAP(4); | ||
console.log("All checks done. Exiting.."); | ||
await mongoose.disconnect(); | ||
process.exit(0); | ||
})(); | ||
|
||
process.on('SIGINT', async () => { | ||
await mongoose.disconnect(); | ||
console.log("Disconnected!"); | ||
}); | ||
|
||
const removeMissingRoundIds = async () => { | ||
|
||
console.log("Finding event rounds which are in event model but missing in round model.") | ||
let events = await EventModel.find(); | ||
let rounds = await RoundModel.find(); | ||
let missing = []; | ||
|
||
GAP(); | ||
events.forEach(event => { | ||
event.rounds.forEach(roundId => { | ||
if (!rounds.find(round => round.id == roundId)) { | ||
console.log(`Missing round ${roundId} for event ${event.name}(id:${event.id})`); | ||
missing.push({ eventId: event.id, roundId }); | ||
} | ||
}) | ||
}) | ||
GAP(); | ||
|
||
if (missing.length > 0) { | ||
let ans = await ask("Do you want to delete the round ids from event model for these missing rounds ?(yes/no)"); | ||
console.log(ans); | ||
if (ans != "yes") { | ||
console.log("Skipping..."); | ||
return; | ||
} | ||
await Promise.all(missing.map(async i => { | ||
let event = events.find(event => event.id == i.eventId); | ||
let index = event.rounds.indexOf(i.roundId); | ||
console.log(event); | ||
event.rounds.splice(index, 1); | ||
console.log(event); | ||
return await event.save(); | ||
})); | ||
console.log("Done"); | ||
} | ||
else { | ||
console.log("No issues found."); | ||
} | ||
} |