|
| 1 | +import mongoose from "mongoose"; |
| 2 | + |
| 3 | +function generateUniqueId() { |
| 4 | + return nanoid(); |
| 5 | +} |
| 6 | + |
| 7 | +mongoose.connect(process.env.DB_CONNECTION_STRING, { |
| 8 | + useNewUrlParser: true, |
| 9 | + useUnifiedTopology: true, |
| 10 | +}); |
| 11 | + |
| 12 | +const CourseSchema = new mongoose.Schema( |
| 13 | + { |
| 14 | + domain: { type: mongoose.Schema.Types.ObjectId, required: true }, |
| 15 | + courseId: { type: String, required: true, default: generateUniqueId }, |
| 16 | + creatorId: { type: String, required: true }, |
| 17 | + groups: [ |
| 18 | + { |
| 19 | + _id: { |
| 20 | + type: String, |
| 21 | + required: true, |
| 22 | + default: generateUniqueId, |
| 23 | + }, |
| 24 | + }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + { |
| 28 | + timestamps: true, |
| 29 | + }, |
| 30 | +); |
| 31 | +const Course = mongoose.model("Course", CourseSchema); |
| 32 | + |
| 33 | +const LessonSchema = new mongoose.Schema({ |
| 34 | + domain: { type: mongoose.Schema.Types.ObjectId, required: true }, |
| 35 | + lessonId: { type: String, required: true, default: generateUniqueId }, |
| 36 | + creatorId: { type: String, required: true }, |
| 37 | + courseId: { type: String, required: true }, |
| 38 | + groupId: { type: String, required: true }, |
| 39 | +}); |
| 40 | + |
| 41 | +const Lesson = mongoose.model("Lesson", LessonSchema); |
| 42 | + |
| 43 | +async function migrateLessonCreatorIdToUserId() { |
| 44 | + const courses = await Course.find({}); |
| 45 | + for (const course of courses) { |
| 46 | + console.log(`Updating lessons for course ${course.courseId}`); |
| 47 | + await Lesson.updateMany( |
| 48 | + { |
| 49 | + domain: course.domain, |
| 50 | + courseId: course.courseId, |
| 51 | + }, |
| 52 | + { |
| 53 | + $set: { |
| 54 | + creatorId: course.creatorId, |
| 55 | + }, |
| 56 | + }, |
| 57 | + ); |
| 58 | + console.log(`Updated lessons for course ${course.courseId}`); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +async function deleteOrphanLessons() { |
| 63 | + const courses = await Course.find({}).lean(); |
| 64 | + for (const course of courses) { |
| 65 | + const groupsIds = course.groups.map((group) => group._id); |
| 66 | + const lessons = await Lesson.find({ |
| 67 | + domain: course.domain, |
| 68 | + courseId: course.courseId, |
| 69 | + }).lean(); |
| 70 | + |
| 71 | + const orphanLessons = lessons.filter( |
| 72 | + (lesson) => !groupsIds.includes(lesson.groupId), |
| 73 | + ); |
| 74 | + |
| 75 | + if (orphanLessons.length > 0) { |
| 76 | + console.log( |
| 77 | + `Detected ${orphanLessons.length} orphan lessons for course ${course.courseId}`, |
| 78 | + ); |
| 79 | + const query = { |
| 80 | + _id: { |
| 81 | + $in: orphanLessons.map((lesson) => lesson._id), |
| 82 | + }, |
| 83 | + }; |
| 84 | + await Lesson.deleteMany(query); |
| 85 | + console.log( |
| 86 | + `Deleted ${orphanLessons.length} orphan lessons for course ${course.courseId}`, |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +(async () => { |
| 93 | + await migrateLessonCreatorIdToUserId(); |
| 94 | + await deleteOrphanLessons(); |
| 95 | + mongoose.connection.close(); |
| 96 | +})(); |
0 commit comments