Adding support for fractional seconds.
This commit is contained in:
parent
72abcb8e9f
commit
c3a336c18d
|
@ -723,7 +723,8 @@ function mixinMigration(MySQL, mysql) {
|
||||||
dt = numericOptionsByType(p, dt);
|
dt = numericOptionsByType(p, dt);
|
||||||
break;
|
break;
|
||||||
case 'Date':
|
case 'Date':
|
||||||
dt = columnType(p, 'DATETIME'); // Currently doesn't need options.
|
dt = columnType(p, 'DATETIME');
|
||||||
|
dt = dateOptionsByType(p, dt);
|
||||||
break;
|
break;
|
||||||
case 'Boolean':
|
case 'Boolean':
|
||||||
dt = 'TINYINT(1)';
|
dt = 'TINYINT(1)';
|
||||||
|
@ -893,6 +894,31 @@ function mixinMigration(MySQL, mysql) {
|
||||||
return columnType;
|
return columnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dateOptionsByType(p, columnType) {
|
||||||
|
switch (columnType.toLowerCase()) {
|
||||||
|
default:
|
||||||
|
case 'date':
|
||||||
|
break;
|
||||||
|
case 'time':
|
||||||
|
case 'datetime':
|
||||||
|
case 'timestamp':
|
||||||
|
columnType = dateOptions(p, columnType);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return columnType;
|
||||||
|
}
|
||||||
|
|
||||||
|
function dateOptions(p, columnType) {
|
||||||
|
var precision = 0;
|
||||||
|
|
||||||
|
if (p.precision) {
|
||||||
|
precision = Number(p.precision);
|
||||||
|
columnType += '(' + precision + ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
return columnType;
|
||||||
|
}
|
||||||
|
|
||||||
function unsigned(p, columnType) {
|
function unsigned(p, columnType) {
|
||||||
if (p.unsigned) {
|
if (p.unsigned) {
|
||||||
columnType += ' UNSIGNED';
|
columnType += ' UNSIGNED';
|
||||||
|
|
|
@ -412,7 +412,7 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
|
||||||
// MySQL allows, unless NO_ZERO_DATE is set, dummy date/time entries
|
// 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
|
// new Date() will return Invalid Date for those, so we need to handle
|
||||||
// those separate.
|
// those separate.
|
||||||
if (!val || val == '0000-00-00 00:00:00' || val == '0000-00-00') {
|
if (!val || /^0{4}(-00){2}( (00:){2}0{2}(\.0{1,6}){0,1}){0,1}$/.test(val)) {
|
||||||
val = null;
|
val = null;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -17,6 +17,7 @@ describe('MySQL datetime handling', function() {
|
||||||
married: Boolean,
|
married: Boolean,
|
||||||
dob: {type: 'DateString'},
|
dob: {type: 'DateString'},
|
||||||
createdAt: {type: Date, default: Date},
|
createdAt: {type: Date, default: Date},
|
||||||
|
lastLogon: {type: Date, precision: 3, default: Date},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Modifying the connection timezones mid-flight is a pain,
|
// Modifying the connection timezones mid-flight is a pain,
|
||||||
|
@ -92,4 +93,22 @@ describe('MySQL datetime handling', function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow use of fractional seconds', function(done) {
|
||||||
|
var d = new Date('1971-06-22T12:34:56.789Z');
|
||||||
|
return Person.create({
|
||||||
|
name: 'Mr. Pink',
|
||||||
|
gender: 'M',
|
||||||
|
lastLogon: d,
|
||||||
|
}).then(function(inst) {
|
||||||
|
return Person.findById(inst.id);
|
||||||
|
}).then(function(inst) {
|
||||||
|
inst.should.not.eql(null);
|
||||||
|
var lastLogon = new Date(inst.lastLogon);
|
||||||
|
lastLogon.toJSON().should.eql(d.toJSON());
|
||||||
|
return done();
|
||||||
|
}).catch(function(err) {
|
||||||
|
return done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue