From 279c296aee95065595a4dfb5a5738bec74cf1dee Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sun, 7 May 2017 18:34:46 +0300 Subject: [PATCH] clean up migrations --- migrations/001-initial.js | 52 +--------------------- migrations/004-transactions-reload.js | 15 +++---- migrations/006-add-machine-config.js | 4 +- migrations/010-cached-requests.js | 4 +- migrations/029-add_valid_to_user_config.js | 1 - migrations/db.js | 47 +++---------------- package.json | 1 + yarn.lock | 4 ++ 8 files changed, 25 insertions(+), 103 deletions(-) diff --git a/migrations/001-initial.js b/migrations/001-initial.js index b262991f..c4ac59ce 100644 --- a/migrations/001-initial.js +++ b/migrations/001-initial.js @@ -1,46 +1,4 @@ -'use strict' - -var db = require('./db') - -var exchanges = { - exchanges: { - settings: { - commission: 1.0, - compliance: { - maximum: { - limit: null - } - } - }, - plugins: { - current: { - BTC: { - ticker: 'bitpay', - transfer: 'bitgo' - }, - email: 'smtp2go', - sms: 'twilio', - notify: [] - }, - settings: { - bitpay: {}, - bitgo: {} - } - } - } -} - -var unit = { - brain: { - locale: { - currency: 'USD', - localeInfo: { - primaryLocale: 'en-US', - primaryLocales: ['en-US'] - } - } - } -} +const db = require('./db') exports.up = function (next) { var sqls = [ @@ -69,13 +27,7 @@ exports.up = function (next) { 'userName text NOT NULL UNIQUE, ' + 'salt text NOT NULL, ' + 'pwdHash text NOT NULL ' + - ')', - - 'INSERT INTO user_config (type, data) ' + - "VALUES ('exchanges', '" + JSON.stringify(exchanges) + "')", - - 'INSERT INTO user_config (type, data) ' + - "VALUES ('unit', '" + JSON.stringify(unit) + "')" + ')' ] db.multi(sqls, next) diff --git a/migrations/004-transactions-reload.js b/migrations/004-transactions-reload.js index d5f57dcd..7ee29b25 100644 --- a/migrations/004-transactions-reload.js +++ b/migrations/004-transactions-reload.js @@ -1,15 +1,14 @@ -'use strict'; +var db = require('./db') -var db = require('./db'); +function singleQuotify (item) { return '\'' + item + '\'' } -function singleQuotify(item) { return '\'' + item + '\''; } - -exports.up = function(next) { +exports.up = function (next) { var stages = ['initial_request', 'partial_request', 'final_request', - 'partial_send', 'deposit', 'dispense_request', 'dispense']. - map(singleQuotify).join(','); + 'partial_send', 'deposit', 'dispense_request', 'dispense'] + .map(singleQuotify).join(',') + var authorizations = ['timeout', 'machine', 'pending', 'rejected', - 'published', 'authorized', 'confirmed'].map(singleQuotify).join(','); + 'published', 'authorized', 'confirmed'].map(singleQuotify).join(',') var sqls = [ 'CREATE TYPE transaction_stage AS ENUM (' + stages + ')', diff --git a/migrations/006-add-machine-config.js b/migrations/006-add-machine-config.js index 5f036250..effa5b58 100644 --- a/migrations/006-add-machine-config.js +++ b/migrations/006-add-machine-config.js @@ -3,10 +3,10 @@ var db = require('./db') exports.up = function (next) { - db.query('CREATE TABLE IF NOT EXISTS machine_configs ( ' + + db.multi(['CREATE TABLE IF NOT EXISTS machine_configs ( ' + 'id serial PRIMARY KEY, ' + 'device_fingerprint text NOT NULL, ' + - 'data json NOT NULL )', next) + 'data json NOT NULL )'], next) } exports.down = function (next) { diff --git a/migrations/010-cached-requests.js b/migrations/010-cached-requests.js index 4dfb0e41..01ca6779 100644 --- a/migrations/010-cached-requests.js +++ b/migrations/010-cached-requests.js @@ -3,7 +3,7 @@ var db = require('./db') exports.up = function (next) { - db.query('CREATE TABLE IF NOT EXISTS cached_responses ( ' + + db.multi(['CREATE TABLE IF NOT EXISTS cached_responses ( ' + 'id serial PRIMARY KEY, ' + 'device_fingerprint text NOT NULL, ' + 'session_id uuid NOT NULL, ' + @@ -12,7 +12,7 @@ exports.up = function (next) { 'body json NOT NULL, ' + 'created timestamptz NOT NULL DEFAULT now(), ' + 'UNIQUE (device_fingerprint, session_id, path, method) ' + - ')', next) + ')'], next) } exports.down = function (next) { diff --git a/migrations/029-add_valid_to_user_config.js b/migrations/029-add_valid_to_user_config.js index e5488df8..1d0c5abb 100644 --- a/migrations/029-add_valid_to_user_config.js +++ b/migrations/029-add_valid_to_user_config.js @@ -2,7 +2,6 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'delete from user_config', 'alter table user_config add column valid boolean not null' ] db.multi(sql, next) diff --git a/migrations/db.js b/migrations/db.js index d07220c9..24779fe9 100644 --- a/migrations/db.js +++ b/migrations/db.js @@ -1,43 +1,10 @@ -'use strict'; +const db = require('../lib/db') +const sequential = require('promise-sequential') -var pg = require('pg'); -var async = require('async'); -var psqlUrl = require('../lib/options').postgresql +module.exports = {multi} -if (!psqlUrl) { - console.log('No postgresql entry in config file') - process.exit(1) +function multi (sqls, cb) { + return sequential(sqls.map(s => db.none(s))) + .then(cb) + .catch(cb) } - -exports.query = function query(sql, cb) { - exports.multi([sql], cb); -}; - -exports.silentQuery = function query(sql, cb) { - pg.connect(psqlUrl, function(err, client, done) { - if (err) { - console.log(err.message); - return cb(err); - } - - client.query(sql, function(err) { - done(true); - cb(err); - }); - }); -}; - -exports.multi = function multi(sqls, cb) { - pg.connect(psqlUrl, function(err, client, done) { - if (err) { - console.log(err.message); - return cb(err); - } - - async.eachSeries(sqls, client.query.bind(client), function(err) { - done(true); - if (err) console.log(err); - cb(err); - }); - }); -}; diff --git a/package.json b/package.json index 5880149a..bf72aa5d 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "pg-promise": "^5.6.4", "pify": "^2.3.0", "pretty-ms": "^2.1.0", + "promise-sequential": "^1.1.1", "ramda": "^0.22.1", "serve-static": "^1.11.1", "socket.io": "^1.7.1", diff --git a/yarn.lock b/yarn.lock index 3bb488c4..5e431b03 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3701,6 +3701,10 @@ process-nextick-args@~1.0.6: version "1.0.7" resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3" +promise-sequential@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/promise-sequential/-/promise-sequential-1.1.1.tgz#f79e8950ef86e7a7a85bf320452643592f6d2fb2" + proxy-addr@~1.1.3: version "1.1.4" resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-1.1.4.tgz#27e545f6960a44a627d9b44467e35c1b6b4ce2f3"