Skip to content

Commit

Permalink
fix(transaction): avoid unnecessarily updating initial state in betwe…
Browse files Browse the repository at this point in the history
…en transactions
  • Loading branch information
vkarpov15 committed Sep 10, 2024
1 parent d533e9f commit 599dc13
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
7 changes: 0 additions & 7 deletions lib/plugins/trackTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ module.exports = function trackTransaction(schema) {
initialState.atomics = _getAtomics(this);

session[sessionNewDocuments].set(this, initialState);
} else {
const state = session[sessionNewDocuments].get(this);

for (const path of Object.keys(this.$__.activePaths.getStatePaths('modify'))) {
state.modifiedPaths.add(path);
}
state.atomics = _getAtomics(this, state.atomics);
}
});
};
Expand Down
56 changes: 56 additions & 0 deletions test/docs/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,62 @@ describe('transactions', function() {
assert.equal(i, 3);
});

it('transaction() avoids duplicating atomic operations (gh-14848)', async function() {
db.deleteModel(/Test/);
// Define some schemas...
const subItemSchema = new mongoose.Schema(
{

Check failure on line 572 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 572 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
name: { type: String, required: true },

Check failure on line 573 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 573 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
},
{ _id: false }
);

const itemSchema = new mongoose.Schema(
{
name: { type: String, required: true },
subItems: { type: [subItemSchema], required: true },

Check failure on line 581 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 581 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
},
{ _id: false }
);

const schema = new mongoose.Schema({
items: { type: [itemSchema], required: true },

Check failure on line 587 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 587 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
});

// ...and a model
const Test = db.model("Test", schema);

Check failure on line 591 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 591 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote


await Test.createCollection();
await Test.deleteMany({});

const { _id } = await Test.create({
items: [
{ name: "test1", subItems: [{ name: "x1" }] },

Check failure on line 599 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 599 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 599 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 599 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote
{ name: "test2", subItems: [{ name: "x2" }] },

Check failure on line 600 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 600 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 600 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 600 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 600 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Strings must use singlequote

Check failure on line 600 in test/docs/transactions.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
],
});

let doc = await Test.findById(_id);

doc.items.push({ name: "test3", subItems: [{ name: "x3" }] });

let i = 0;
await db.transaction(async(session) => {
await doc.save({ session });
if (++i < 3) {
throw new mongoose.mongo.MongoServerError({
errorLabels: ['TransientTransactionError']
});
}
});

assert.equal(i, 3);

doc = await Test.findById(_id);
assert.equal(doc.items.length, 3);
});

it('doesnt apply schema write concern to transaction operations (gh-11382)', async function() {
db.deleteModel(/Test/);
const Test = db.model('Test', Schema({ status: String }, { writeConcern: { w: 'majority' } }));
Expand Down
22 changes: 11 additions & 11 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8177,14 +8177,14 @@ describe('document', function() {
await person.save();
});

it('set() merge option with double nested', async function () {
it('set() merge option with double nested', async function() {
const PersonSchema = new Schema({
info: {
address: {
city: String,
country: { type: String, default: "UK" },
country: { type: String, default: 'UK' },
postcode: String
},
}
}
});

Expand All @@ -8194,19 +8194,19 @@ describe('document', function() {
const person = new Person({
info: {
address: {
country: "United States",
city: "New York"
},
country: 'United States',
city: 'New York'
}
}
});

const update = { info: { address: { postcode: "12H" } } };
const update = { info: { address: { postcode: '12H' } } };

person.set(update, undefined, { merge: true });
assert.equal(person.info.address.city, "New York");
assert.equal(person.info.address.postcode, "12H");
assert.equal(person.info.address.country, "United States");

assert.equal(person.info.address.city, 'New York');
assert.equal(person.info.address.postcode, '12H');
assert.equal(person.info.address.country, 'United States');
});

it('setting single nested subdoc with timestamps (gh-8251)', async function() {
Expand Down

0 comments on commit 599dc13

Please sign in to comment.