Skip to content

fix(35): fix rollback incorrectly restoring nested objects when $set targets a dotted path #36

Merged
pp0rtal merged 3 commits into
mainfrom
fix/overwrite
Jun 19, 2026
Merged

fix(35): fix rollback incorrectly restoring nested objects when $set targets a dotted path #36
pp0rtal merged 3 commits into
mainfrom
fix/overwrite

Conversation

@hugop95

@hugop95 hugop95 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

This fix, alongside making sure that the platform gets the latest version of mongo-bulk-data-migration, should ensure that https://github.com/360Learning/platform/pull/82247#pullrequestreview-4525537020 works.

Description

When a migration uses $set with a dotted path (e.g. 'a.b') to overwrite an existing nested object, the rollback restores that field to a wrong value.

Instead of restoring the original nested structure, it produces a flat object with a dotted key in the field name:

// Expected after rollback
{ channels: { email: true } }

// Received after rollback
{ 'channels.email': true }

Why it happened

During rollback, computeRollbackQuery needs to figure out what value to $set back for each key the migration changed.

For a $set: { 'a.b': newValue }, the backup stores the full original document (e.g. { a: { b: { channels: { email: true } } } }). Internally, the backup is flattened into leaf paths for processing, so a.b.channels.email: true becomes one entry.

The bug was in how those leaf paths were reassembled. The code was slicing off the prefix (a.b) to get the remaining path (channels.email) and using that as a plain JS object key:

// ❌
rollbackSet['a.b'] = { 'channels.email': true }

Instead of looking up the original value directly from the backup:

// ✅
rollbackSet['a.b'] = _.get(backup, 'a.b')

@orca-security-eu orca-security-eu Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

@hugop95 hugop95 marked this pull request as ready for review June 18, 2026 23:37
@hugop95 hugop95 requested a review from pp0rtal as a code owner June 18, 2026 23:37
@hugop95

hugop95 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Hi @pp0rtal 👋

I noticed this issue while reviewing a usage of one of our migration scripts (https://github.com/360Learning/platform/pull/82247#pullrequestreview-4525537020).

I've tested in local and that fix allows https://github.com/360Learning/platform/pull/82247 to be unblocked and merged as it is 👍

There is also another similar PR using that script coming soon (cf https://github.com/360Learning/platform/pull/81907#discussion_r3438557511).


The long story short is: we ideally would like to have this merged and used by the platform by the release on next week. 😁

I've allowed myself to bump the package.json and update the changelog, feel free to adjust as needed.

Feel free to merge if everything looks good to you!

@pp0rtal pp0rtal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hugop95 Perfect fix!
Safe to go, No need to wait. I'll publish right now, so you can bump MDBM version in your PR

Thank you for taking the time to fix this tool!

Comment on lines +118 to +121
rollbackSet[setPropertyKeyStartsWith] = _.get(
backup,
setPropertyKeyStartsWith,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hugop95 Ofc we need lodash for nested support here (like for other cases)

@pp0rtal pp0rtal merged commit bfb5d12 into main Jun 19, 2026
8 checks passed
@hugop95 hugop95 deleted the fix/overwrite branch June 19, 2026 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rollback incorrectly restores nested objects when $set targets a dotted path

2 participants