Merge pull request #179 from azatoth/zero_datetime

Add special handling of zero date/time entries
This commit is contained in:
Raymond Feng 2016-08-11 09:03:14 -07:00 committed by GitHub
commit 5fec12a00c
2 changed files with 26 additions and 1 deletions

View File

@ -408,7 +408,15 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
val = String(val);
break;
case 'Date':
// 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;
case 'Boolean':
val = Boolean(val);

View File

@ -338,6 +338,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() {
db.automigrate('XYZ', function(err) {
assert(err);