Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 10128 #10163

Merged
merged 10 commits into from Apr 22, 2021
Merged

Gh 10128 #10163

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/document.js
Expand Up @@ -2981,7 +2981,6 @@ Document.prototype.$__dirty = function() {
schema: _this.$__path(path)
};
});

// gh-2558: if we had to set a default and the value is not undefined,
// we have to save as well
all = all.concat(this.$__.activePaths.map('default', function(path) {
Expand Down Expand Up @@ -3025,7 +3024,6 @@ Document.prototype.$__dirty = function() {
top.value[arrayAtomicsSymbol].$set = top.value;
}
});

top = lastPath = null;
return minimal;
};
Expand Down
28 changes: 12 additions & 16 deletions lib/model.js
Expand Up @@ -257,7 +257,6 @@ Model.prototype.$__handleSave = function(options, callback) {
if (this.isNew) {
// send entire doc
const obj = this.toObject(saveToObjectOptions);

if ((obj || {})._id === void 0) {
// documents must have an _id else mongoose won't know
// what to update later if more changes are made. the user
Expand All @@ -281,7 +280,6 @@ Model.prototype.$__handleSave = function(options, callback) {

callback(null, ret);
});

this.$__reset();
_setIsNew(this, false);
// Make it possible to retry the insert
Expand All @@ -292,7 +290,6 @@ Model.prototype.$__handleSave = function(options, callback) {
this.$__.inserting = false;

const delta = this.$__delta();

if (delta) {
if (delta instanceof MongooseError) {
callback(delta);
Expand All @@ -319,7 +316,15 @@ Model.prototype.$__handleSave = function(options, callback) {
});
} else {
const optionsWithCustomValues = Object.assign({}, options, saveOptions);
this.constructor.exists(this.$__where(), optionsWithCustomValues)
const where = this.$__where();
if (this.$__schema.options.optimisticConcurrency) {
const key = this.$__schema.options.versionKey;
const val = this.$__getValue(key);
if (val != null) {
where[key] = val;
}
}
this.constructor.exists(where, optionsWithCustomValues)
.then((documentExists) => {
if (!documentExists) {
throw new DocumentNotFoundError(this.$__where(), this.constructor.modelName);
Expand All @@ -333,7 +338,6 @@ Model.prototype.$__handleSave = function(options, callback) {

// store the modified paths before the document is reset
this.$__.modifiedPaths = this.modifiedPaths();

this.$__reset();

_setIsNew(this, false);
Expand All @@ -352,7 +356,6 @@ Model.prototype.$__save = function(options, callback) {
callback(error, this);
});
}

let numAffected = 0;
if (get(options, 'safe.w') !== 0 && get(options, 'w') !== 0) {
// Skip checking if write succeeded if writeConcern is set to
Expand All @@ -368,15 +371,12 @@ Model.prototype.$__save = function(options, callback) {
numAffected = result;
}
}

// was this an update that required a version bump?
if (this.$__.version && !this.$__.inserting) {
const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
this.$__.version = undefined;

const key = this.$__schema.options.versionKey;
const version = this.$__getValue(key) || 0;

if (numAffected <= 0) {
// the update failed. pass an error back
this.$__undoReset();
Expand All @@ -390,7 +390,6 @@ Model.prototype.$__save = function(options, callback) {
this.$__setValue(key, version + 1);
}
}

if (result != null && numAffected <= 0) {
this.$__undoReset();
error = new DocumentNotFoundError(result.$where,
Expand Down Expand Up @@ -474,11 +473,9 @@ Model.prototype.save = function(options, fn) {
if (options.hasOwnProperty('session')) {
this.$session(options.session);
}

this.$__.$versionError = generateVersionError(this, this.modifiedPaths());

fn = this.constructor.$handleCallbackError(fn);

return this.constructor.db.base._promiseOrCallback(fn, cb => {
cb = this.constructor.$wrapCallback(cb);

Expand Down Expand Up @@ -670,7 +667,6 @@ Model.prototype.$__delta = function() {
if (!dirty.length && VERSION_ALL !== this.$__.version) {
return;
}

const where = {};
const delta = {};
const len = dirty.length;
Expand All @@ -683,7 +679,6 @@ Model.prototype.$__delta = function() {
if (get(where, '_id.$__', null) != null) {
where._id = where._id.toObject({ transform: false, depopulate: true });
}

for (; d < len; ++d) {
const data = dirty[d];
let value = data.value;
Expand Down Expand Up @@ -716,7 +711,6 @@ Model.prototype.$__delta = function() {
}

if (divergent.length) continue;

if (value === undefined) {
operand(this, where, delta, data, 1, '$unset');
} else if (value === null) {
Expand Down Expand Up @@ -813,7 +807,9 @@ Model.prototype.$__version = function(where, delta) {

if (where === true) {
// this is an insert
if (key) this.$__setValue(key, delta[key] = 0);
if (key) {
this.$__setValue(key, delta[key] = 0);
}
return;
}

Expand Down
18 changes: 18 additions & 0 deletions test/versioning.test.js
Expand Up @@ -540,6 +540,24 @@ describe('versioning', function() {
});
});

it('should persist correctly when optimisticConcurrency is true gh-10128', function() {
const thingSchema = new Schema({ price: Number }, { optimisticConcurrency: true });
const Thing = db.model('Thing', thingSchema);
return co(function*() {
const thing = yield Thing.create({ price: 1 });
yield thing.save();
assert.equal(thing.__v, 0);
const thing_1 = yield Thing.findById(thing.id);
const thing_2 = yield Thing.findById(thing.id);
thing_1.set({ price: 2 });
yield thing_1.save();
assert.equal(thing_1.__v, 1);
thing_2.set({ price: 1 });
const err = yield thing_2.save().then(() => null, err => err);
assert.equal(err.name, 'DocumentNotFoundError');
});
});

describe('versioning is off', function() {
it('when { safe: false } is set (gh-1520)', function(done) {
const schema1 = new Schema({ title: String }, { safe: false });
Expand Down