不要怂,就是干,撸起袖子干!

You need to sign in or sign up before continuing.
Commit 781d5288 by Sushant Committed by GitHub

fix(datatypes): Sequelize.NOW set milliseconds to zero (#8896)

1 parent e5a9efd7
...@@ -989,7 +989,7 @@ class Model { ...@@ -989,7 +989,7 @@ class Model {
} }
if (definition.hasOwnProperty('defaultValue')) { 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) { if (definition.hasOwnProperty('unique') && definition.unique) {
...@@ -2838,7 +2838,7 @@ class Model { ...@@ -2838,7 +2838,7 @@ class Model {
static _getDefaultTimestamp(attr) { static _getDefaultTimestamp(attr) {
if (!!this.rawAttributes[attr] && !!this.rawAttributes[attr].defaultValue) { 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; return undefined;
} }
...@@ -3069,24 +3069,24 @@ class Model { ...@@ -3069,24 +3069,24 @@ class Model {
} }
if (this.constructor._timestampAttributes.createdAt && defaults[this.constructor._timestampAttributes.createdAt]) { 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]; delete defaults[this.constructor._timestampAttributes.createdAt];
} }
if (this.constructor._timestampAttributes.updatedAt && defaults[this.constructor._timestampAttributes.updatedAt]) { 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]; delete defaults[this.constructor._timestampAttributes.updatedAt];
} }
if (this.constructor._timestampAttributes.deletedAt && defaults[this.constructor._timestampAttributes.deletedAt]) { 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]; delete defaults[this.constructor._timestampAttributes.deletedAt];
} }
if (Object.keys(defaults).length) { if (Object.keys(defaults).length) {
for (key in defaults) { for (key in defaults) {
if (values[key] === undefined) { 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]; delete values[key];
} }
} }
......
...@@ -299,7 +299,7 @@ function removeCommentsFromFunctionString(s) { ...@@ -299,7 +299,7 @@ function removeCommentsFromFunctionString(s) {
} }
exports.removeCommentsFromFunctionString = removeCommentsFromFunctionString; exports.removeCommentsFromFunctionString = removeCommentsFromFunctionString;
function toDefaultValue(value) { function toDefaultValue(value, dialect) {
if (typeof value === 'function') { if (typeof value === 'function') {
const tmp = value(); const tmp = value();
if (tmp instanceof DataTypes.ABSTRACT) { if (tmp instanceof DataTypes.ABSTRACT) {
...@@ -312,7 +312,7 @@ function toDefaultValue(value) { ...@@ -312,7 +312,7 @@ function toDefaultValue(value) {
} else if (value instanceof DataTypes.UUIDV4) { } else if (value instanceof DataTypes.UUIDV4) {
return uuid.v4(); return uuid.v4();
} else if (value instanceof DataTypes.NOW) { } else if (value instanceof DataTypes.NOW) {
return now(); return now(dialect);
} else if (_.isPlainObject(value) || _.isArray(value)) { } else if (_.isPlainObject(value) || _.isArray(value)) {
return _.clone(value); return _.clone(value);
} else { } else {
......
...@@ -697,6 +697,8 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -697,6 +697,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user).to.be.ok; expect(user).to.be.ok;
expect(user.created_time).to.be.ok; expect(user.created_time).to.be.ok;
expect(user.updated_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);
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!