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

Commit cb2f06da by Jan Aagaard Meier

Insert datetimes with timezone and millisecond precision in sqlite

1 parent 43ed8726
...@@ -9,11 +9,13 @@ Notice: All 1.7.x changes are present in 2.0.x aswell ...@@ -9,11 +9,13 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- [FEATURE] Support for FOR UPDATE and FOR SHARE statements [#1777](https://github.com/sequelize/sequelize/pull/1777) - [FEATURE] Support for FOR UPDATE and FOR SHARE statements [#1777](https://github.com/sequelize/sequelize/pull/1777)
- [FEATURE] n:m createAssocation now returns the target model instance instead of the join model instance - [FEATURE] n:m createAssocation now returns the target model instance instead of the join model instance
- [FEATURE] Extend the `foreignKey` option for associations to support a full data type definition, and not just a string - [FEATURE] Extend the `foreignKey` option for associations to support a full data type definition, and not just a string
- [FEATURE] Extract CLI into [separate projects](https://github.com/sequelize/cli).
- [FEATURE] Sqlite now inserts dates with millisecond precision
- [BUG] An error is now thrown if an association would create a naming conflict between the association and the foreign key when doing eager loading. Closes [#1272](https://github.com/sequelize/sequelize/issues/1272) - [BUG] An error is now thrown if an association would create a naming conflict between the association and the foreign key when doing eager loading. Closes [#1272](https://github.com/sequelize/sequelize/issues/1272)
- [BUG] Fix logging options for sequelize.sync - [BUG] Fix logging options for sequelize.sync
- [INTERNALS] `bulkDeleteQuery` was removed from the MySQL / abstract query generator, since it was never used internally. Please use `deleteQuery` instead.
- [BUG] find no longer applies limit: 1 if querying on a primary key, should fix a lot of subquery issues. - [BUG] find no longer applies limit: 1 if querying on a primary key, should fix a lot of subquery issues.
- [FEATURE] Extract CLI into [separate projects](https://github.com/sequelize/cli). - [INTERNALS] `bulkDeleteQuery` was removed from the MySQL / abstract query generator, since it was never used internally. Please use `deleteQuery` instead.
#### Breaking changes #### Breaking changes
- Sequelize now returns promises instead of its custom event emitter from most calls. This affects methods that return multiple values (like `findOrCreate` or `findOrInitialize`). If your current callbacks do not accept the 2nd success parameter you might be seeing an array as the first param. Either use `.spread()` for these methods or add another argument to your callback: `.success(instance)` -> `.success(instance, created)`. - Sequelize now returns promises instead of its custom event emitter from most calls. This affects methods that return multiple values (like `findOrCreate` or `findOrInitialize`). If your current callbacks do not accept the 2nd success parameter you might be seeing an array as the first param. Either use `.spread()` for these methods or add another argument to your callback: `.success(instance)` -> `.success(instance, created)`.
......
...@@ -73,8 +73,14 @@ module.exports = (function() { ...@@ -73,8 +73,14 @@ module.exports = (function() {
if (metaData.columnTypes[name] === 'DATETIME') { if (metaData.columnTypes[name] === 'DATETIME') {
// we need to convert the timestamps into actual date objects // we need to convert the timestamps into actual date objects
var val = result[name]; var val = result[name];
if (val !== null) { if (val !== null) {
result[name] = new Date(val + 'Z'); // Z means UTC if (val.indexOf('+') === -1) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
result[name] = new Date(val + 'Z'); // Z means UTC
} else {
result[name] = new Date(val); // We already have a timezone stored in the string
}
} }
} else if (metaData.columnTypes[name].lastIndexOf('BLOB') !== -1) { } else if (metaData.columnTypes[name].lastIndexOf('BLOB') !== -1) {
if (result[name]) { if (result[name]) {
......
...@@ -162,7 +162,7 @@ SqlString.dateToString = function(date, timeZone, dialect) { ...@@ -162,7 +162,7 @@ SqlString.dateToString = function(date, timeZone, dialect) {
var dt = new Date(date); var dt = new Date(date);
// TODO: Ideally all dialects would work a bit more like this // TODO: Ideally all dialects would work a bit more like this
if (dialect === 'postgres') { if (dialect === 'postgres' || dialect === 'sqlite') {
return moment(dt).zone('+00:00').format('YYYY-MM-DD HH:mm:ss.SSS Z'); return moment(dt).zone('+00:00').format('YYYY-MM-DD HH:mm:ss.SSS Z');
} }
......
...@@ -894,13 +894,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -894,13 +894,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(users[0].username).to.equal("Peter") expect(users[0].username).to.equal("Peter")
expect(users[1].username).to.equal("Paul") expect(users[1].username).to.equal("Paul")
if (dialect === "sqlite") { expect(moment(users[0].deletedAt).utc().format('YYYY-MM-DD h:mm')).to.equal(date)
expect(moment(users[0].deletedAt).format('YYYY-MM-DD h:mm')).to.equal(date) expect(moment(users[1].deletedAt).utc().format('YYYY-MM-DD h:mm')).to.equal(date)
expect(moment(users[1].deletedAt).format('YYYY-MM-DD h:mm')).to.equal(date)
} else {
expect(moment(users[0].deletedAt).utc().format('YYYY-MM-DD h:mm')).to.equal(date)
expect(moment(users[1].deletedAt).utc().format('YYYY-MM-DD h:mm')).to.equal(date)
}
done() done()
}) })
}) })
......
...@@ -182,7 +182,7 @@ if (dialect === 'sqlite') { ...@@ -182,7 +182,7 @@ if (dialect === 'sqlite') {
] ]
} }
}], }],
expectation: "SELECT * FROM `myTable` ORDER BY f1(`myTable`.`id`) DESC, f2(12, 'lalala', '2011-03-27 10:01:55') ASC;", expectation: "SELECT * FROM `myTable` ORDER BY f1(`myTable`.`id`) DESC, f2(12, 'lalala', '2011-03-27 10:01:55.000 +00:00') ASC;",
context: QueryGenerator, context: QueryGenerator,
needsSequelize: true needsSequelize: true
}, { }, {
...@@ -315,7 +315,7 @@ if (dialect === 'sqlite') { ...@@ -315,7 +315,7 @@ if (dialect === 'sqlite') {
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);" expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);"
}, { }, {
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}], arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}],
expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55');" expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55.000 +00:00');"
}, { }, {
arguments: ['myTable', { name: "foo", value: true }], arguments: ['myTable', { name: "foo", value: true }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',1);" expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',1);"
...@@ -357,7 +357,7 @@ if (dialect === 'sqlite') { ...@@ -357,7 +357,7 @@ if (dialect === 'sqlite') {
expectation: "INSERT INTO `myTable` (`name`) VALUES ('''bar'''),('foo');" expectation: "INSERT INTO `myTable` (`name`) VALUES ('''bar'''),('foo');"
}, { }, {
arguments: ['myTable', [{name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, {name: 'bar', birthday: moment("2012-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}]], arguments: ['myTable', [{name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, {name: 'bar', birthday: moment("2012-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}]],
expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55'),('bar','2012-03-27 10:01:55');" expectation: "INSERT INTO `myTable` (`name`,`birthday`) VALUES ('foo','2011-03-27 10:01:55.000 +00:00'),('bar','2012-03-27 10:01:55.000 +00:00');"
}, { }, {
arguments: ['myTable', [{name: "bar", value: null}, {name: 'foo', value: 1}]], arguments: ['myTable', [{name: "bar", value: null}, {name: 'foo', value: 1}]],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL),('foo',1);" expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL),('foo',1);"
...@@ -394,10 +394,10 @@ if (dialect === 'sqlite') { ...@@ -394,10 +394,10 @@ if (dialect === 'sqlite') {
updateQuery: [ updateQuery: [
{ {
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, {id: 2}], arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, {id: 2}],
expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55' WHERE `id`=2" expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55.000 +00:00' WHERE `id`=2"
}, { }, {
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, 2], arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}, 2],
expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55' WHERE `id`=2" expectation: "UPDATE `myTable` SET `name`='foo',`birthday`='2011-03-27 10:01:55.000 +00:00' WHERE `id`=2"
}, { }, {
arguments: ['myTable', { name: 'foo' }, { id: 2 }], arguments: ['myTable', { name: 'foo' }, { id: 2 }],
expectation: "UPDATE `myTable` SET `name`='foo' WHERE `id`=2" expectation: "UPDATE `myTable` SET `name`='foo' WHERE `id`=2"
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!