From 94ee178c653ce1e329e96bcbcb6fbbdb7c179bc1 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sun, 7 May 2017 18:52:33 +0300 Subject: [PATCH] clean up migrations --- migrations/002-bills.js | 24 ++++++++++-------------- migrations/003-device-events.js | 18 ++++++++---------- migrations/004-transactions-reload.js | 17 ++++++----------- migrations/db.js | 19 ++++++++++++++++--- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/migrations/002-bills.js b/migrations/002-bills.js index dd7c0bac..a75f9dcb 100644 --- a/migrations/002-bills.js +++ b/migrations/002-bills.js @@ -1,10 +1,8 @@ -'use strict'; +var db = require('./db') -var db = require('./db'); - -exports.up = function(next) { - var sql = - 'CREATE TABLE bills ( ' + +exports.up = function (next) { + const sql = + ['CREATE TABLE bills ( ' + 'id uuid PRIMARY KEY, ' + 'device_fingerprint text NOT NULL, ' + 'denomination integer NOT NULL, ' + @@ -13,13 +11,11 @@ exports.up = function(next) { 'to_address text NOT NULL, ' + 'session_id uuid NOT NULL, ' + 'device_time bigint NOT NULL, ' + - 'created timestamp NOT NULL DEFAULT now() )'; + 'created timestamp NOT NULL DEFAULT now() )'] - db.silentQuery('ALTER TABLE bills RENAME TO bills_old', function() { - db.query(sql, next); - }); -}; + db.multi(sql, next) +} -exports.down = function(next) { - next(); -}; +exports.down = function (next) { + next() +} diff --git a/migrations/003-device-events.js b/migrations/003-device-events.js index b0d103fe..21c666ef 100644 --- a/migrations/003-device-events.js +++ b/migrations/003-device-events.js @@ -1,17 +1,15 @@ -'use strict'; +var db = require('./db') -var db = require('./db'); - -exports.up = function(next) { - db.query('CREATE TABLE IF NOT EXISTS machine_events ( ' + +exports.up = function (next) { + db.multi(['CREATE TABLE IF NOT EXISTS machine_events ( ' + 'id uuid PRIMARY KEY, ' + 'device_fingerprint text NOT NULL, ' + 'event_type text NOT NULL, ' + 'note text, ' + 'device_time bigint NOT NULL, ' + - 'created timestamp NOT NULL DEFAULT now() )', next); -}; + 'created timestamp NOT NULL DEFAULT now() )'], next) +} -exports.down = function(next) { - next(); -}; +exports.down = function (next) { + next() +} diff --git a/migrations/004-transactions-reload.js b/migrations/004-transactions-reload.js index 7ee29b25..fb5aba8b 100644 --- a/migrations/004-transactions-reload.js +++ b/migrations/004-transactions-reload.js @@ -59,16 +59,11 @@ exports.up = function (next) { 'created timestamp NOT NULL DEFAULT now() ' + ')', 'CREATE INDEX ON dispenses (device_fingerprint)' - ]; + ] - // Need to call this separately to ignore error - // in case transactions doesn't exist - var renameSql = 'ALTER TABLE transactions RENAME TO transactions_old'; - db.silentQuery(renameSql, function() { - db.multi(sqls, next); - }); -}; + db.multi(sqls, next) +} -exports.down = function(next) { - next(); -}; +exports.down = function (next) { + next() +} diff --git a/migrations/db.js b/migrations/db.js index 24779fe9..7cb08347 100644 --- a/migrations/db.js +++ b/migrations/db.js @@ -4,7 +4,20 @@ const sequential = require('promise-sequential') module.exports = {multi} function multi (sqls, cb) { - return sequential(sqls.map(s => db.none(s))) - .then(cb) - .catch(cb) + const doQuery = s => { + return () => { + return db.none(s) + .catch(err => { + console.log(err.stack) + throw err + }) + } + } + + return sequential(sqls.map(doQuery)) + .then(() => cb()) + .catch(err => { + console.log(err.stack) + cb(err) + }) }