Skip to content

Commit

Permalink
added resourceType migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Popovych committed Dec 27, 2024
1 parent 6127bd0 commit cedf6aa
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
17 changes: 17 additions & 0 deletions migrations/20241223103243-fix-and-add-resourceType-to-lessons.js
Original file line number Diff line number Diff line change
@@ -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: '' } })
}
}
Original file line number Diff line number Diff line change
@@ -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: '<p>'.concat('Content 1 '.repeat(10), '</p>'),
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')
}
})
})
})

0 comments on commit cedf6aa

Please sign in to comment.