Skip to content

Commit

Permalink
fix: add db migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
justynoh committed Feb 15, 2024
1 parent 7aca689 commit 91e2c40
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable */

// Creates a new workflow key in all existing mrf submissions, populated with
// the form's existing workflow. From this point on, workflows should be captured
// when the submission is initially created (i.e. the workflow is started),

// BEFORE
// COUNT existing number of MRF submissions with no workflow key
db.submissions.countDocuments({
submissionType: 'multirespondentSubmission',
workflow: { $exists: false },
})

// UPDATE
db.submissions.aggregate([
{
$match: {
submissionType: 'multirespondentSubmission',
},
},
{
$lookup: {
from: 'forms',
localField: 'form',
foreignField: '_id',
as: 'forms',
},
},
{
$addFields: {
workflow: { $first: '$forms.workflow' },
},
},
{
$merge: {
into: 'submissions',
on: '_id',
whenNotMatched: 'discard',
},
},
])

// AFTER
// Count number of MRF submissions with workflow key
// Expect this to match the COUNT in BEFORE
db.submissions.countDocuments({
submissionType: 'multirespondentSubmission',
workflow: { $exists: true },
})

0 comments on commit 91e2c40

Please sign in to comment.