Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ exports.Driver = Driver;
function Driver(config, connection, opts) {
this.config = config || {};
this.opts = opts || {};
this.query = new Query("mysql");
this.customTypes = {};

if (!this.config.timezone) {
// force UTC if not defined, UTC is always better..
this.config.timezone = "Z";
this.config.timezone = "local";
}
this.query = new Query({ dialect: "mysql", timezone: config.timezone });

this.reconnect(null, connection);

Expand Down
3 changes: 3 additions & 0 deletions lib/ORM.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ exports.connect = function (opts, cb) {
return ORM_Error(ErrorCodes.generateError(ErrorCodes.PARAM_MISMATCH, "CONNECTION_URL_EMPTY"), cb);
}
opts = url.parse(opts, true);
for(var k in opts.query) {
opts[k] = opts.query[k];
}
}
if (!opts.database) {
// if (!opts.pathname) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"analyse" : false,
"dependencies": {
"enforce" : "0.1.2",
"sql-query" : "0.1.12",
"sql-query" : "0.1.13",
"hat" : "0.0.3",
"lodash" : "1.3.1"
},
Expand Down
110 changes: 52 additions & 58 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var common = exports;
var path = require('path');
var async = require('async');
var ORM = require('../');
var common = exports;
var path = require('path');
var async = require('async');
var _ = require('lodash');
var util = require('util');
var querystring = require('querystring');
var ORM = require('../');

common.ORM = ORM;

Expand All @@ -13,8 +16,8 @@ common.isTravis = function() {
return Boolean(process.env.CI);
};

common.createConnection = function(cb) {
ORM.connect(this.getConnectionString(), cb);
common.createConnection = function(opts, cb) {
ORM.connect(this.getConnectionString(opts), cb);
};

common.hasConfig = function (proto) {
Expand Down Expand Up @@ -51,58 +54,49 @@ common.getConfig = function () {
}
};

common.getConnectionString = function () {
var url;

if (common.isTravis()) {
switch (this.protocol()) {
case 'mysql':
return 'mysql://root@localhost/orm_test';
case 'postgres':
case 'redshift':
return 'postgres://postgres@localhost/orm_test';
case 'sqlite':
return 'sqlite://';
case 'mongodb':
return 'mongodb://localhost/test';
default:
throw new Error("Unknown protocol");
}
} else {
var config = require("./config")[this.protocol()];

switch (this.protocol()) {
case 'mysql':
return 'mysql://' +
(config.user || 'root') +
(config.password ? ':' + config.password : '') +
'@' + (config.host || 'localhost') +
'/' + (config.database || 'orm_test');
case 'postgres':
return 'postgres://' +
(config.user || 'postgres') +
(config.password ? ':' + config.password : '') +
'@' + (config.host || 'localhost') +
'/' + (config.database || 'orm_test');
case 'redshift':
return 'redshift://' +
(config.user || 'postgres') +
(config.password ? ':' + config.password : '') +
'@' + (config.host || 'localhost') +
'/' + (config.database || 'orm_test');
case 'mongodb':
return 'mongodb://' +
(config.user || '') +
(config.password ? ':' + config.password : '') +
'@' + (config.host || 'localhost') +
'/' + (config.database || 'test');
case 'sqlite':
return 'sqlite://' + (config.pathname || "");
default:
throw new Error("Unknown protocol");
}
}
return url;
common.getConnectionString = function (opts) {
var config, query;
var protocol = this.protocol();

if (common.isTravis()) {
config = {};
} else {
config = require("./config")[protocol];
}

opts = opts || {};
_.defaults(config, {
user : { postgres: 'postgres', redshift: 'postgres' }[protocol] || 'root',
database : { mongodb: 'test' }[protocol] || 'orm_test',
password : '',
host : 'localhost',
pathname : '',
query : {}
});
_.merge(config, opts);
query = querystring.stringify(config.query);

switch (protocol) {
case 'mysql':
case 'postgres':
case 'redshift':
case 'mongodb':
if (common.isTravis()) {
if (protocol == 'redshift') protocol = 'postgres';
return util.format("%s://%s@%s/%s?%s",
protocol, config.user, config.host, config.database, query
);
} else {
return util.format("%s://%s:%s@%s/%s?%s",
protocol, config.user, config.password,
config.host, config.database, query
);
}
case 'sqlite':
return util.format("%s://%s?%s", protocol, config.pathname, query);
default:
throw new Error("Unknown protocol " + protocol);
}
};

common.retry = function (before, run, until, done, args) {
Expand Down
96 changes: 96 additions & 0 deletions test/integration/property-timezones.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var should = require('should');
var helper = require('../support/spec_helper');
var common = require('../common');
var ORM = require('../../');

// Only MySql support for now
if (common.protocol() != 'mysql') return;

describe("Timezones", function() {
var db = null;
var Event = null;

var setup = function (opts) {
return function (done) {
helper.connect({ query: opts.query }, function (connection) {
db = connection;
db.settings.set('instance.cache', false);

Event = db.define("event", {
name : { type: 'text' },
when : { type: 'date', time: true }
});

if (opts.sync) {
return helper.dropSync(Event, done);
} else {
return done();
}
});
};
};

describe("specified", function () {
var a, zones = ['local', '-0734', '+11:22'];

for (a = 0; a < zones.length; a++ ) {
describe(zones[a], function () {
before(setup({ sync: true, query: { timezone: zones[a] } }));

after(function () {
return db.close();
});

it("should get back the same date that was stored", function (done) {
var when = new Date(2013,12,5,5,34,27);

Event.create({ name: "raid fridge", when: when }, function (err) {
should.not.exist(err);

Event.one({ name: "raid fridge" }, function (err, item) {
should.not.exist(err);
when.should.eql(item.when);

done();
});
});
});
});
}
});

describe("different for each connection", function () {
after(function () {
return db.close();
});

it("should get back a correctly offset time", function (done) {
var when = new Date(2013,12,5,5,34,27);

setup({ sync: true, query: { timezone: '+0200' }})(function () {
Event.create({ name: "raid fridge", when: when }, function (err) {
should.not.exist(err);

Event.one({ name: "raid fridge" }, function (err, item) {
should.not.exist(err);
when.should.eql(item.when);

db.close();

setup({ query: { timezone: '+0400' }})(function () {
Event.one({ name: "raid fridge" }, function (err, item) {
var expected = new Date(2013,12,5,3,34,27);

should.not.exist(err);
expected.should.eql(item.when);

done();
});
});
});
});
});
});
});

});
12 changes: 10 additions & 2 deletions test/support/spec_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ var async = require('async');
var should = require('should');

module.exports.connect = function(cb) {
common.createConnection(function (err, conn) {
var opts = {};

if (1 in arguments) {
opts = arguments[0];
cb = arguments[1];
}
common.createConnection(opts, function (err, conn) {
if (err) throw err;
cb(conn);
});
Expand All @@ -21,7 +27,9 @@ module.exports.dropSync = function (models, done) {
item.sync(cb);
});
}, function (err) {
should.not.exist(err);
if (common.protocol() != 'sqlite') {
should.not.exist(err);
}
done(err);
});
};