Skip to content

Commit

Permalink
Insert datetimes with timezone and millisecond precision in sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
janmeier committed Jun 2, 2014
1 parent 43ed872 commit cb2f06d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
6 changes: 4 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] 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] 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] 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.
- [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
- 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)`.
Expand Down
8 changes: 7 additions & 1 deletion lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ module.exports = (function() {
if (metaData.columnTypes[name] === 'DATETIME') {
// we need to convert the timestamps into actual date objects
var val = result[name];

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) {
if (result[name]) {
Expand Down
2 changes: 1 addition & 1 deletion lib/sql-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ SqlString.dateToString = function(date, timeZone, dialect) {
var dt = new Date(date);

// 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');
}

Expand Down
9 changes: 2 additions & 7 deletions test/dao-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,13 +894,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(users[0].username).to.equal("Peter")
expect(users[1].username).to.equal("Paul")

if (dialect === "sqlite") {
expect(moment(users[0].deletedAt).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)
}
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()
})
})
Expand Down
10 changes: 5 additions & 5 deletions test/sqlite/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
needsSequelize: true
}, {
Expand Down Expand Up @@ -315,7 +315,7 @@ if (dialect === 'sqlite') {
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()}],
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 }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',1);"
Expand Down Expand Up @@ -357,7 +357,7 @@ if (dialect === 'sqlite') {
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()}]],
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}]],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL),('foo',1);"
Expand Down Expand Up @@ -394,10 +394,10 @@ if (dialect === 'sqlite') {
updateQuery: [
{
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],
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 }],
expectation: "UPDATE `myTable` SET `name`='foo' WHERE `id`=2"
Expand Down

0 comments on commit cb2f06d

Please sign in to comment.