diff --git a/bin/lamassu-migrate b/bin/lamassu-migrate index ee212caa..7decbe77 100755 --- a/bin/lamassu-migrate +++ b/bin/lamassu-migrate @@ -3,11 +3,11 @@ const migrate = require('../lib/migrate') migrate.run() -.then(() => { - console.log('DB Migration succeeded.') - process.exit(0) -}) -.catch(err => { - console.error('DB Migration failed: %s', err) - process.exit(1) -}) + .then(() => { + console.log('DB Migration succeeded.') + process.exit(0) + }) + .catch(err => { + console.error('DB Migration failed: %s', err) + process.exit(1) + }) diff --git a/migrations/001-initial.js b/migrations/001-initial.js index c4ac59ce..3f5547f6 100644 --- a/migrations/001-initial.js +++ b/migrations/001-initial.js @@ -3,30 +3,30 @@ const 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 ' + + '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' + + '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() ' + + 'id serial PRIMARY KEY, ' + + 'token text, ' + + 'created timestamp NOT NULL DEFAULT now() ' + ')', 'CREATE TABLE IF NOT EXISTS users ( ' + - 'id serial PRIMARY KEY, ' + - 'userName text NOT NULL UNIQUE, ' + - 'salt text NOT NULL, ' + - 'pwdHash text NOT NULL ' + + 'id serial PRIMARY KEY, ' + + 'userName text NOT NULL UNIQUE, ' + + 'salt text NOT NULL, ' + + 'pwdHash text NOT NULL ' + ')' ] diff --git a/migrations/002-bills.js b/migrations/002-bills.js index a75f9dcb..4b5dffa8 100644 --- a/migrations/002-bills.js +++ b/migrations/002-bills.js @@ -2,16 +2,17 @@ var db = require('./db') exports.up = function (next) { const sql = - ['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() )'] + ['CREATE TABLE IF NOT EXISTS 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() ' + + ')'] db.multi(sql, next) } diff --git a/migrations/003-device-events.js b/migrations/003-device-events.js index 21c666ef..70263db9 100644 --- a/migrations/003-device-events.js +++ b/migrations/003-device-events.js @@ -7,7 +7,8 @@ exports.up = function (next) { '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) { diff --git a/migrations/004-transactions-reload.js b/migrations/004-transactions-reload.js index fb5aba8b..6a9544aa 100644 --- a/migrations/004-transactions-reload.js +++ b/migrations/004-transactions-reload.js @@ -11,54 +11,54 @@ exports.up = function (next) { 'published', 'authorized', 'confirmed'].map(singleQuotify).join(',') var sqls = [ - 'CREATE TYPE transaction_stage AS ENUM (' + stages + ')', - 'CREATE TYPE transaction_authority AS ENUM (' + authorizations + ')', + db.defineEnum('transaction_stage', stages), + db.defineEnum('transaction_authority', authorizations), - 'CREATE TABLE transactions ( ' + - 'id serial PRIMARY KEY, ' + - 'session_id uuid NOT NULL, ' + - 'device_fingerprint text, ' + - 'to_address text NOT NULL, ' + - 'satoshis integer NOT NULL DEFAULT 0, ' + - 'fiat integer NOT NULL DEFAULT 0, ' + - 'currency_code text NOT NULL, ' + - 'fee integer NOT NULL DEFAULT 0, ' + - 'incoming boolean NOT NULL, ' + - 'stage transaction_stage NOT NULL, ' + - 'authority transaction_authority NOT NULL, ' + - 'tx_hash text, ' + - 'error text, ' + - 'created timestamp NOT NULL DEFAULT now(), ' + - 'UNIQUE (session_id, to_address, stage, authority) ' + + 'CREATE TABLE IF NOT EXISTS transactions ( ' + + 'id serial PRIMARY KEY, ' + + 'session_id uuid NOT NULL, ' + + 'device_fingerprint text, ' + + 'to_address text NOT NULL, ' + + 'satoshis integer NOT NULL DEFAULT 0, ' + + 'fiat integer NOT NULL DEFAULT 0, ' + + 'currency_code text NOT NULL, ' + + 'fee integer NOT NULL DEFAULT 0, ' + + 'incoming boolean NOT NULL, ' + + 'stage transaction_stage NOT NULL, ' + + 'authority transaction_authority NOT NULL, ' + + 'tx_hash text, ' + + 'error text, ' + + 'created timestamp NOT NULL DEFAULT now(), ' + + 'UNIQUE (session_id, to_address, stage, authority) ' + ')', 'CREATE INDEX ON transactions (session_id)', - 'CREATE TABLE pending_transactions ( ' + - 'id serial PRIMARY KEY, ' + - 'device_fingerprint text NOT NULL, ' + - 'session_id uuid UNIQUE NOT NULL, ' + - 'incoming boolean NOT NULL, ' + - 'currency_code text NOT NULL, ' + - 'to_address text NOT NULL, ' + - 'satoshis integer NOT NULL, ' + - 'updated timestamp NOT NULL DEFAULT now() ' + + 'CREATE TABLE IF NOT EXISTS pending_transactions ( ' + + 'id serial PRIMARY KEY, ' + + 'device_fingerprint text NOT NULL, ' + + 'session_id uuid UNIQUE NOT NULL, ' + + 'incoming boolean NOT NULL, ' + + 'currency_code text NOT NULL, ' + + 'to_address text NOT NULL, ' + + 'satoshis integer NOT NULL, ' + + 'updated timestamp NOT NULL DEFAULT now() ' + ')', - 'CREATE TABLE dispenses ( ' + - 'id serial PRIMARY KEY, ' + - 'device_fingerprint text NOT NULL, ' + - '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, ' + - 'created timestamp NOT NULL DEFAULT now() ' + + 'CREATE TABLE IF NOT EXISTS dispenses ( ' + + 'id serial PRIMARY KEY, ' + + 'device_fingerprint text NOT NULL, ' + + '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, ' + + 'created timestamp NOT NULL DEFAULT now() ' + ')', - 'CREATE INDEX ON dispenses (device_fingerprint)' + db.ifColumn('dispenses', 'device_fingerprint', 'CREATE INDEX ON dispenses (device_fingerprint)') ] db.multi(sqls, next) diff --git a/migrations/005-addCrypto.js b/migrations/005-addCrypto.js index eaa551c7..1f003031 100644 --- a/migrations/005-addCrypto.js +++ b/migrations/005-addCrypto.js @@ -2,12 +2,12 @@ var db = require('./db') exports.up = function (next) { var sqls = [ - 'alter table transactions alter satoshis TYPE bigint', - "alter table transactions add crypto_code text default 'BTC'", - "alter table pending_transactions add crypto_code text default 'BTC'", - 'alter table pending_transactions alter satoshis TYPE bigint', - "alter table bills add crypto_code text default 'BTC'", - 'alter table bills alter satoshis TYPE bigint' + db.alterColumn('transactions', 'satoshis', 'TYPE bigint'), + db.addColumn('transactions', 'crypto_code', 'text default \'BTC\''), + db.addColumn('pending_transactions', 'crypto_code', 'text default \'BTC\''), + db.alterColumn('pending_transactions', 'satoshis', 'TYPE bigint'), + db.addColumn('bills', 'crypto_code', 'text default \'BTC\''), + db.alterColumn('bills', 'satoshis', 'TYPE bigint') ] db.multi(sqls, next) diff --git a/migrations/007-add-phone.js b/migrations/007-add-phone.js index 8cb3c13f..6e42998f 100644 --- a/migrations/007-add-phone.js +++ b/migrations/007-add-phone.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table transactions add phone text', + db.addColumn('transactions', 'phone', 'text'), 'create index on transactions (phone)' ] db.multi(sql, next) diff --git a/migrations/008-add-two-way.js b/migrations/008-add-two-way.js index db27faa4..a106d43e 100644 --- a/migrations/008-add-two-way.js +++ b/migrations/008-add-two-way.js @@ -8,12 +8,12 @@ exports.up = function (next) { .map(singleQuotify).join(',') var sql = [ - 'create type status_stage AS enum (' + statuses + ')', - 'alter table transactions add dispensed boolean NOT NULL DEFAULT false', - 'alter table transactions add notified boolean NOT NULL DEFAULT false', - 'alter table transactions add redeem boolean NOT NULL DEFAULT false', - 'alter table transactions add confirmation_time timestamptz', - 'alter table transactions add status status_stage NOT NULL DEFAULT \'notSeen\'' + db.defineEnum('status_stage', statuses), + db.addColumn('transactions', 'dispensed', 'boolean NOT NULL DEFAULT false'), + db.addColumn('transactions', 'notified', 'boolean NOT NULL DEFAULT false'), + db.addColumn('transactions', 'redeem', 'boolean NOT NULL DEFAULT false'), + db.addColumn('transactions', 'confirmation_time', 'timestamptz'), + db.addColumn('transactions', 'status', 'status_stage NOT NULL DEFAULT \'notSeen\'') ] db.multi(sql, next) } diff --git a/migrations/009-update-timestamps.js b/migrations/009-update-timestamps.js index 34bc56f1..99b4a3d9 100644 --- a/migrations/009-update-timestamps.js +++ b/migrations/009-update-timestamps.js @@ -2,12 +2,12 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table transactions alter created type timestamptz', - 'alter table bills alter created type timestamptz', - 'alter table dispenses alter created type timestamptz', - 'alter table machine_events alter created type timestamptz', - 'alter table pairing_tokens alter created type timestamptz', - 'alter table pending_transactions alter updated type timestamptz' + db.alterColumn('transactions', 'created', 'type timestamptz'), + db.alterColumn('bills', 'created', 'type timestamptz'), + db.alterColumn('dispenses', 'created', 'type timestamptz'), + db.alterColumn('machine_events', 'created', 'type timestamptz'), + db.alterColumn('pairing_tokens', 'created', 'type timestamptz'), + db.alterColumn('pending_transactions', 'updated', 'type timestamptz') ] db.multi(sql, next) } diff --git a/migrations/010-cached-requests.js b/migrations/010-cached-requests.js index 01ca6779..40c5c293 100644 --- a/migrations/010-cached-requests.js +++ b/migrations/010-cached-requests.js @@ -4,14 +4,14 @@ var db = require('./db') exports.up = function (next) { db.multi(['CREATE TABLE IF NOT EXISTS cached_responses ( ' + - 'id serial PRIMARY KEY, ' + - 'device_fingerprint text NOT NULL, ' + - 'session_id uuid NOT NULL, ' + - 'path text NOT NULL, ' + - 'method text NOT NULL, ' + - 'body json NOT NULL, ' + - 'created timestamptz NOT NULL DEFAULT now(), ' + - 'UNIQUE (device_fingerprint, session_id, path, method) ' + + 'id serial PRIMARY KEY, ' + + 'device_fingerprint text NOT NULL, ' + + 'session_id uuid NOT NULL, ' + + 'path text NOT NULL, ' + + 'method text NOT NULL, ' + + 'body json NOT NULL, ' + + 'created timestamptz NOT NULL DEFAULT now(), ' + + 'UNIQUE (device_fingerprint, session_id, path, method) ' + ')'], next) } diff --git a/migrations/011-transactions-reload-2.js b/migrations/011-transactions-reload-2.js index e4dd0811..3334861d 100644 --- a/migrations/011-transactions-reload-2.js +++ b/migrations/011-transactions-reload-2.js @@ -9,7 +9,7 @@ exports.up = function (next) { .map(singleQuotify).join(',') var sql = [ - `create table cash_in_txs ( + `CREATE TABLE IF NOT EXISTS cash_in_txs ( session_id uuid PRIMARY KEY, device_fingerprint text NOT NULL, to_address text NOT NULL, @@ -23,7 +23,7 @@ exports.up = function (next) { error text, created timestamptz NOT NULL default now() )`, - `create table cash_out_txs ( + `CREATE TABLE IF NOT EXISTS cash_out_txs ( session_id uuid PRIMARY KEY, device_fingerprint text NOT NULL, to_address text NOT NULL, @@ -32,7 +32,7 @@ exports.up = function (next) { fiat numeric(14, 5) NOT NULL, currency_code text NOT NULL, tx_hash text, - status status_stage NOT NULL default \'notSeen\', + status status_stage NOT NULL default 'notSeen', dispensed boolean NOT NULL default false, notified boolean NOT NULL default false, redeem boolean NOT NULL default false, @@ -41,15 +41,16 @@ exports.up = function (next) { created timestamptz NOT NULL default now(), confirmation_time timestamptz )`, - `create type cash_out_action_types AS ENUM (${actions})`, - `create table cash_out_actions ( + db.defineEnum('cash_out_action_types', actions), + `CREATE TABLE IF NOT EXISTS cash_out_actions ( id serial PRIMARY KEY, - session_id uuid REFERENCES cash_out_txs(session_id), + session_id uuid, action cash_out_action_types NOT NULL, created timestamptz NOT NULL default now() )`, - `alter table dispenses add session_id uuid`, - `alter table dispenses drop constraint dispenses_transaction_id_fkey` + db.addConstraint('cash_out_actions', 'cash_out_actions_session_id_fkey', 'FOREIGN KEY (session_id) REFERENCES cash_out_txs(session_id)', 'cash_out_txs', 'session_id'), + db.addColumn('dispenses', 'session_id', 'uuid'), + db.dropConstraint('dispenses', 'dispenses_transaction_id_fkey') ] db.multi(sql, next) } diff --git a/migrations/012-add-hd-path-serial.js b/migrations/012-add-hd-path-serial.js index ad8ac2d4..571c85d9 100644 --- a/migrations/012-add-hd-path-serial.js +++ b/migrations/012-add-hd-path-serial.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - `create table cash_out_hds ( + `CREATE TABLE IF NOT EXISTS cash_out_hds ( session_id uuid PRIMARY KEY, crypto_code text NOT NULL, hd_serial integer NOT NULL, diff --git a/migrations/013-add-last-checked.js b/migrations/013-add-last-checked.js index 8b943c30..562b264e 100644 --- a/migrations/013-add-last-checked.js +++ b/migrations/013-add-last-checked.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_out_hds add last_checked timestamptz not null default now()', - 'alter table cash_out_hds add confirmed boolean not null default false', + db.addColumn('cash_out_hds', 'last_checked', 'timestamptz not null default now()'), + db.addColumn('cash_out_hds', 'confirmed', 'boolean not null default false'), 'create index on cash_out_hds (confirmed, last_checked)' ] db.multi(sql, next) diff --git a/migrations/014-session-to-tx-id.js b/migrations/014-session-to-tx-id.js index 0e457dd6..76d1ec30 100644 --- a/migrations/014-session-to-tx-id.js +++ b/migrations/014-session-to-tx-id.js @@ -2,31 +2,31 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table bills rename device_fingerprint to device_id', - 'alter table bills rename satoshis to crypto_atoms', - 'alter table bills rename session_id to cash_in_txs_id', + db.renameColumn('bills', 'device_fingerprint', 'device_id'), + db.renameColumn('bills', 'satoshis', 'crypto_atoms'), + db.renameColumn('bills', 'session_id', 'cash_in_txs_id'), - 'alter table cached_responses rename device_fingerprint to device_id', - 'alter table cached_responses rename session_id to tx_id', + db.renameColumn('cached_responses', 'device_fingerprint', 'device_id'), + db.renameColumn('cached_responses', 'session_id', 'tx_id'), - 'alter table cash_in_txs rename session_id to id', - 'alter table cash_in_txs rename device_fingerprint to device_id', + db.renameColumn('cash_in_txs', 'session_id', 'id'), + db.renameColumn('cash_in_txs', 'device_fingerprint', 'device_id'), - 'alter table cash_out_actions rename session_id to cash_out_txs_id', + db.renameColumn('cash_out_actions', 'session_id', 'cash_out_txs_id'), - 'alter table cash_out_hds rename session_id to id', + db.renameColumn('cash_out_hds', 'session_id', 'id'), - 'alter table cash_out_txs rename session_id to id', - 'alter table cash_out_txs rename device_fingerprint to device_id', + db.renameColumn('cash_out_txs', 'session_id', 'id'), + db.renameColumn('cash_out_txs', 'device_fingerprint', 'device_id'), - 'alter table devices rename fingerprint to device_id', + db.renameColumn('devices', 'fingerprint', 'device_id'), - 'alter table dispenses rename session_id to cash_out_txs_id', - 'alter table dispenses rename device_fingerprint to device_id', + db.renameColumn('dispenses', 'session_id', 'cash_out_txs_id'), + db.renameColumn('dispenses', 'device_fingerprint', 'device_id'), - 'alter table machine_configs rename device_fingerprint to device_id', + db.renameColumn('machine_configs', 'device_fingerprint', 'device_id'), - 'alter table machine_events rename device_fingerprint to device_id' + db.renameColumn('machine_events', 'device_fingerprint', 'device_id') ] db.multi(sql, next) } diff --git a/migrations/015-paired_devices.js b/migrations/015-paired_devices.js index 5c310b29..2186f195 100644 --- a/migrations/015-paired_devices.js +++ b/migrations/015-paired_devices.js @@ -2,9 +2,9 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table devices drop authorized', - 'alter table devices drop unpair', - `create table paired_devices ( + db.dropColumn('devices', 'authorized'), + db.dropColumn('devices', 'unpair'), + `CREATE TABLE IF NOT EXISTS paired_devices ( device_id text PRIMARY KEY, created timestamptz NOT NULL default now() )` diff --git a/migrations/016-new_cached_requests_table.js b/migrations/016-new_cached_requests_table.js index f94f4d6a..e42e8553 100644 --- a/migrations/016-new_cached_requests_table.js +++ b/migrations/016-new_cached_requests_table.js @@ -3,7 +3,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ 'drop table if exists cached_responses', - `create table idempotents ( + `create table if not exists idempotents ( request_id text PRIMARY KEY, device_id text NOT NULL, body json NOT NULL, diff --git a/migrations/017-user_tokens.js b/migrations/017-user_tokens.js index e7c001ba..af598599 100644 --- a/migrations/017-user_tokens.js +++ b/migrations/017-user_tokens.js @@ -3,12 +3,12 @@ var db = require('./db') exports.up = function (next) { var sql = [ 'drop table if exists users', - `create table user_tokens ( + `create table if not exists user_tokens ( token text PRIMARY KEY, name text NOT NULL, created timestamptz NOT NULL default now() )`, - `create table one_time_passes ( + `create table if not exists one_time_passes ( token text PRIMARY KEY, name text NOT NULL, created timestamptz NOT NULL default now() diff --git a/migrations/018-alter_devices.js b/migrations/018-alter_devices.js index 8060fc9f..7102092c 100644 --- a/migrations/018-alter_devices.js +++ b/migrations/018-alter_devices.js @@ -4,7 +4,7 @@ exports.up = function (next) { var sql = [ 'drop table if exists devices', 'drop table if exists paired_devices', - `create table devices ( + `create table if not exists devices ( device_id text PRIMARY KEY, name text NOT NULL, cashbox integer NOT NULL default 0, @@ -14,7 +14,7 @@ exports.up = function (next) { display boolean NOT NULL default TRUE, created timestamptz NOT NULL default now() )`, - 'alter table pairing_tokens add column name text NOT NULL' + db.addColumn('pairing_tokens', 'name', 'text NOT NULL') ] db.multi(sql, next) } diff --git a/migrations/019-remove-dispense-counts.js b/migrations/019-remove-dispense-counts.js index fec34664..57632524 100644 --- a/migrations/019-remove-dispense-counts.js +++ b/migrations/019-remove-dispense-counts.js @@ -2,9 +2,9 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table dispenses drop column count1', - 'alter table dispenses drop column count2', - 'alter table dispenses drop column refill' + db.dropColumn('dispenses', 'count1'), + db.dropColumn('dispenses', 'count2'), + db.dropColumn('dispenses', 'refill') ] db.multi(sql, next) } diff --git a/migrations/020-add-server-events.js b/migrations/020-add-server-events.js index 93d0ce0e..73fb65d1 100644 --- a/migrations/020-add-server-events.js +++ b/migrations/020-add-server-events.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - `create table server_events ( + `create table if not exists server_events ( id serial PRIMARY KEY, event_type text NOT NULL, created timestamptz NOT NULL default now() diff --git a/migrations/021-config-version-id.js b/migrations/021-config-version-id.js index 44c01949..ef2b13de 100644 --- a/migrations/021-config-version-id.js +++ b/migrations/021-config-version-id.js @@ -2,11 +2,9 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table devices add column user_config_id int', - 'alter table user_config add column created timestamptz NOT NULL default now()', - `ALTER TABLE devices ADD CONSTRAINT user_config_id - FOREIGN KEY (user_config_id) - REFERENCES user_config (id)` + db.addColumn('devices', 'user_config_id', 'int'), + db.addColumn('user_config', 'created', 'timestamptz NOT NULL default now()'), + db.addConstraint('devices', 'user_config_id', 'FOREIGN KEY (user_config_id) REFERENCES user_config (id)') ] db.multi(sql, next) } diff --git a/migrations/022-add_cash_in_sent.js b/migrations/022-add_cash_in_sent.js index 74423dab..1dd031d2 100644 --- a/migrations/022-add_cash_in_sent.js +++ b/migrations/022-add_cash_in_sent.js @@ -2,13 +2,13 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_in_txs add column send boolean not null default false', - 'alter table cash_in_txs rename currency_code to fiat_code', - 'alter table bills rename currency_code to fiat_code', - 'alter table bills rename denomination to fiat', - 'alter table bills drop column to_address', - 'alter table bills drop column device_id', - 'alter table cash_out_txs rename currency_code to fiat_code' + db.addColumn('cash_in_txs', 'send', 'boolean not null default false'), + db.renameColumn('cash_in_txs', 'currency_code', 'fiat_code'), + db.renameColumn('bills', 'currency_code', 'fiat_code'), + db.renameColumn('bills', 'denomination', 'fiat'), + db.dropColumn('bills', 'to_address'), + db.dropColumn('bills', 'device_id'), + db.renameColumn('cash_out_txs', 'currency_code', 'fiat_code') ] db.multi(sql, next) } diff --git a/migrations/023-add-dispenses-to-cash-out.js b/migrations/023-add-dispenses-to-cash-out.js index 3229514d..54184f98 100644 --- a/migrations/023-add-dispenses-to-cash-out.js +++ b/migrations/023-add-dispenses-to-cash-out.js @@ -2,15 +2,15 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_out_txs add column dispensed_1 integer', - 'alter table cash_out_txs add column dispensed_2 integer', - 'alter table cash_out_txs add column rejected_1 integer', - 'alter table cash_out_txs add column rejected_2 integer', - 'alter table cash_out_txs add column denomination_1 integer', - 'alter table cash_out_txs add column denomination_2 integer', - 'alter table cash_out_txs add column dispense_error text', - 'alter table cash_out_txs add column dispense_time timestamptz', - 'drop table dispenses' + db.addColumn('cash_out_txs', 'dispensed_1', 'integer'), + db.addColumn('cash_out_txs', 'dispensed_2', 'integer'), + db.addColumn('cash_out_txs', 'rejected_1', 'integer'), + db.addColumn('cash_out_txs', 'rejected_2', 'integer'), + db.addColumn('cash_out_txs', 'denomination_1', 'integer'), + db.addColumn('cash_out_txs', 'denomination_2', 'integer'), + db.addColumn('cash_out_txs', 'dispense_error', 'text'), + db.addColumn('cash_out_txs', 'dispense_time', 'timestamptz'), + 'drop table if exists dispenses' ] db.multi(sql, next) } diff --git a/migrations/024-consolidate-hd.js b/migrations/024-consolidate-hd.js index aeb8333f..026636e6 100644 --- a/migrations/024-consolidate-hd.js +++ b/migrations/024-consolidate-hd.js @@ -2,18 +2,18 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'create sequence hd_indices_seq minvalue 0 maxvalue 2147483647', - 'alter table cash_out_txs add column hd_index integer', - 'alter sequence hd_indices_seq owned by cash_out_txs.hd_index', - "alter table cash_out_txs add column swept boolean not null default 'f'", - 'alter table cash_out_txs drop column tx_hash', + db.addSequence('hd_indices_seq', 'minvalue 0 maxvalue 2147483647'), + db.addColumn('cash_out_txs', 'hd_index', 'integer'), + db.alterSequence('hd_indices_seq', 'owned by cash_out_txs.hd_index'), + db.addColumn('cash_out_txs', 'swept', 'boolean not null default \'f\''), + db.dropColumn('cash_out_txs', 'tx_hash'), 'create unique index on cash_out_txs (hd_index)', - 'drop table cash_out_hds', - 'drop table cash_out_actions', - 'drop table transactions', - 'drop table idempotents', - 'drop table machine_configs', - 'drop table pending_transactions' + 'drop table if exists cash_out_hds', + 'drop table if exists cash_out_actions', + 'drop table if exists transactions', + 'drop table if exists idempotents', + 'drop table if exists machine_configs', + 'drop table if exists pending_transactions' ] db.multi(sql, next) } diff --git a/migrations/025-create_trades.js b/migrations/025-create_trades.js index 9aeaa5e1..5e0c16ff 100644 --- a/migrations/025-create_trades.js +++ b/migrations/025-create_trades.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { var sql = [ - "create type trade_type as enum ('buy', 'sell')", - `create table trades ( + db.defineEnum('trade_type', "'buy', 'sell'"), + `create table if not exists trades ( id serial PRIMARY KEY, type trade_type not null, crypto_code text not null, diff --git a/migrations/026-add_send_confirmed.js b/migrations/026-add_send_confirmed.js index 5b64c567..bce1dcee 100644 --- a/migrations/026-add_send_confirmed.js +++ b/migrations/026-add_send_confirmed.js @@ -2,15 +2,15 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_in_txs add column send_confirmed boolean not null default false', - 'alter table cash_in_txs add column device_time bigint not null', - 'alter table cash_in_txs add column timedout boolean not null default false', - 'alter table cash_in_txs add column send_time timestamptz', - 'alter table cash_in_txs add column error_code text', - 'alter table cash_in_txs add column operator_completed boolean not null default false', - 'alter table cash_in_txs add column send_pending boolean not null default false', - 'alter table cash_out_txs add column device_time bigint not null', - 'alter table cash_out_txs add column timedout boolean not null default false' + db.addColumn('cash_in_txs', 'send_confirmed', 'boolean not null default false'), + db.addColumn('cash_in_txs', 'device_time', 'bigint not null'), + db.addColumn('cash_in_txs', 'timedout', 'boolean not null default false'), + db.addColumn('cash_in_txs', 'send_time', 'timestamptz'), + db.addColumn('cash_in_txs', 'error_code', 'text'), + db.addColumn('cash_in_txs', 'operator_completed', 'boolean not null default false'), + db.addColumn('cash_in_txs', 'send_pending', 'boolean not null default false'), + db.addColumn('cash_out_txs', 'device_time', 'bigint not null'), + db.addColumn('cash_out_txs', 'timedout', 'boolean not null default false') ] db.multi(sql, next) } diff --git a/migrations/027-tx_errors.js b/migrations/027-tx_errors.js index 374bcdb4..defae46e 100644 --- a/migrations/027-tx_errors.js +++ b/migrations/027-tx_errors.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - `create table cash_in_actions ( + `create table if not exists cash_in_actions ( id serial primary key, tx_id uuid not null, action text not null, diff --git a/migrations/028-cash_out_actions.js b/migrations/028-cash_out_actions.js index 61312f88..467da617 100644 --- a/migrations/028-cash_out_actions.js +++ b/migrations/028-cash_out_actions.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - `create table cash_out_actions ( + `create table if not exists cash_out_actions ( id serial primary key, tx_id uuid not null, action text not null, @@ -22,16 +22,16 @@ exports.up = function (next) { device_time bigint, created timestamptz not null default now() )`, - 'alter table cash_out_txs drop column dispensed_1', - 'alter table cash_out_txs drop column dispensed_2', - 'alter table cash_out_txs drop column rejected_1', - 'alter table cash_out_txs drop column rejected_2', - 'alter table cash_out_txs drop column denomination_1', - 'alter table cash_out_txs drop column denomination_2', - 'alter table cash_out_txs drop column dispense_error', - 'alter table cash_out_txs drop column dispense_time', - 'alter table cash_out_txs add column dispense_confirmed boolean default false', - 'alter table cash_out_txs rename column dispensed to dispense' + db.dropColumn('cash_out_txs', 'dispensed_1'), + db.dropColumn('cash_out_txs', 'dispensed_2'), + db.dropColumn('cash_out_txs', 'rejected_1'), + db.dropColumn('cash_out_txs', 'rejected_2'), + db.dropColumn('cash_out_txs', 'denomination_1'), + db.dropColumn('cash_out_txs', 'denomination_2'), + db.dropColumn('cash_out_txs', 'dispense_error'), + db.dropColumn('cash_out_txs', 'dispense_time'), + db.addColumn('cash_out_txs', 'dispense_confirmed', 'boolean default false'), + db.renameColumn('cash_out_txs', 'dispensed', 'dispense') ] db.multi(sql, next) } diff --git a/migrations/029-add_valid_to_user_config.js b/migrations/029-add_valid_to_user_config.js index 1d0c5abb..85a55b3e 100644 --- a/migrations/029-add_valid_to_user_config.js +++ b/migrations/029-add_valid_to_user_config.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table user_config add column valid boolean not null' + db.addColumn('user_config', 'valid', 'boolean not null') ] db.multi(sql, next) } diff --git a/migrations/030-cash-out-provision.js b/migrations/030-cash-out-provision.js index 80b613b7..bbe154bb 100644 --- a/migrations/030-cash-out-provision.js +++ b/migrations/030-cash-out-provision.js @@ -2,10 +2,10 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_out_txs add column provisioned_1 integer', - 'alter table cash_out_txs add column provisioned_2 integer', - 'alter table cash_out_txs add column denomination_1 integer', - 'alter table cash_out_txs add column denomination_2 integer' + db.addColumn('cash_out_txs', 'provisioned_1', 'integer'), + db.addColumn('cash_out_txs', 'provisioned_2', 'integer'), + db.addColumn('cash_out_txs', 'denomination_1', 'integer'), + db.addColumn('cash_out_txs', 'denomination_2', 'integer') ] db.multi(sql, next) } diff --git a/migrations/031-remove_name_from_devices.js b/migrations/031-remove_name_from_devices.js index 89899d67..6444f881 100644 --- a/migrations/031-remove_name_from_devices.js +++ b/migrations/031-remove_name_from_devices.js @@ -2,7 +2,7 @@ const db = require('./db') exports.up = function (next) { const sql = [ - 'alter table devices drop column name' + db.dropColumn('devices', 'name') ] db.multi(sql, next) } diff --git a/migrations/032-create_machine_pings_table.js b/migrations/032-create_machine_pings_table.js index 25588e7a..245d83b8 100644 --- a/migrations/032-create_machine_pings_table.js +++ b/migrations/032-create_machine_pings_table.js @@ -2,24 +2,24 @@ var db = require('./db') exports.up = function (next) { var sql = [ - `create table machine_pings ( - id uuid PRIMARY KEY, - device_id text not null, - serial_number integer not null, - device_time timestamptz not null, - created timestamptz not null default now())`, - `create table aggregated_machine_pings ( - id uuid PRIMARY KEY, - device_id text not null, - dropped_pings integer not null, - total_pings integer not null, - lag_sd_ms integer not null, - lag_min_ms integer not null, - lag_max_ms integer not null, - lag_median_ms integer not null, - day date not null)`, - 'alter table machine_events drop column device_time', - 'alter table machine_events add column device_time timestamptz' + `create table if not exists machine_pings ( + id uuid PRIMARY KEY, + device_id text not null, + serial_number integer not null, + device_time timestamptz not null, + created timestamptz not null default now())`, + `create table if not exists aggregated_machine_pings ( + id uuid PRIMARY KEY, + device_id text not null, + dropped_pings integer not null, + total_pings integer not null, + lag_sd_ms integer not null, + lag_min_ms integer not null, + lag_max_ms integer not null, + lag_median_ms integer not null, + day date not null)`, + db.dropColumn('machine_events', 'device_time'), + db.addColumn('machine_events', 'device_time', 'timestamptz') ] db.multi(sql, next) } diff --git a/migrations/033-add_cash_in_fee.js b/migrations/033-add_cash_in_fee.js index 5aca727f..0fba64d1 100644 --- a/migrations/033-add_cash_in_fee.js +++ b/migrations/033-add_cash_in_fee.js @@ -2,12 +2,12 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_in_txs add column cash_in_fee numeric(14, 5) not null', - 'alter table cash_in_txs add column cash_in_fee_crypto bigint not null', - 'alter table cash_in_txs add column minimum_tx integer not null', - 'alter table bills add column cash_in_fee numeric(14, 5) not null', - 'alter table bills add column cash_in_fee_crypto bigint not null', - 'alter table bills add column crypto_atoms_after_fee bigint not null' + db.addColumn('cash_in_txs', 'cash_in_fee', 'numeric(14, 5) not null'), + db.addColumn('cash_in_txs', 'cash_in_fee_crypto', 'bigint not null'), + db.addColumn('cash_in_txs', 'minimum_tx', 'integer not null'), + db.addColumn('bills', 'cash_in_fee', 'numeric(14, 5) not null'), + db.addColumn('bills', 'cash_in_fee_crypto', 'bigint not null'), + db.addColumn('bills', 'crypto_atoms_after_fee', 'bigint not null') ] db.multi(sql, next) } diff --git a/migrations/034-add_cash_out_error_code.js b/migrations/034-add_cash_out_error_code.js index b8a446b0..fd1c2ec7 100644 --- a/migrations/034-add_cash_out_error_code.js +++ b/migrations/034-add_cash_out_error_code.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_out_txs add column error_code text' + db.addColumn('cash_out_txs', 'error_code', 'text') ] db.multi(sql, next) } diff --git a/migrations/035-log_bank_notes.js b/migrations/035-log_bank_notes.js index 3f4f2b3c..18a710c3 100644 --- a/migrations/035-log_bank_notes.js +++ b/migrations/035-log_bank_notes.js @@ -2,21 +2,21 @@ var db = require('./db') exports.up = function (next) { var sql = [ - `create table cash_out_refills ( - id uuid PRIMARY KEY, - device_id text not null, - user_id integer not null, - cassette1 integer not null, - cassette2 integer not null, - denomination1 integer not null, - denomination2 integer not null, - created timestamptz not null default now())`, - `create table cash_in_refills ( - id uuid PRIMARY KEY, - device_id text not null, - user_id integer not null, - cash_box_count integer not null, - created timestamptz not null default now())` + `create table if not exists cash_out_refills ( + id uuid PRIMARY KEY, + device_id text not null, + user_id integer not null, + cassette1 integer not null, + cassette2 integer not null, + denomination1 integer not null, + denomination2 integer not null, + created timestamptz not null default now())`, + `create table if not exists cash_in_refills ( + id uuid PRIMARY KEY, + device_id text not null, + user_id integer not null, + cash_box_count integer not null, + created timestamptz not null default now())` ] db.multi(sql, next) } diff --git a/migrations/036-add_customers_table.js b/migrations/036-add_customers_table.js index 253e81e6..cf481efd 100644 --- a/migrations/036-add_customers_table.js +++ b/migrations/036-add_customers_table.js @@ -3,26 +3,26 @@ var anonymous = require('../lib/constants').anonymousCustomer exports.up = function (next) { const sql = - [`create table customers ( - id uuid PRIMARY KEY, - phone text unique, - phone_at timestamptz, - id_card_number text, - id_card_expiration date, - id_card_data json, - id_card_at timestamptz, - name text, - address text, - manually_verified boolean, - sanctions_check boolean, - front_facing_cam_path text, - front_facing_cam_at timestamptz, - id_card_image_path text, - id_card_image_at timestamptz, - created timestamptz NOT NULL DEFAULT now() )`, - `insert into customers (id, name) VALUES ( '${anonymous.uuid}','${anonymous.name}' )`, - `alter table cash_in_txs add column customer_id uuid references customers (id) DEFAULT '${anonymous.uuid}'`, - `alter table cash_out_txs add column customer_id uuid references customers (id) DEFAULT '${anonymous.uuid}'` + [`create table if not exists customers ( + id uuid PRIMARY KEY, + phone text unique, + phone_at timestamptz, + id_card_number text, + id_card_expiration date, + id_card_data json, + id_card_at timestamptz, + name text, + address text, + manually_verified boolean, + sanctions_check boolean, + front_facing_cam_path text, + front_facing_cam_at timestamptz, + id_card_image_path text, + id_card_image_at timestamptz, + created timestamptz NOT NULL DEFAULT now() )`, + `insert into customers (id, name) VALUES ('${anonymous.uuid}','${anonymous.name}') ON CONFLICT DO NOTHING`, + db.addColumn('cash_in_txs', 'customer_id', `uuid references customers (id) DEFAULT '${anonymous.uuid}'`), + db.addColumn('cash_out_txs', 'customer_id', `uuid references customers (id) DEFAULT '${anonymous.uuid}'`) ] db.multi(sql, next) diff --git a/migrations/037-add_compliance_authorizations_table.js b/migrations/037-add_compliance_authorizations_table.js index 3ccfa05b..274e3a77 100644 --- a/migrations/037-add_compliance_authorizations_table.js +++ b/migrations/037-add_compliance_authorizations_table.js @@ -1,9 +1,9 @@ var db = require('./db') exports.up = function (next) { - const sql = - [ "create type compliance_types as enum ('manual', 'sanctions', 'sanctions_override')", - `create table compliance_authorizations ( + const sql = [ + db.defineEnum('compliance_types', "'manual', 'sanctions', 'sanctions_override'"), + `create table if not exists compliance_authorizations ( id uuid PRIMARY KEY, customer_id uuid REFERENCES customers (id), compliance_type compliance_types NOT NULL, diff --git a/migrations/1503907708756-drop-device-time.js b/migrations/1503907708756-drop-device-time.js index b1288b68..a092eeca 100644 --- a/migrations/1503907708756-drop-device-time.js +++ b/migrations/1503907708756-drop-device-time.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { const sql = [ - 'alter table cash_in_txs drop column device_time', - 'alter table cash_out_txs drop column device_time' + db.dropColumn('cash_in_txs', 'device_time'), + db.dropColumn('cash_out_txs', 'device_time') ] db.multi(sql, next) diff --git a/migrations/1503945570220-add-tx-version.js b/migrations/1503945570220-add-tx-version.js index 0cab75c5..1c117d49 100644 --- a/migrations/1503945570220-add-tx-version.js +++ b/migrations/1503945570220-add-tx-version.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { const sql = [ - 'alter table cash_in_txs add column tx_version integer not null', - 'alter table cash_out_txs add column tx_version integer not null' + db.addColumn('cash_in_txs', 'tx_version', 'integer not null'), + db.addColumn('cash_out_txs', 'tx_version', 'integer not null') ] db.multi(sql, next) diff --git a/migrations/1505044429557-add_cash_out_txs_published_at.js b/migrations/1505044429557-add_cash_out_txs_published_at.js index 451ed8db..32c4d9bf 100644 --- a/migrations/1505044429557-add_cash_out_txs_published_at.js +++ b/migrations/1505044429557-add_cash_out_txs_published_at.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { const sql = [ - 'alter table cash_out_txs add column published_at timestamptz', - 'alter table cash_out_txs rename column confirmation_time to confirmed_at' + db.addColumn('cash_out_txs', 'published_at', 'timestamptz'), + db.renameColumn('cash_out_txs', 'confirmation_time', 'confirmed_at') ] db.multi(sql, next) diff --git a/migrations/1505296896905-manual-override.js b/migrations/1505296896905-manual-override.js index 4b172d48..66b1f85f 100644 --- a/migrations/1505296896905-manual-override.js +++ b/migrations/1505296896905-manual-override.js @@ -12,39 +12,38 @@ exports.up = function (next) { * * @see {@link http://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/} */ - `create type compliance_type as enum - ('authorized', 'sms', 'id_card_data', 'id_card_photo', 'sanctions_check', 'front_facing_cam', 'hard_limit')`, - 'alter table compliance_authorizations alter column compliance_type set data type compliance_type using compliance_type::text::compliance_type', - 'drop type compliance_types', + db.defineEnum('compliance_type', "'authorized', 'sms', 'id_card_data', 'id_card_photo', 'sanctions_check', 'front_facing_cam', 'hard_limit'"), + db.alterColumn('compliance_authorizations', 'compliance_type', 'set data type compliance_type using compliance_type::text::compliance_type'), + db.dropEnum('compliance_types'), - "create type verification_type as enum ('verified', 'blocked', 'automatic')", + db.defineEnum('verification_type', "'verified', 'blocked', 'automatic'"), - 'alter table customers drop column manually_verified ', - "alter table customers add column sms_override verification_type not null default 'automatic'", - 'alter table customers add column sms_override_by text references user_tokens (token)', - 'alter table customers add column sms_override_at timestamptz', - "alter table customers add column id_card_data_override verification_type not null default 'automatic'", - 'alter table customers add column id_card_data_override_by text references user_tokens (token)', - 'alter table customers add column id_card_data_override_at timestamptz', - "alter table customers add column id_card_photo_override verification_type not null default 'automatic'", - 'alter table customers add column id_card_photo_override_by text references user_tokens (token)', - 'alter table customers add column id_card_photo_override_at timestamptz', - "alter table customers add column front_facing_cam_override verification_type not null default 'automatic'", - 'alter table customers add column front_facing_cam_override_by text references user_tokens (token)', - 'alter table customers add column front_facing_cam_override_at timestamptz', - "alter table customers add column sanctions_check_override verification_type not null default 'automatic'", - 'alter table customers add column sanctions_check_override_by text references user_tokens (token)', - 'alter table customers add column sanctions_check_override_at timestamptz', - "alter table customers add column authorized_override verification_type not null default 'automatic'", - 'alter table customers add column authorized_override_by text references user_tokens (token)', - 'alter table customers add column authorized_override_at timestamptz', - 'alter table customers add column authorized_at timestamptz', - 'alter table customers add column sanctions_check_at timestamptz', + db.dropColumn('customers', 'manually_verified'), + db.addColumn('customers', 'sms_override', 'verification_type not null default \'automatic\''), + db.addColumn('customers', 'sms_override_by', 'text references user_tokens (token)'), + db.addColumn('customers', 'sms_override_at', 'timestamptz'), + db.addColumn('customers', 'id_card_data_override', 'verification_type not null default \'automatic\''), + db.addColumn('customers', 'id_card_data_override_by', 'text references user_tokens (token)'), + db.addColumn('customers', 'id_card_data_override_at', 'timestamptz'), + db.addColumn('customers', 'id_card_photo_override', 'verification_type not null default \'automatic\''), + db.addColumn('customers', 'id_card_photo_override_by', 'text references user_tokens (token)'), + db.addColumn('customers', 'id_card_photo_override_at', 'timestamptz'), + db.addColumn('customers', 'front_facing_cam_override', 'verification_type not null default \'automatic\''), + db.addColumn('customers', 'front_facing_cam_override_by', 'text references user_tokens (token)'), + db.addColumn('customers', 'front_facing_cam_override_at', 'timestamptz'), + db.addColumn('customers', 'sanctions_check_override', 'verification_type not null default \'automatic\''), + db.addColumn('customers', 'sanctions_check_override_by', 'text references user_tokens (token)'), + db.addColumn('customers', 'sanctions_check_override_at', 'timestamptz'), + db.addColumn('customers', 'authorized_override', 'verification_type not null default \'automatic\''), + db.addColumn('customers', 'authorized_override_by', 'text references user_tokens (token)'), + db.addColumn('customers', 'authorized_override_at', 'timestamptz'), + db.addColumn('customers', 'authorized_at', 'timestamptz'), + db.addColumn('customers', 'sanctions_check_at', 'timestamptz'), - 'alter table compliance_authorizations rename to compliance_overrides', - 'alter table compliance_overrides add column verification verification_type not null', - 'alter table compliance_overrides rename column authorized_at to override_at', - 'alter table compliance_overrides rename column authorized_by to override_by' + db.renameColumn('compliance_authorizations', 'name', 'compliance_overrides'), + db.addColumn('compliance_overrides', 'verification', 'verification_type not null'), + db.renameColumn('compliance_overrides', 'authorized_at', 'override_at'), + db.renameColumn('compliance_overrides', 'authorized_by', 'override_by') ] db.multi(sql, next) diff --git a/migrations/1507639057362-compliance-override-naming.js b/migrations/1507639057362-compliance-override-naming.js index ae4c4110..0bee74d8 100644 --- a/migrations/1507639057362-compliance-override-naming.js +++ b/migrations/1507639057362-compliance-override-naming.js @@ -4,21 +4,21 @@ const db = require('./db') exports.up = function (next) { const sql = [ - 'alter table customers rename column id_card_number to id_card_data_number', - 'alter table customers rename column id_card_at to id_card_data_at', - 'alter table customers rename column sanctions_check to sanctions', - 'alter table customers rename column sanctions_check_at to sanctions_at', - 'alter table customers rename column front_facing_cam_at to front_camera_at', - 'alter table customers rename column front_facing_cam_path to front_camera_path', - 'alter table customers rename column id_card_image_path to id_card_photo_path', - 'alter table customers rename column id_card_image_at to id_card_photo_at', - 'alter table customers rename column id_card_expiration to id_card_data_expiration', - 'alter table customers rename column front_facing_cam_override to front_camera_override', - 'alter table customers rename column front_facing_cam_override_by to front_camera_override_by', - 'alter table customers rename column front_facing_cam_override_at to front_camera_override_at', - 'alter table customers rename column sanctions_check_override to sanctions_override', - 'alter table customers rename column sanctions_check_override_by to sanctions_override_by', - 'alter table customers rename column sanctions_check_override_at to sanctions_override_at', + db.renameColumn('customers', 'id_card_number', 'id_card_data_number'), + db.renameColumn('customers', 'id_card_at', 'id_card_data_at'), + db.renameColumn('customers', 'sanctions_check', 'sanctions'), + db.renameColumn('customers', 'sanctions_check_at', 'sanctions_at'), + db.renameColumn('customers', 'front_facing_cam_at', 'front_camera_at'), + db.renameColumn('customers', 'front_facing_cam_path', 'front_camera_path'), + db.renameColumn('customers', 'id_card_image_path', 'id_card_photo_path'), + db.renameColumn('customers', 'id_card_image_at', 'id_card_photo_at'), + db.renameColumn('customers', 'id_card_expiration', 'id_card_data_expiration'), + db.renameColumn('customers', 'front_facing_cam_override', 'front_camera_override'), + db.renameColumn('customers', 'front_facing_cam_override_by', 'front_camera_override_by'), + db.renameColumn('customers', 'front_facing_cam_override_at', 'front_camera_override_at'), + db.renameColumn('customers', 'sanctions_check_override', 'sanctions_override'), + db.renameColumn('customers', 'sanctions_check_override_by', 'sanctions_override_by'), + db.renameColumn('customers', 'sanctions_check_override_at', 'sanctions_override_at'), /** * Replace all compliance_type enum values * @@ -27,11 +27,10 @@ exports.up = function (next) { * * @see {@link http://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/} */ - 'alter type compliance_type rename to old_compliance_type', - `create type compliance_type as enum - ('authorized', 'sms', 'id_card_data', 'id_card_photo', 'sanctions', 'front_camera', 'hard_limit')`, - 'alter table compliance_overrides alter column compliance_type set data type compliance_type using compliance_type::text::compliance_type', - 'drop type old_compliance_type' + db.renameEnum('compliance_type', 'old_compliance_type'), + db.defineEnum('compliance_type', "'authorized', 'sms', 'id_card_data', 'id_card_photo', 'sanctions', 'front_camera', 'hard_limit'"), + db.alterColumn('compliance_overrides', 'compliance_type', 'set data type compliance_type using compliance_type::text::compliance_type'), + db.dropEnum('old_compliance_type') ] db.multi(sql, next) diff --git a/migrations/1508261875640-logs.js b/migrations/1508261875640-logs.js index 5f5b2138..819676ca 100644 --- a/migrations/1508261875640-logs.js +++ b/migrations/1508261875640-logs.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { const sql = - [`create table logs ( + [`create table if not exists logs ( id uuid PRIMARY KEY, device_id text, log_level text, diff --git a/migrations/1509091634946-support_logs.js b/migrations/1509091634946-support_logs.js index 1d87286c..409f1455 100644 --- a/migrations/1509091634946-support_logs.js +++ b/migrations/1509091634946-support_logs.js @@ -2,11 +2,11 @@ var db = require('./db') exports.up = function (next) { const sql = - [`create table support_logs ( + [`create table if not exists support_logs ( id uuid PRIMARY KEY, device_id text, timestamp timestamptz not null default now() )`, - 'alter table logs add column server_timestamp timestamptz not null default now() ' + db.addColumn('logs', 'server_timestamp', 'timestamptz not null default now()') ] db.multi(sql, next) diff --git a/migrations/1509439657189-add_machine_name_to_devices.js b/migrations/1509439657189-add_machine_name_to_devices.js index 36895d43..8ea29782 100644 --- a/migrations/1509439657189-add_machine_name_to_devices.js +++ b/migrations/1509439657189-add_machine_name_to_devices.js @@ -5,17 +5,17 @@ exports.up = function (next) { return migrateTools.migrateNames() .then(updateSql => { const sql = [ - 'alter table devices add column name text', + db.addColumn('devices', 'name', 'text'), updateSql, - 'alter table devices alter column name set not null' + db.alterColumn('devices', 'name', 'set not null') ] return db.multi(sql, next) }) .catch(() => { const sql = [ - 'alter table devices add column name text', - 'alter table devices alter column name set not null' + db.addColumn('devices', 'name', 'text'), + db.alterColumn('devices', 'name', 'set not null') ] return db.multi(sql, next) @@ -23,6 +23,8 @@ exports.up = function (next) { } exports.down = function (next) { - const sql = ['alter table devices drop column name'] + const sql = [ + db.dropColumn('devices', 'name') + ] db.multi(sql, next) } diff --git a/migrations/1514981004673-add_serial_to_logs.js b/migrations/1514981004673-add_serial_to_logs.js index 560b6549..ae1fa13b 100644 --- a/migrations/1514981004673-add_serial_to_logs.js +++ b/migrations/1514981004673-add_serial_to_logs.js @@ -1,7 +1,9 @@ const db = require('./db') exports.up = function (next) { - const sql = ['alter table logs add column serial integer not null default 0'] + const sql = [ + db.addColumn('logs', 'serial', 'integer not null default 0') + ] db.multi(sql, next) } diff --git a/migrations/1525671972351-add_sanctions_logs.js b/migrations/1525671972351-add_sanctions_logs.js index 591ea108..fb30e83d 100644 --- a/migrations/1525671972351-add_sanctions_logs.js +++ b/migrations/1525671972351-add_sanctions_logs.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { const sql = - [`create table sanctions_logs ( + [`create table if not exists sanctions_logs ( id uuid PRIMARY KEY, device_id text not null, sanctioned_id text not null, diff --git a/migrations/1526034901860-crypto_atoms_to_numeric.js b/migrations/1526034901860-crypto_atoms_to_numeric.js index a491f86c..ba8cf89c 100644 --- a/migrations/1526034901860-crypto_atoms_to_numeric.js +++ b/migrations/1526034901860-crypto_atoms_to_numeric.js @@ -2,13 +2,13 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_in_txs alter column cash_in_fee_crypto type numeric(30)', - 'alter table cash_in_txs alter column crypto_atoms type numeric(30)', - 'alter table cash_out_txs alter column crypto_atoms type numeric(30)', - 'alter table trades alter column crypto_atoms type numeric(30)', - 'alter table bills alter column crypto_atoms type numeric(30)', - 'alter table bills alter column cash_in_fee_crypto type numeric(30)', - 'alter table bills alter column crypto_atoms_after_fee type numeric(30)' + db.alterColumn('cash_in_txs', 'cash_in_fee_crypto', 'type numeric(30)'), + db.alterColumn('cash_in_txs', 'crypto_atoms', 'type numeric(30)'), + db.alterColumn('cash_out_txs', 'crypto_atoms', 'type numeric(30)'), + db.alterColumn('trades', 'crypto_atoms', 'type numeric(30)'), + db.alterColumn('bills', 'crypto_atoms', 'type numeric(30)'), + db.alterColumn('bills', 'cash_in_fee_crypto', 'type numeric(30)'), + db.alterColumn('bills', 'crypto_atoms_after_fee', 'type numeric(30)') ] db.multi(sql, next) } diff --git a/migrations/1526038623129-add_device_location.js b/migrations/1526038623129-add_device_location.js index a1adf5ca..604785e6 100644 --- a/migrations/1526038623129-add_device_location.js +++ b/migrations/1526038623129-add_device_location.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table devices add column last_online timestamptz not null default now()', - "alter table devices add column location json not null default '{}'" + db.addColumn('devices', 'last_online', 'timestamptz not null default now()'), + db.addColumn('devices', 'location', 'json not null default \'{}\'') ] db.multi(sql, next) } diff --git a/migrations/1527814550220-add-tx-terms-accepted.js b/migrations/1527814550220-add-tx-terms-accepted.js index 76a3b7de..18fa3558 100644 --- a/migrations/1527814550220-add-tx-terms-accepted.js +++ b/migrations/1527814550220-add-tx-terms-accepted.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { const sql = [ - 'alter table cash_in_txs add column terms_accepted boolean not null default false', - 'alter table cash_out_txs add column terms_accepted boolean not null default false' + db.addColumn('cash_in_txs', 'terms_accepted', 'boolean not null default false'), + db.addColumn('cash_out_txs', 'terms_accepted', 'boolean not null default false') ] db.multi(sql, next) diff --git a/migrations/1528017752387-add-layer2-address.js b/migrations/1528017752387-add-layer2-address.js index 6c19bf24..08ccd9b1 100644 --- a/migrations/1528017752387-add-layer2-address.js +++ b/migrations/1528017752387-add-layer2-address.js @@ -2,8 +2,8 @@ var db = require('./db') exports.up = function (next) { var sql = [ - 'alter table cash_out_txs add column layer_2_address text null', - 'alter table cash_out_actions add column layer_2_address text null' + db.addColumn('cash_out_txs', 'layer_2_address', 'text null'), + db.addColumn('cash_out_actions', 'layer_2_address', 'text null') ] db.multi(sql, next) } diff --git a/migrations/1536651947391-add-device-id-to-cash-out-actions.js b/migrations/1536651947391-add-device-id-to-cash-out-actions.js index 3a07a6bf..07d5f7a7 100644 --- a/migrations/1536651947391-add-device-id-to-cash-out-actions.js +++ b/migrations/1536651947391-add-device-id-to-cash-out-actions.js @@ -2,7 +2,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ - "alter table cash_out_actions add device_id text not null default ''" + db.addColumn('cash_out_actions', 'device_id', 'text not null default \'\'') ] db.multi(sql, next) } diff --git a/migrations/db.js b/migrations/db.js index a750effc..9145df68 100644 --- a/migrations/db.js +++ b/migrations/db.js @@ -1,7 +1,22 @@ +const _ = require('lodash/fp') const db = require('../lib/db') const sequential = require('promise-sequential') -module.exports = {multi} +module.exports = { + multi, + defineEnum, + dropEnum, + renameEnum, + alterColumn, + addColumn, + dropColumn, + renameColumn, + dropConstraint, + addConstraint, + addSequence, + alterSequence, + ifColumn +} function multi (sqls, cb) { const doQuery = s => { @@ -21,3 +36,139 @@ function multi (sqls, cb) { cb(err) }) } + +function beginEnd (statement) { + return ` + DO $$ + BEGIN + ${statement}; + END $$ +` +} + +function defineEnum (name, values) { + return beginEnd(` + IF NOT EXISTS ( + SELECT 1 FROM pg_type WHERE typname = '${name}' + ) THEN + CREATE TYPE ${name} AS ENUM (${values}); + END IF + `) +} + +function dropEnum (name) { + return beginEnd(` + IF EXISTS ( + SELECT 1 FROM pg_type WHERE typname = '${name}' + ) THEN + DROP TYPE ${name}; + END IF + `) +} + +function renameEnum (name, newName) { + return beginEnd(` + IF EXISTS ( + SELECT 1 FROM pg_type WHERE typname = '${name}' + ) THEN + IF NOT EXISTS ( + SELECT 1 FROM pg_type WHERE typname = '${newName}' + ) THEN + ALTER TYPE ${name} RENAME TO ${newName}; + END IF; + END IF + `) +} + +function ifColumn (table, column, statement, not, skipBeginEnd) { + statement = ` + IF EXISTS ( + SELECT NULL + FROM INFORMATION_SCHEMA.TABLEs + WHERE table_name = '${table}' + ) THEN + IF ${not ? 'NOT' : ''} EXISTS ( + SELECT NULL + FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = '${table}' + AND column_name = '${column}' + ) THEN + ${statement}; + END IF; + END IF + ` + return skipBeginEnd ? statement : beginEnd(statement) +} + +function alterColumn (table, column, change) { + return ifColumn(table, column, ` + ALTER TABLE ${table} + ALTER ${column} ${change}`) +} + +function addColumn (table, column, change) { + return ifColumn(table, column, ` + ALTER TABLE ${table} + ADD ${column} ${change}`, true) +} + +function dropColumn (table, column) { + return ifColumn(table, column, ` + ALTER TABLE ${table} + DROP ${column}`) +} + +function renameColumn (table, column, newName) { + return ifColumn(table, column, + ifColumn(table, column, ` + ALTER TABLE ${table} + RENAME ${column} to ${newName}`, true, true)) +} + +function dropConstraint (table, column) { + return beginEnd(` + IF EXISTS( + SELECT NULL + FROM INFORMATION_SCHEMA.constraint_column_usage + WHERE constraint_name = '${column}' + ) THEN + ALTER TABLE ${table} DROP CONSTRAINT ${column}; + END IF + `) +} + +function addConstraint (table, column, change, refTable, refColumn) { + return ifColumn(refTable, refColumn, ` + IF NOT EXISTS( + SELECT NULL + FROM INFORMATION_SCHEMA.constraint_column_usage + WHERE constraint_name = '${column}' + ) THEN + ALTER TABLE ${table} ADD CONSTRAINT ${column} ${change}; + END IF + `, false) +} + +function addSequence (name, change) { + return beginEnd(` + IF NOT EXISTS( + SELECT NULL + FROM INFORMATION_SCHEMA.sequences + WHERE sequence_name = '${name}' + ) THEN + CREATE SEQUENCE ${name} ${change}; + END IF + `) +} + +function alterSequence (name, change) { + return beginEnd(` + IF EXISTS( + SELECT NULL + FROM INFORMATION_SCHEMA.sequences + WHERE sequence_name = '${name}' + ) THEN + ALTER SEQUENCE ${name} ${change}; + END IF + `) +}