From 4a879332ad4406119f423ae7af3b8e43c6e4605b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 22 Dec 2024 18:37:06 +0200 Subject: [PATCH 1/4] created a migration to add resourceType field and remove availability field, added tests to verify the correctness of this migration --- ...type-delete-availability-in-attachments.js | 28 +++++ ...delete-availability-in-attachments.spec.js | 103 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js create mode 100644 src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js diff --git a/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js b/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js new file mode 100644 index 00000000..e0a4c8f9 --- /dev/null +++ b/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js @@ -0,0 +1,28 @@ +module.exports = { + async up(db) { + const attachmentsCollection = db.collection('attachments') + await attachmentsCollection.updateMany({ availability: { $exists: true } }, { $unset: { availability: '' } }) + await attachmentsCollection.updateMany( + { resourceType: { $exists: false } }, + { $set: { resourceType: 'attachment' } } + ) + + await attachmentsCollection.updateMany({ resourceType: 'attachments' }, { $set: { resourceType: 'attachment' } }) + }, + + async down(db) { + const attachmentsCollection = db.collection('attachments') + await attachmentsCollection.updateMany( + { availability: { $exists: false } }, + { + $set: { + availability: { + status: 'open', + date: null + } + } + } + ) + await attachmentsCollection.updateMany({ resourceType: { $exists: true } }, { $unset: { resourceType: '' } }) + } +} diff --git a/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js b/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js new file mode 100644 index 00000000..5a12f23b --- /dev/null +++ b/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js @@ -0,0 +1,103 @@ +const { MongoClient, ObjectId } = require('mongodb') +const { up, down } = require('@root/migrations/20241222143712-add-resource-type-delete-availability-in-attachments') + +require('~/initialization/envSetup') +const { + config: { MONGODB_URL } +} = require('~/configs/config') + +const collectionName = 'attachments' + +const url = MONGODB_URL.slice(0, MONGODB_URL.lastIndexOf('/')) +const databaseName = MONGODB_URL.slice(MONGODB_URL.lastIndexOf('/') + 1) + +describe('20241222143712-add-resource-type-delete-availability-in-attachments', () => { + let client, database + + beforeAll(async () => { + client = new MongoClient(url) + await client.connect() + database = client.db(databaseName) + database.collection(collectionName).deleteMany({}) + }) + + afterAll(async () => { + database.collection(collectionName).deleteMany({}) + await client.close() + }) + + test('should remove availability field when it exists', async () => { + const testId = new ObjectId() + const testDocument = { + _id: testId, + availability: { status: 'open', date: null } + } + await database.collection(collectionName).insertOne(testDocument) + + const resultBefore = await database.collection(collectionName).findOne({ _id: testId }) + expect(resultBefore).toStrictEqual(testDocument) + + await up(database) + + const resultAfter = await database.collection(collectionName).findOne({ availability: { $exists: true } }) + expect(resultAfter).toBeNull() + }) + + test('should set resourceType when it is missing', async () => { + const testId = new ObjectId() + const testDocument = { + _id: testId + } + await database.collection(collectionName).insertOne(testDocument) + + await up(database) + + const resultAfter = await database.collection(collectionName).findOne({ _id: testId }) + expect(resultAfter.resourceType).toBe('attachment') + }) + + test('should not add resourceType if already exists', async () => { + const testId = new ObjectId() + const testDocument = { + _id: testId, + resourceType: 'attachments' + } + await database.collection(collectionName).insertOne(testDocument) + + await up(database) + + const resultAfter = await database.collection(collectionName).findOne({ _id: testId }) + expect(resultAfter.resourceType).toBe('attachment') + }) + + test('should set availability field back when rolling back migration', async () => { + const testId = new ObjectId() + const testDocument = { + _id: testId, + resourceType: 'attachments' + } + await database.collection(collectionName).insertOne(testDocument) + + await down(database) + + const resultAfterDown = await database.collection(collectionName).findOne({ _id: testId }) + expect(resultAfterDown.availability).toEqual({ + status: 'open', + date: null + }) + }) + + test('should remove resourceType field when rolling back migration', async () => { + const testId = new ObjectId() + const testDocument = { + _id: testId, + resourceType: 'attachments' + } + await database.collection(collectionName).insertOne(testDocument) + + await down(database) + + const resultAfterDownResourceType = await database.collection(collectionName).findOne({ _id: testId }) + expect(resultAfterDownResourceType.resourceType).toBeUndefined() + }) +}) From 7ba24dae0af870980b5b79bdaca54cb4e5899ffe Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 22 Dec 2024 18:54:22 +0200 Subject: [PATCH 2/4] added asynchronous deletion of attachments at the beginning and end of tests --- ...d-resource-type-delete-availability-in-attachments.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js b/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js index 5a12f23b..073c2723 100644 --- a/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js +++ b/src/test/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.spec.js @@ -18,11 +18,11 @@ describe('20241222143712-add-resource-type-delete-availability-in-attachments', client = new MongoClient(url) await client.connect() database = client.db(databaseName) - database.collection(collectionName).deleteMany({}) + await database.collection(collectionName).deleteMany({}) }) afterAll(async () => { - database.collection(collectionName).deleteMany({}) + await database.collection(collectionName).deleteMany({}) await client.close() }) From 409ba5c2c5a34241b01501805944b2cde05c7853 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Jan 2025 20:27:35 +0200 Subject: [PATCH 3/4] replaced removal with reverting to resourceType:attachment --- ...2-add-resource-type-delete-availability-in-attachments.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js b/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js index e0a4c8f9..8289f2b7 100644 --- a/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js +++ b/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js @@ -23,6 +23,9 @@ module.exports = { } } ) - await attachmentsCollection.updateMany({ resourceType: { $exists: true } }, { $unset: { resourceType: '' } }) + await attachmentsCollection.updateMany( + { resourceType: { $exists: true } }, + { $unset: { resourceType: 'attachment' } } + ) } } From abad54ac53e2d3a11b7723f9d8806d5952630d1e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 2 Jan 2025 12:23:46 +0200 Subject: [PATCH 4/4] replaced attachment with attachments in down function --- ...3712-add-resource-type-delete-availability-in-attachments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js b/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js index 8289f2b7..8d5d6978 100644 --- a/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js +++ b/migrations/20241222143712-add-resource-type-delete-availability-in-attachments.js @@ -25,7 +25,7 @@ module.exports = { ) await attachmentsCollection.updateMany( { resourceType: { $exists: true } }, - { $unset: { resourceType: 'attachment' } } + { $unset: { resourceType: 'attachments' } } ) } }