Skip to content

Commit

Permalink
fix(datatypes): Sequelize.NOW set milliseconds to zero (#8896)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed Jan 14, 2018
1 parent e5a9efd commit 781d528
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ class Model {
}

if (definition.hasOwnProperty('defaultValue')) {
this._defaultValues[name] = _.partial(Utils.toDefaultValue, definition.defaultValue);
this._defaultValues[name] = _.partial(Utils.toDefaultValue, definition.defaultValue, this.sequelize.options.dialect);
}

if (definition.hasOwnProperty('unique') && definition.unique) {
Expand Down Expand Up @@ -2838,7 +2838,7 @@ class Model {

static _getDefaultTimestamp(attr) {
if (!!this.rawAttributes[attr] && !!this.rawAttributes[attr].defaultValue) {
return Utils.toDefaultValue(this.rawAttributes[attr].defaultValue);
return Utils.toDefaultValue(this.rawAttributes[attr].defaultValue, this.sequelize.options.dialect);
}
return undefined;
}
Expand Down Expand Up @@ -3069,24 +3069,24 @@ class Model {
}

if (this.constructor._timestampAttributes.createdAt && defaults[this.constructor._timestampAttributes.createdAt]) {
this.dataValues[this.constructor._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.createdAt]);
this.dataValues[this.constructor._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.createdAt], this.sequelize.options.dialect);
delete defaults[this.constructor._timestampAttributes.createdAt];
}

if (this.constructor._timestampAttributes.updatedAt && defaults[this.constructor._timestampAttributes.updatedAt]) {
this.dataValues[this.constructor._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.updatedAt]);
this.dataValues[this.constructor._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.updatedAt], this.sequelize.options.dialect);
delete defaults[this.constructor._timestampAttributes.updatedAt];
}

if (this.constructor._timestampAttributes.deletedAt && defaults[this.constructor._timestampAttributes.deletedAt]) {
this.dataValues[this.constructor._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.deletedAt]);
this.dataValues[this.constructor._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.deletedAt], this.sequelize.options.dialect);
delete defaults[this.constructor._timestampAttributes.deletedAt];
}

if (Object.keys(defaults).length) {
for (key in defaults) {
if (values[key] === undefined) {
this.set(key, Utils.toDefaultValue(defaults[key]), defaultsOptions);
this.set(key, Utils.toDefaultValue(defaults[key], this.sequelize.options.dialect), defaultsOptions);
delete values[key];
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function removeCommentsFromFunctionString(s) {
}
exports.removeCommentsFromFunctionString = removeCommentsFromFunctionString;

function toDefaultValue(value) {
function toDefaultValue(value, dialect) {
if (typeof value === 'function') {
const tmp = value();
if (tmp instanceof DataTypes.ABSTRACT) {
Expand All @@ -312,7 +312,7 @@ function toDefaultValue(value) {
} else if (value instanceof DataTypes.UUIDV4) {
return uuid.v4();
} else if (value instanceof DataTypes.NOW) {
return now();
return now(dialect);
} else if (_.isPlainObject(value) || _.isArray(value)) {
return _.clone(value);
} else {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/model/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user).to.be.ok;
expect(user.created_time).to.be.ok;
expect(user.updated_time).to.be.ok;
expect(user.created_time.getMilliseconds()).not.to.equal(0);
expect(user.updated_time.getMilliseconds()).not.to.equal(0);
});
});
});
Expand Down

0 comments on commit 781d528

Please sign in to comment.