diff --git a/migrations/20241223103243-fix-and-add-resourceType-to-lessons.js b/migrations/20241223103243-fix-and-add-resourceType-to-lessons.js new file mode 100644 index 00000000..ddfe0756 --- /dev/null +++ b/migrations/20241223103243-fix-and-add-resourceType-to-lessons.js @@ -0,0 +1,17 @@ +module.exports = { + async up(db) { + const lessonsCollection = db.collection('lessons') + + await lessonsCollection.updateMany({ resourceType: 'lessons' }, { $set: { resourceType: 'lesson' } }) + + await lessonsCollection.updateMany({ resourceType: { $exists: false } }, { $set: { resourceType: 'lesson' } }) + }, + + async down(db) { + const lessonsCollection = db.collection('lessons') + + await lessonsCollection.updateMany({ resourceType: 'lesson' }, { $set: { resourceType: 'lessons' } }) + + await lessonsCollection.updateMany({ resourceType: 'lesson' }, { $unset: { resourceType: '' } }) + } +} diff --git a/src/test/migrations/20241223103243-fix-and-add-resourceType-to-lessons.spec.js b/src/test/migrations/20241223103243-fix-and-add-resourceType-to-lessons.spec.js new file mode 100644 index 00000000..9bcac39f --- /dev/null +++ b/src/test/migrations/20241223103243-fix-and-add-resourceType-to-lessons.spec.js @@ -0,0 +1,96 @@ +const mongoose = require('mongoose') +const Lessons = require('~/models/lesson') +const { serverInit, serverCleanup, stopServer } = require('~/test/setup') +const migration = require('@root/migrations/20241223103243-fix-and-add-resourceType-to-lessons') + +describe('20241223082318-update-resource-type-in-lessons', () => { + let server + + beforeAll(async () => { + ;({ server } = await serverInit()) + }) + + afterEach(async () => { + await serverCleanup() + }) + + afterAll(async () => { + await stopServer(server) + }) + + const insertLessons = async (data) => { + await Lessons.insertMany(data) + } + + const getLessons = async () => { + return Lessons.find({}).lean() + } + + const lessonBase = { + content: '

'.concat('Content 1 '.repeat(10), '

'), + description: 'This is a test description.', + author: new mongoose.Types.ObjectId() + } + + it('should update resourceType to "lesson" for documents with "resourceType: lessons" or missing field (up)', async () => { + await insertLessons([ + { + ...lessonBase, + title: 'Lesson 1', + resourceType: 'lesson', + otherField: 'value1' + }, + { + ...lessonBase, + title: 'Lesson 2', + resourceType: 'quiz', + otherField: 'value2' + } + ]) + + await migration.up(mongoose.connection.db) + + const updatedDocs = await getLessons() + + expect(updatedDocs).toHaveLength(2) + + updatedDocs.forEach((doc) => { + if (doc.title === 'Lesson 1') { + expect(doc.resourceType).toBe('lesson') + } else if (doc.title === 'Lesson 2') { + expect(doc.resourceType).toBe('quiz') + } + }) + }) + + it('should revert resourceType to "lessons" or unset it (down)', async () => { + await insertLessons([ + { + ...lessonBase, + title: 'Lesson 1', + resourceType: 'lesson', + otherField: 'value1' + }, + { + ...lessonBase, + title: 'Lesson 2', + resourceType: 'quiz', + otherField: 'value2' + } + ]) + + await migration.down(mongoose.connection.db) + + const revertedDocs = await getLessons() + + expect(revertedDocs).toHaveLength(2) + + revertedDocs.forEach((doc) => { + if (doc.title === 'Lesson 1') { + expect(doc.resourceType).toBe('lessons') + } else if (doc.title === 'Lesson 2') { + expect(doc.resourceType).toBe('quiz') + } + }) + }) +})