add migrations

This commit is contained in:
Josh Harvey 2014-11-19 19:15:13 -05:00
parent c38372a173
commit e650e591d5
6 changed files with 163 additions and 0 deletions

2
.gitignore vendored
View file

@ -22,3 +22,5 @@ dist
.sass-cache .sass-cache
app/bower_components app/bower_components
options.mine.js options.mine.js
.migrate

55
migrations/001-initial.js Normal file
View file

@ -0,0 +1,55 @@
'use strict';
var db = require('./db');
exports.up = function(next){
var sqls = [
'CREATE TABLE IF NOT EXISTS user_config ( ' +
'id serial PRIMARY KEY, ' +
'type text NOT NULL, ' +
'data json NOT NULL ' +
')',
'CREATE TABLE IF NOT EXISTS devices ( ' +
'id serial PRIMARY KEY, ' +
'fingerprint text NOT NULL UNIQUE, ' +
'name text, ' +
'authorized boolean, ' +
'unpair boolean NOT NULL DEFAULT false' +
')',
'CREATE TABLE IF NOT EXISTS pairing_tokens (' +
'id serial PRIMARY KEY, ' +
'token text, ' +
'created timestamp NOT NULL DEFAULT now() ' +
')',
'CREATE TABLE IF NOT EXISTS transactions ( ' +
'id uuid PRIMARY KEY, ' +
'status text NOT NULL, ' +
'tx_hash text, ' +
'device_fingerprint text, ' +
'to_address text NOT NULL, ' +
'satoshis integer, ' +
'currency_code text, ' +
'fiat decimal, ' +
'error text, ' +
'created timestamp NOT NULL DEFAULT now(), ' +
'completed timestamp ' +
')',
'CREATE TABLE IF NOT EXISTS users ( ' +
'id serial PRIMARY KEY, ' +
'userName text NOT NULL UNIQUE, ' +
'salt text NOT NULL, ' +
'pwdHash text NOT NULL ' +
')'
];
db.multi(sqls, next);
};
exports.down = function(next){
next();
};

20
migrations/002-bills.js Normal file
View file

@ -0,0 +1,20 @@
'use strict';
var db = require('./db');
exports.up = function(next){
db.query('CREATE TABLE bills ( ' +
'id uuid PRIMARY KEY, ' +
'device_fingerprint text NOT NULL, ' +
'denomination integer NOT NULL, ' +
'currency_code text NOT NULL, ' +
'satoshis integer NOT NULL, ' +
'to_address text NOT NULL, ' +
'session_id uuid NOT NULL, ' +
'device_time bigint NOT NULL, ' +
'created timestamp NOT NULL DEFAULT now() )', next);
};
exports.down = function(next){
next();
};

View file

@ -0,0 +1,17 @@
'use strict';
var db = require('./db');
exports.up = function(next){
db.query('CREATE TABLE 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);
};
exports.down = function(next){
next();
};

View file

@ -0,0 +1,46 @@
'use strict';
var db = require('./db');
exports.up = function(next){
var sqls = [
'ALTER TABLE transactions DROP IF EXISTS completed',
'ALTER TABLE transactions DROP CONSTRAINT transactions_pkey',
'ALTER TABLE transactions RENAME id TO session_id',
'ALTER TABLE transactions ADD COLUMN id SERIAL',
'UPDATE transactions SET id = DEFAULT',
'ALTER TABLE transactions ADD PRIMARY KEY (id)',
'CREATE INDEX ON transactions (session_id)',
'ALTER TABLE transactions ADD CONSTRAINT transactions_session_status UNIQUE (session_id,status)',
'CREATE TABLE digital_transactions ( ' +
'id serial PRIMARY KEY, ' +
'transaction_id integer REFERENCES transactions(id), ' +
'status text, ' +
'incoming boolean, ' +
'tx_hash text NULL, ' +
'error text NULL, ' +
'created timestamp NOT NULL DEFAULT now(), ' +
'CONSTRAINT digital_transactions_status_txid UNIQUE (status, transaction_id) ' +
')',
'CREATE TABLE dispenses ( ' +
'id serial PRIMARY KEY, ' +
'transaction_id integer UNIQUE REFERENCES transactions(id), ' +
'dispense1 integer NOT NULL, ' +
'reject1 integer NOT NULL, ' +
'count1 integer NOT NULL, ' +
'dispense2 integer NOT NULL, ' +
'reject2 integer NOT NULL, ' +
'count2 integer NOT NULL, ' +
'refill boolean NOT NULL, ' +
'error text NULL, ' +
'created timestamp NOT NULL DEFAULT now() ' +
')'
];
db.multi(sqls, next);
};
exports.down = function(next){
next();
};

23
migrations/db.js Normal file
View file

@ -0,0 +1,23 @@
'use strict';
var pg = require('pg');
var async = require('async');
// TODO: generalize
var conString = 'psql://lamassu:lamassu@localhost/lamassu';
exports.query = function query(sql, cb) {
exports.multi([sql], cb);
};
exports.multi = function multi(sqls, cb) {
pg.connect(conString, function(err, client, done) {
if (err) throw err;
async.eachSeries(sqls, client.query.bind(client), function(err) {
done(true);
if (err) throw err;
cb();
});
});
};