Skip to content

Commit e3422b2

Browse files
authored
Missing down Method in Migration delete-old-fields-in-cooperatives-and-add-new-ones #1136 (#1350)
* add down method to rollback cooperatives * added tests for 20241220213859-delete-old-fields-in-cooperatives-and-add-new-ones.spec * fixed errors and tests * update packages
1 parent 47fbd25 commit e3422b2

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

migrations/20241220213859-delete-old-fields-in-cooperatives-and-add-new-ones.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ function transformSection(section) {
1515
}
1616

1717
if (activity.resourceType) transformedResource.resourceType = activity.resourceType
18-
if (activity.resource.availability) {
18+
if (activity.resource?.availability) {
1919
transformedResource.availability = {
2020
status: activity.resource.availability.status,
2121
date: activity.resource.availability.date
2222
}
2323
}
2424

25-
if (activity.completionStatus) transformedResource.completionStatus = activity.completionStatus
26-
else {
27-
transformedResource.completionStatus = 'active'
28-
}
25+
transformedResource.completionStatus = activity.completionStatus || 'active'
2926

3027
return transformedResource
3128
})
@@ -56,6 +53,11 @@ async function processSections(db, cooperation) {
5653

5754
module.exports = {
5855
async up(db) {
56+
await db
57+
.collection('cooperation')
58+
.aggregate([{ $match: {} }, { $out: 'cooperation_backup' }])
59+
.toArray()
60+
5961
const allUserIds = new Set(
6062
(
6163
await db
@@ -71,14 +73,12 @@ module.exports = {
7173
await db.collection('cooperation').deleteOne({ _id: cooperation._id })
7274
}
7375

74-
if (Array.isArray(cooperation.proficiencyLevel)) {
75-
if (cooperation.proficiencyLevel.length > 0) {
76-
const averageProficiencyLevelIndex = cooperation.proficiencyLevel.length / 2
77-
const averageProficiencyLevel = cooperation.proficiencyLevel[Math.ceil(averageProficiencyLevelIndex)]
78-
await db
79-
.collection('cooperation')
80-
.updateOne({ _id: cooperation._id }, { $set: { proficiencyLevel: averageProficiencyLevel } })
81-
}
76+
if (Array.isArray(cooperation.proficiencyLevel) && cooperation.proficiencyLevel.length > 0) {
77+
const averageProficiencyLevelIndex = cooperation.proficiencyLevel.length / 2
78+
const averageProficiencyLevel = cooperation.proficiencyLevel[Math.ceil(averageProficiencyLevelIndex)]
79+
await db
80+
.collection('cooperation')
81+
.updateOne({ _id: cooperation._id }, { $set: { proficiencyLevel: averageProficiencyLevel } })
8282
}
8383

8484
if (!cooperation.title) {
@@ -93,6 +93,7 @@ module.exports = {
9393
}
9494
await processSections(db, cooperation)
9595
}
96+
9697
await db.collection('cooperation').updateMany(
9798
{
9899
$or: [{ category: { $exists: true } }, { subject: { $exists: true } }, { languages: { $exists: true } }]
@@ -117,6 +118,21 @@ module.exports = {
117118
}
118119
)
119120
},
120-
down() {},
121+
122+
async down(db) {
123+
const backupExists = await db.listCollections({ name: 'cooperation_backup' }).hasNext()
124+
if (!backupExists) {
125+
throw new Error("Backup 'cooperation_backup' not found. Rollback is not possible.")
126+
}
127+
128+
await db.collection('cooperation').drop()
129+
130+
await db
131+
.collection('cooperation_backup')
132+
.aggregate([{ $match: {} }, { $out: 'cooperation' }])
133+
.toArray()
134+
135+
await db.collection('cooperation_backup').drop()
136+
},
121137
transformSection
122138
}

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/migrations/20241220213859-delete-old-fields-in-cooperatives-and-add-new-ones.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,38 @@ describe('20241220213859-delete-old-fields-in-cooperatives-and-add-new-ones', ()
257257

258258
expect(updatedCooperation.sections).toEqual([])
259259
})
260+
test('down should restore the cooperation collection from backup if it exists', async () => {
261+
const backupDoc = {
262+
_id: new ObjectId(),
263+
initiator: validCooperation.initiator,
264+
receiver: validCooperation.receiver,
265+
title: 'Restored from Backup',
266+
proficiencyLevel: 'Advanced',
267+
sections: [],
268+
additionalInfo: 'Restored info'
269+
}
270+
await database.collection('cooperation_backup').insertOne(backupDoc)
271+
272+
await database.collection(cooperationCollection).drop()
273+
274+
await require('@root/migrations/20241220213859-delete-old-fields-in-cooperatives-and-add-new-ones').down(database)
275+
276+
const restoredDoc = await database.collection(cooperationCollection).findOne({ _id: backupDoc._id })
277+
expect(restoredDoc).toBeDefined()
278+
expect(restoredDoc.title).toBe('Restored from Backup')
279+
280+
const backupStillExists = await database.listCollections({ name: 'cooperation_backup' }).hasNext()
281+
expect(backupStillExists).toBe(false)
282+
})
283+
284+
test('down should throw an error if the backup does not exist', async () => {
285+
const backupExists = await database.listCollections({ name: 'cooperation_backup' }).hasNext()
286+
if (backupExists) {
287+
await database.collection('cooperation_backup').drop()
288+
}
289+
290+
await expect(
291+
require('@root/migrations/20241220213859-delete-old-fields-in-cooperatives-and-add-new-ones').down(database)
292+
).rejects.toThrow("Backup 'cooperation_backup' not found. Rollback is not possible.")
293+
})
260294
})

0 commit comments

Comments
 (0)