-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add-contact-id-to-user-docs
migration
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const db = require('../db'); | ||
const logger = require('../logger'); | ||
|
||
const BATCH_SIZE = 1; | ||
|
||
const runBatch = async (skip = 0) => { | ||
const options = { | ||
include_docs: true, | ||
limit: BATCH_SIZE, | ||
key: ['user-settings'], | ||
skip, | ||
}; | ||
const { rows } = await db.medic.query('medic-client/doc_by_type', options); | ||
if (!rows.length) { | ||
return; | ||
} | ||
|
||
for (const row of rows) { | ||
const { _id, contact_id } = row.doc; | ||
try { | ||
const user = await db.users.get(_id); | ||
user.contact_id = contact_id; | ||
await db.users.put(user); | ||
} catch (error) { | ||
if (error.status !== 404) { | ||
throw error; | ||
} | ||
logger.warn(`User with id "${_id}" does not exist anymore, skipping it.`); | ||
} | ||
} | ||
|
||
if (rows.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, | ||
}; |