-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…8928) Co-authored-by: Joshua Kuestersteffen <[email protected]> Co-authored-by: Diana Barsan <[email protected]>
- Loading branch information
1 parent
61cf2ba
commit 5e9032d
Showing
17 changed files
with
1,003 additions
and
187 deletions.
There are no files selected for viewing
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
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,55 @@ | ||
const db = require('../db'); | ||
const logger = require('../logger'); | ||
|
||
const BATCH_SIZE = 100; | ||
|
||
const getUserSettingsDocs = async (skip = 0) => db.medic.query('medic-client/doc_by_type', { | ||
include_docs: true, | ||
limit: BATCH_SIZE, | ||
key: ['user-settings'], | ||
skip, | ||
}).then(({ rows }) => rows.map(({ doc }) => doc)); | ||
|
||
const getUpdatedUserDoc = (userSettingsDocs) => (userDoc, index) => { | ||
const { _id, contact_id } = userSettingsDocs[index]; | ||
if (!userDoc) { | ||
logger.warn(`Could not find user with id "${_id}". Skipping it.`); | ||
return null; | ||
} | ||
return { ...userDoc, contact_id }; | ||
}; | ||
|
||
const updateUsersDatabase = async (userSettingsDocs) => db.users.allDocs({ | ||
include_docs: true, | ||
keys: userSettingsDocs.map(doc => doc._id), | ||
}).then(({ rows }) => { | ||
const updatedUsersDocs = rows | ||
.map(({ doc }) => doc) | ||
.map(getUpdatedUserDoc(userSettingsDocs)) | ||
.filter(Boolean); | ||
if (!updatedUsersDocs.length) { | ||
return; | ||
} | ||
return db.users.bulkDocs(updatedUsersDocs); | ||
}); | ||
|
||
const runBatch = async (skip = 0) => { | ||
const userSettingsDocs = await getUserSettingsDocs(skip); | ||
if (!userSettingsDocs.length) { | ||
return; | ||
} | ||
|
||
await updateUsersDatabase(userSettingsDocs); | ||
|
||
if (userSettingsDocs.length < BATCH_SIZE) { | ||
return; | ||
} | ||
|
||
return runBatch(skip + BATCH_SIZE); | ||
}; | ||
|
||
module.exports = { | ||
name: 'add-contact-id-to-user-docs', | ||
created: new Date(2024, 5, 2), | ||
run: runBatch, | ||
}; |
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
115 changes: 115 additions & 0 deletions
115
api/tests/integration/migrations/add-contact-id-to-user-docs.js
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,115 @@ | ||
const { expect } = require('chai'); | ||
const utils = require('./utils'); | ||
const db = require('../../../src/db'); | ||
const logger = require('../../../src/logger'); | ||
const sinon = require('sinon'); | ||
|
||
const createUserSettingsDoc = (id, contactId) => { | ||
const userSettings = { | ||
_id: id, | ||
name: id.split(':')[1], | ||
type: 'user-settings', | ||
roles: ['chw'], | ||
facility_id: 'abc' | ||
}; | ||
if (contactId) { | ||
userSettings.contact_id = contactId; | ||
} | ||
return userSettings; | ||
}; | ||
|
||
const createUserDoc = userSettings => { | ||
return { | ||
...userSettings, | ||
type: 'user', | ||
contact_id: null, | ||
}; | ||
}; | ||
|
||
const writeUserDocs = userDocs => db.users.bulkDocs(userDocs); | ||
|
||
const getUserDoc = async (id) => db.users.get(id); | ||
|
||
describe('add-contact-id-to-user migration', function() { | ||
afterEach(() => { | ||
sinon.restore(); | ||
utils.tearDown(); | ||
}); | ||
|
||
it('migrates the contact_id value from user-settings to _users for all users', async () => { | ||
const userDocTuples = Array | ||
.from({ length: 299 }, (_, i) => i) | ||
.map(i => { | ||
const userSettingsDoc = createUserSettingsDoc(`org.couchdb.user:test-chw-${i}`, `contact-${i}`); | ||
return [ | ||
userSettingsDoc, | ||
createUserDoc(userSettingsDoc) | ||
]; | ||
}); | ||
const userSettingsDocs = userDocTuples.map(([ userSettingsDoc ]) => userSettingsDoc); | ||
await utils.initDb(userSettingsDocs); | ||
await writeUserDocs(userDocTuples.map(([, userDoc]) => userDoc)); | ||
|
||
await utils.runMigration('add-contact-id-to-user-docs'); | ||
|
||
await utils.assertDb(userSettingsDocs); | ||
for (const [userSettingsDoc, userDoc] of userDocTuples) { | ||
const updatedUserDoc = await getUserDoc(userDoc._id); | ||
expect(updatedUserDoc).to.deep.include({ ...userDoc, contact_id: userSettingsDoc.contact_id }); | ||
} | ||
}); | ||
|
||
it('skips users that do not exist in _users', async () => { | ||
const userSettingsDocMissing = createUserSettingsDoc('org.couchdb.user:missing-chw', 'contact'); | ||
const userSettingsDocDeleted = createUserSettingsDoc('org.couchdb.user:user-deleted', 'contact'); | ||
await utils.initDb([ userSettingsDocMissing, userSettingsDocDeleted ]); | ||
const userDoc = { | ||
...createUserDoc(userSettingsDocDeleted), | ||
_deleted: true | ||
}; | ||
await writeUserDocs([userDoc]); | ||
sinon.spy(logger, 'warn'); | ||
|
||
await utils.runMigration('add-contact-id-to-user-docs'); | ||
|
||
expect(logger.warn.calledTwice).to.equal(true); | ||
expect(logger.warn.firstCall.args[0]).to | ||
.equal(`Could not find user with id "${userSettingsDocMissing._id}". Skipping it.`); | ||
expect(logger.warn.secondCall.args[0]).to | ||
.equal(`Could not find user with id "${userSettingsDocDeleted._id}". Skipping it.`); | ||
await utils.assertDb([ userSettingsDocMissing, userSettingsDocDeleted ]); | ||
}); | ||
|
||
it('overwrites any existing contact_id value in _users', async () => { | ||
const userSettingsDocs = [ | ||
createUserSettingsDoc('org.couchdb.user:different-contact', 'contact'), | ||
createUserSettingsDoc('org.couchdb.user:no-contact') | ||
]; | ||
await utils.initDb(userSettingsDocs); | ||
const userDocs = [ | ||
{ | ||
...createUserDoc(userSettingsDocs[0]), | ||
contact_id: 'old-contact' | ||
}, | ||
{ | ||
...createUserDoc(userSettingsDocs[1]), | ||
contact_id: 'old-contact', | ||
facility_id: 'different-facility' | ||
}, | ||
]; | ||
await writeUserDocs(userDocs); | ||
|
||
await utils.runMigration('add-contact-id-to-user-docs'); | ||
|
||
await utils.assertDb(userSettingsDocs); | ||
const updatedUserDoc0 = await getUserDoc(userDocs[0]._id); | ||
expect(updatedUserDoc0).to.deep.include({ ...userDocs[0], contact_id: userSettingsDocs[0].contact_id }); | ||
const updatedUserDoc1 = await getUserDoc(userDocs[1]._id); | ||
const expectedUserDoc1 = { ...userDocs[1] }; | ||
delete expectedUserDoc1.contact_id; | ||
expect(updatedUserDoc1).to.deep.include(expectedUserDoc1); | ||
expect(updatedUserDoc1.contact_id).to.be.undefined; | ||
// The _users doc facility_id will not be updated | ||
expect(updatedUserDoc1.facility_id).to.equal('different-facility'); | ||
}); | ||
}); |
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
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
Oops, something went wrong.