Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGLOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.7.0 (2026-06-19)

- Patch rollback incorrectly restoring nested objects - https://github.com/360Learning/mongo-bulk-data-migration/pull/36

## 1.6.0 (2026-02-25)

- Automatic resume for empty queries using `FETCH_ALL` - https://github.com/360Learning/mongo-bulk-data-migration/pull/31
Expand Down
19 changes: 19 additions & 0 deletions __tests__/MongoBulkDataMigration.rollback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
collectionName: string;
db: Db;
id: string;
query: any;

Check warning on line 33 in __tests__/MongoBulkDataMigration.rollback.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
projection: any;

Check warning on line 34 in __tests__/MongoBulkDataMigration.rollback.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
logger: LoggerInterface;
};
let loggerMock: LoggerInterface;
Expand Down Expand Up @@ -141,7 +141,7 @@
const dataMigration = new MongoBulkDataMigration<DmDemoCollection>({
...DM_DEFAULT_SETUP,
projection: { a: 1, b: 1 },
update: ({ a, b }) => ({ $set: { a: a + b } }),

Check warning on line 144 in __tests__/MongoBulkDataMigration.rollback.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
options: {
projectionBackupFilter: ['a'],
},
Expand Down Expand Up @@ -206,7 +206,7 @@
const insertedDocuments = await collection.find().toArray();
const dataMigration = new MongoBulkDataMigration({
...DM_DEFAULT_SETUP,
update: (doc: any) => ({ $set: { a: doc.a.b } }),

Check warning on line 209 in __tests__/MongoBulkDataMigration.rollback.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
});

await dataMigration.update();
Expand All @@ -216,6 +216,25 @@
expect(restoredDocuments).toEqual(insertedDocuments);
});

it('should restore a nested object value when the target key already existed', async () => {
await collection.insertOne({
a: { source: { x: { nested: 1 } }, target: { x: { nested: 9 } } },
});
const insertedDocuments = await collection.find().toArray();
const dataMigration = new MongoBulkDataMigration({
...DM_DEFAULT_SETUP,
projection: { a: 1 },
query: { 'a.source': { $exists: true } },
update: { $set: { 'a.target': { x: { nested: 1 } } } },
});

await dataMigration.update();
await dataMigration.rollback();

const restoredDocuments = await collection.find().toArray();
expect(restoredDocuments).toEqual(insertedDocuments);
});

it('should restore removed documents', async () => {
await collection.insertMany([{ key: 1 }, { key: 2 }, { key: 3 }]);
const insertedDocuments = await collection.find().toArray();
Expand Down
13 changes: 13 additions & 0 deletions __tests__/lib/computeRollbackQuery.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ describe('computeRollbackQuery', () => {
$set: { 'nested.array': ['a', 'b'] },
});
});

it('should set back the nested original object value when the target already existed', async () => {
const updateQuery = {
$set: { 'a.b': { channels: { email: false, teams: true } } },
};
const backup = { a: { b: { channels: { email: true } } } };

const restoreQuery = computeRollbackQuery(updateQuery, backup);

expect(restoreQuery).toEqual({
$set: { 'a.b': { channels: { email: true } } },
});
});
});

describe('resulting from an $unset', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@360-l/mongo-bulk-data-migration",
"version": "1.6.0",
"version": "1.7.0",
"description": "MongoDB bulk data migration for node scripts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
9 changes: 4 additions & 5 deletions src/lib/computeRollbackQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ function computeRollbackSet(
(setProperty) => key.startsWith(`${setProperty}.`),
);
if (setPropertyKeyStartsWith) {
const keyToSet = key.slice(setPropertyKeyStartsWith.length + 1);
rollbackSet[setPropertyKeyStartsWith] = {
...(rollbackSet[setPropertyKeyStartsWith] ?? {}),
[keyToSet]: value,
};
rollbackSet[setPropertyKeyStartsWith] = _.get(
backup,
setPropertyKeyStartsWith,
);
Comment on lines +118 to +121

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)

return rollbackSet;
}

Expand Down
Loading