-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to remove NID field from user
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.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/migration/src/migrations/user-mgnt/20241007100507-remove-nid-field.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |