clean up migrations

This commit is contained in:
Josh Harvey 2017-05-07 18:52:33 +03:00
parent 393f45a1b3
commit 94ee178c65
4 changed files with 40 additions and 38 deletions

View file

@ -1,10 +1,8 @@
'use strict'; var db = require('./db')
var db = require('./db'); exports.up = function (next) {
const sql =
exports.up = function(next) { ['CREATE TABLE bills ( ' +
var sql =
'CREATE TABLE bills ( ' +
'id uuid PRIMARY KEY, ' + 'id uuid PRIMARY KEY, ' +
'device_fingerprint text NOT NULL, ' + 'device_fingerprint text NOT NULL, ' +
'denomination integer NOT NULL, ' + 'denomination integer NOT NULL, ' +
@ -13,13 +11,11 @@ exports.up = function(next) {
'to_address text NOT NULL, ' + 'to_address text NOT NULL, ' +
'session_id uuid NOT NULL, ' + 'session_id uuid NOT NULL, ' +
'device_time bigint 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.multi(sql, next)
db.query(sql, next); }
});
};
exports.down = function(next) { exports.down = function (next) {
next(); next()
}; }

View file

@ -1,17 +1,15 @@
'use strict'; var db = require('./db')
var db = require('./db'); exports.up = function (next) {
db.multi(['CREATE TABLE IF NOT EXISTS machine_events ( ' +
exports.up = function(next) {
db.query('CREATE TABLE IF NOT EXISTS machine_events ( ' +
'id uuid PRIMARY KEY, ' + 'id uuid PRIMARY KEY, ' +
'device_fingerprint text NOT NULL, ' + 'device_fingerprint text NOT NULL, ' +
'event_type text NOT NULL, ' + 'event_type text NOT NULL, ' +
'note text, ' + 'note text, ' +
'device_time bigint NOT NULL, ' + 'device_time bigint NOT NULL, ' +
'created timestamp NOT NULL DEFAULT now() )', next); 'created timestamp NOT NULL DEFAULT now() )'], next)
}; }
exports.down = function(next) { exports.down = function (next) {
next(); next()
}; }

View file

@ -59,16 +59,11 @@ exports.up = function (next) {
'created timestamp NOT NULL DEFAULT now() ' + 'created timestamp NOT NULL DEFAULT now() ' +
')', ')',
'CREATE INDEX ON dispenses (device_fingerprint)' 'CREATE INDEX ON dispenses (device_fingerprint)'
]; ]
// Need to call this separately to ignore error db.multi(sqls, next)
// in case transactions doesn't exist }
var renameSql = 'ALTER TABLE transactions RENAME TO transactions_old';
db.silentQuery(renameSql, function() {
db.multi(sqls, next);
});
};
exports.down = function(next) { exports.down = function (next) {
next(); next()
}; }

View file

@ -4,7 +4,20 @@ const sequential = require('promise-sequential')
module.exports = {multi} module.exports = {multi}
function multi (sqls, cb) { function multi (sqls, cb) {
return sequential(sqls.map(s => db.none(s))) const doQuery = s => {
.then(cb) return () => {
.catch(cb) 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)
})
} }