Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a migration for attachments #1081

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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: 'attachments' } }
)
}
}
Original file line number Diff line number Diff line change
@@ -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)
await database.collection(collectionName).deleteMany({})
})

afterAll(async () => {
await 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()
})
})
Loading