Permalink
Browse files

Adds tests for Amazon Redshift

Redshift is based on Postgres driver. The only difference for now is
that it can't use RETURNING keyword syntax and so it has to do a second
query after INSERT to retrieve the last insert id.
  • Loading branch information...
1 parent 62b67e1 commit de86054715613d6b64eda181cf282ab865fadbfb @dresende committed Feb 21, 2013
Showing with 50 additions and 0 deletions.
  1. +37 −0 lib/Drivers/DML/redshift.js
  2. +13 −0 test/common.js
@@ -0,0 +1,37 @@
+var util = require("util");
+var postgres = require("./postgres");
+
+exports.Driver = Driver;
+
+function Driver(config, connection, opts) {
+ postgres.Driver.call(this, config, connection, opts);
+}
+
+util.inherits(Driver, postgres.Driver);
+
+Driver.prototype.insert = function (table, data, cb) {
+ this.QueryInsert
+ .clear()
+ .table(table);
+ for (var k in data) {
+ this.QueryInsert.set(k, data[k]);
+ }
+ if (this.opts.debug) {
+ require("../../Debug").sql('postgres', this.QueryInsert.build());
+ }
+ this.db.query(this.QueryInsert.build(), function (err, result) {
+ if (err) {
+ return cb(err);
+ }
+
+ this.db.query("SELECT LASTVAL() AS id", function (err, result) {
+ if (err) {
+ return cb(err);
+ }
+
+ return cb(null, {
+ id: result.rows[0].id || null
+ });
+ });
+ }.bind(this));
+};
View
@@ -24,6 +24,7 @@ common.getConnectionString = function () {
case 'mysql':
return 'mysql://root@localhost/orm_test';
case 'postgres':
+ case 'redshift':
return 'postgres://postgres@localhost/orm_test';
case 'sqlite':
return 'sqlite://';
@@ -46,6 +47,12 @@ common.getConnectionString = function () {
(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 'sqlite':
return 'sqlite://' + (config.pathname || "");
default:
@@ -64,6 +71,7 @@ common.getModelProperties = function () {
common.createModelTable = function (table, db, cb) {
switch (this.protocol()) {
case "postgres":
+ case "redshift":
db.query("CREATE TEMPORARY TABLE " + table + " (id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL)", cb);
break;
case "sqlite":
@@ -80,6 +88,7 @@ common.createModelTable = function (table, db, cb) {
common.createModel2Table = function (table, db, cb) {
switch (this.protocol()) {
case "postgres":
+ case "redshift":
db.query("CREATE TEMPORARY TABLE " + table + " (id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, assoc_id BIGINT NOT NULL)", cb);
break;
case "sqlite":
@@ -96,6 +105,7 @@ common.createModel2Table = function (table, db, cb) {
common.createModelAssocTable = function (table, assoc, db, cb) {
switch (this.protocol()) {
case "postgres":
+ case "redshift":
db.query("CREATE TEMPORARY TABLE " + table + "_" + assoc + " (" + table + "_id BIGINT NOT NULL, " + assoc + "_id BIGINT NOT NULL, extra_field BIGINT)", cb);
break;
case "sqlite":
@@ -114,6 +124,7 @@ common.insertModelData = function (table, db, data, cb) {
switch (this.protocol()) {
case "postgres":
+ case "redshift":
case "mysql":
query = [];
@@ -143,6 +154,7 @@ common.insertModel2Data = function (table, db, data, cb) {
switch (this.protocol()) {
case "postgres":
+ case "redshift":
case "mysql":
query = [];
@@ -172,6 +184,7 @@ common.insertModelAssocData = function (table, db, data, cb) {
switch (this.protocol()) {
case "postgres":
+ case "redshift":
case "mysql":
query = [];

0 comments on commit de86054

Please sign in to comment.