Skip to content

Commit

Permalink
Add migration to remove NID field from user
Browse files Browse the repository at this point in the history
A requirement from client to remove the NID field since most of the contries are not really using it.

#6830
  • Loading branch information
Siyasanga Mtshokotsha authored and Siyasanga Mtshokotsha committed Oct 8, 2024
1 parent 08556a2 commit 1d83f88
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* OpenCRVS is also distributed under the terms of the Civil Registration
* & Healthcare Disclaimer located at http://opencrvs.org/license.
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/

import { Db, MongoClient } from 'mongodb'

export const up = async (db: Db, client: MongoClient) => {
// Add migration logic for applying changes to the database
// This code will be executed when running the migration
// It can include creating collections, modifying documents, etc.
const session = client.startSession()
try {
await db.collection('users').updateMany(
{ identifiers: { $exists: true } }, // Only affect documents that have the identifiers field
{ $unset: { identifiers: '' } } // Remove the identifiers field
)
} finally {
await session.endSession()
}
}

export const down = async (db: Db, client: MongoClient) => {
// Add migration logic for reverting changes made by the up() function
// This code will be executed when rolling back the migration
// It should reverse the changes made in the up() function
const session = client.startSession()
try {
await db.collection('users').updateMany(
{ identifiers: { $exists: false } }, // Only affect documents that no longer have the identifiers field
{ $set: { identifiers: [] } } // Re-add identifiers as an empty array
)
} finally {
await session.endSession()
}
}

0 comments on commit 1d83f88

Please sign in to comment.