Skip to content

Commit 286ab98

Browse files
authored
Merge pull request Automattic#14875 from Automattic/vkarpov15/Automatticgh-14848
fix(transaction): avoid unnecessarily updating initial state in between transactions
2 parents e4e1d66 + e13fc38 commit 286ab98

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

lib/plugins/trackTransaction.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ module.exports = function trackTransaction(schema) {
2727
initialState.atomics = _getAtomics(this);
2828

2929
session[sessionNewDocuments].set(this, initialState);
30-
} else {
31-
const state = session[sessionNewDocuments].get(this);
32-
33-
for (const path of Object.keys(this.$__.activePaths.getStatePaths('modify'))) {
34-
state.modifiedPaths.add(path);
35-
}
36-
state.atomics = _getAtomics(this, state.atomics);
3730
}
3831
});
3932
};

test/docs/transactions.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,57 @@ describe('transactions', function() {
565565
assert.equal(i, 3);
566566
});
567567

568+
it('transaction() avoids duplicating atomic operations (gh-14848)', async function() {
569+
db.deleteModel(/Test/);
570+
const subItemSchema = new mongoose.Schema(
571+
{
572+
name: { type: String, required: true }
573+
},
574+
{ _id: false }
575+
);
576+
const itemSchema = new mongoose.Schema(
577+
{
578+
name: { type: String, required: true },
579+
subItems: { type: [subItemSchema], required: true }
580+
},
581+
{ _id: false }
582+
);
583+
const schema = new mongoose.Schema({
584+
items: { type: [itemSchema], required: true }
585+
});
586+
const Test = db.model('Test', schema);
587+
588+
589+
await Test.createCollection();
590+
await Test.deleteMany({});
591+
592+
const { _id } = await Test.create({
593+
items: [
594+
{ name: 'test1', subItems: [{ name: 'x1' }] },
595+
{ name: 'test2', subItems: [{ name: 'x2' }] }
596+
]
597+
});
598+
599+
let doc = await Test.findById(_id);
600+
601+
doc.items.push({ name: 'test3', subItems: [{ name: 'x3' }] });
602+
603+
let i = 0;
604+
await db.transaction(async(session) => {
605+
await doc.save({ session });
606+
if (++i < 3) {
607+
throw new mongoose.mongo.MongoServerError({
608+
errorLabels: ['TransientTransactionError']
609+
});
610+
}
611+
});
612+
613+
assert.equal(i, 3);
614+
615+
doc = await Test.findById(_id);
616+
assert.equal(doc.items.length, 3);
617+
});
618+
568619
it('doesnt apply schema write concern to transaction operations (gh-11382)', async function() {
569620
db.deleteModel(/Test/);
570621
const Test = db.model('Test', Schema({ status: String }, { writeConcern: { w: 'majority' } }));

test/document.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8177,14 +8177,14 @@ describe('document', function() {
81778177
await person.save();
81788178
});
81798179

8180-
it('set() merge option with double nested', async function () {
8180+
it('set() merge option with double nested', async function() {
81818181
const PersonSchema = new Schema({
81828182
info: {
81838183
address: {
81848184
city: String,
8185-
country: { type: String, default: "UK" },
8185+
country: { type: String, default: 'UK' },
81868186
postcode: String
8187-
},
8187+
}
81888188
}
81898189
});
81908190

@@ -8194,19 +8194,19 @@ describe('document', function() {
81948194
const person = new Person({
81958195
info: {
81968196
address: {
8197-
country: "United States",
8198-
city: "New York"
8199-
},
8197+
country: 'United States',
8198+
city: 'New York'
8199+
}
82008200
}
82018201
});
82028202

8203-
const update = { info: { address: { postcode: "12H" } } };
8203+
const update = { info: { address: { postcode: '12H' } } };
82048204

82058205
person.set(update, undefined, { merge: true });
8206-
8207-
assert.equal(person.info.address.city, "New York");
8208-
assert.equal(person.info.address.postcode, "12H");
8209-
assert.equal(person.info.address.country, "United States");
8206+
8207+
assert.equal(person.info.address.city, 'New York');
8208+
assert.equal(person.info.address.postcode, '12H');
8209+
assert.equal(person.info.address.country, 'United States');
82108210
});
82118211

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

0 commit comments

Comments
 (0)