Add special handling of zero date/time entries

Per MySQL docs
(http://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html):

  "MySQL permits you to store a “zero” value of '0000-00-00' as a
  “dummy date.” This is in some cases more convenient than using NULL
  values, and uses less data and index space. To disallow '0000-00-00',
  enable the NO_ZERO_DATE mode.

  “Zero” date or time values used through Connector/ODBC are converted
  automatically to NULL because ODBC cannot handle such values."

As we are not using Connector/ODBC we need to handle this ourself.
This commit is contained in:
Carl Fürstenberg 2016-07-26 13:39:52 +02:00
parent 0cb17dc0ab
commit 1ee79768f2
2 changed files with 26 additions and 1 deletions

View File

@ -405,7 +405,15 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
val = String(val); val = String(val);
break; break;
case 'Date': case 'Date':
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
// MySQL allows, unless NO_ZERO_DATE is set, dummy date/time entries
// new Date() will return Invalid Date for those, so we need to handle
// those separate.
if (val == '0000-00-00 00:00:00') {
val = null;
} else {
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
}
break; break;
case 'Boolean': case 'Boolean':
val = Boolean(val); val = Boolean(val);

View File

@ -318,6 +318,23 @@ describe('migrations', function () {
}); });
}); });
it('should map zero dateTime into null', function (done) {
query('INSERT INTO `DateData` ' +
'(`dateTime`, `timestamp`) ' +
'VALUES("0000-00-00 00:00:00", "0000-00-00 00:00:00") ',
function (err, ret) {
should.not.exists(err);
DateData.findById(ret.insertId, function (err, dateData) {
should(dateData.dateTime)
.be.null();
should(dateData.timestamp)
.be.null();
done();
});
});
});
it('should report errors for automigrate', function() { it('should report errors for automigrate', function() {
db.automigrate('XYZ', function(err) { db.automigrate('XYZ', function(err) {
assert(err); assert(err);