From e650e591d5d63e6ea4b5405ef307441ac43e46a8 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Wed, 19 Nov 2014 19:15:13 -0500 Subject: [PATCH] add migrations --- .gitignore | 2 + migrations/001-initial.js | 55 +++++++++++++++++++++++++++ migrations/002-bills.js | 20 ++++++++++ migrations/003-device-events.js | 17 +++++++++ migrations/004-transactions-reload.js | 46 ++++++++++++++++++++++ migrations/db.js | 23 +++++++++++ 6 files changed, 163 insertions(+) create mode 100644 migrations/001-initial.js create mode 100644 migrations/002-bills.js create mode 100644 migrations/003-device-events.js create mode 100644 migrations/004-transactions-reload.js create mode 100644 migrations/db.js diff --git a/.gitignore b/.gitignore index 0bb81cb2..fbcf0587 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ dist .sass-cache app/bower_components options.mine.js + +.migrate diff --git a/migrations/001-initial.js b/migrations/001-initial.js new file mode 100644 index 00000000..b8bb961f --- /dev/null +++ b/migrations/001-initial.js @@ -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(); +}; diff --git a/migrations/002-bills.js b/migrations/002-bills.js new file mode 100644 index 00000000..58066b18 --- /dev/null +++ b/migrations/002-bills.js @@ -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(); +}; diff --git a/migrations/003-device-events.js b/migrations/003-device-events.js new file mode 100644 index 00000000..c9b65894 --- /dev/null +++ b/migrations/003-device-events.js @@ -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(); +}; diff --git a/migrations/004-transactions-reload.js b/migrations/004-transactions-reload.js new file mode 100644 index 00000000..119e47f1 --- /dev/null +++ b/migrations/004-transactions-reload.js @@ -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(); +}; diff --git a/migrations/db.js b/migrations/db.js new file mode 100644 index 00000000..16b76e8b --- /dev/null +++ b/migrations/db.js @@ -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(); + }); + }); +};