Repository navigation
Clone defaultScope so it doesn't get overwritten in Model#init - #6971
Conversation
|
@broofa, thanks for your PR! By analyzing the history of the files in this pull request, we identified @felixfbecker, @BridgeAR and @janmeier to be potential reviewers. |
|
Verified this is still an issue in v4.0.0-2 |
| this._findAutoIncrementField(); | ||
|
|
||
| this._scope = this.options.defaultScope; | ||
| this._scope = Object.assign({}, this.options.defaultScope); |
There was a problem hiding this comment.
should this be a deep clone?
There was a problem hiding this comment.
Yeah, probably. What's the preferred way of doing that? JSON.parse(JSON.stringify(...))?
|
There are two test failures |
|
Hmm. Integration check failing with In other news, I stumbled across this comment of yours, @felixfbecker, on |
|
That's not my comment, and it should not be relevant for |
|
@felixfbecker, sorry about that. I tracked that comment down to this commit, attributed to @greenkeeperio-bot and @janmeier. So... what's up with that failing integration check? On this project's home page the Travis badge says "build failing", and links here... which calls out this PR. Is Travis testing against all outstanding PRs or something? My apologies if that's a noob question. ... but, as I said above, I see the same error on master (w/out my PR). |
Current coverage is 94.67% (diff: 100%)
|
| this.sequelize = options.sequelize; | ||
|
|
||
| const globalOptions = this.sequelize.options; | ||
| const globalOptions = _.cloneDeep(this.sequelize.options); |
There was a problem hiding this comment.
It appears to me that globalOptions is not used besides in L688. if that is the case, I think we could just modify L689 to read
options = Utils.merge({}, globalOptions.define, options)|
Restarted the jobs and builds are now fixed |
|
Utils.merge copies-by-reference when the target object is undefined, which turns out to not solve the original problem. But _.cloneDeep(globalOptions.define) does the trick. |
| * @return {Model} | ||
| */ | ||
| static init(attributes, options) { // testhint options:none | ||
| options = options || {}; |
There was a problem hiding this comment.
options parameter must stay optional, please keep this line
There was a problem hiding this comment.
FWIW, this is functionally the same since options = {}, fails the options.sequelize check at L681 (precommit, below), but I'll revert it.
| if (globalOptions.define) { | ||
| options = Utils.merge(globalOptions.define, options); | ||
| } | ||
| options = Utils.merge(_.cloneDeep(globalOptions.define), options); |
There was a problem hiding this comment.
What happens now if globalOptions.define is undefined?
There was a problem hiding this comment.
_.cloneDeep(undefined) -> undefined, and Utils.merge() ignores undefined values... so functionally unchanged.
> var Utils = require('./lib/utils.js')
undefined
> var _ = require('lodash')
Expression assignment to _ now disabled.
undefined
> Utils.merge(_.cloneDeep())
{}
There was a problem hiding this comment.
Integration test failing again for unrelated reasons?
Pull Request check-list
(I have not been able to test against
masterdue tonpm installfailing with `gyp: Call to 'pg_config --libdir' returned exit status 127 while in binding.gyp. However this is reproduceable with latest version in npm and the code seems largely unchanged, so I'm assuming this is still an issue...?)npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.
Description of change
https://gist.github.com/broofa/cee97d28e3923ad62438561438f7e12a is a standalone test case showing how sequelized.options.define.defaultScope.options.attributes is overwritten when defining a model. It logs the state of of this property before, between, and after doing two define() calls. The output I see (Sequelized v3.27.0) is this:
Cloning defaultScope in Model.init() as per this PR fixes this, however I'm not 100% sure this is the correct solution. (If nothing else, doing a deep-copy would probably be more resistent to similar bugs emerging in the future).