Save migrations file on db (#215)

* Revert "Migration sql changes (#199)"

This reverts commit 12c834469c.

* Save migrate file on db

* Add message on migration error in lamassu-update
This commit is contained in:
Rafael Taranto 2018-11-18 07:15:22 -02:00 committed by Josh Harvey
parent 9af204e609
commit 06f8c57608
58 changed files with 1516 additions and 567 deletions

View file

@ -1,8 +1,34 @@
#!/usr/bin/env node
const FileStore = require('migrate/lib/file-store')
const db = require('../lib/db')
const migrate = require('../lib/migrate')
const options = require('../lib/options')
migrate.run()
const createMigration = `CREATE TABLE IF NOT EXISTS migrations (
id serial PRIMARY KEY,
data json NOT NULL
)`
const select = 'select * from migrations limit 1'
const getMigrateFile = () => {
return new Promise((resolve, reject) => {
new FileStore(options.migrateStatePath).load((err, store) => {
if (err) return reject(err)
return resolve(store)
})
})
}
db.none(createMigration)
.then(() => Promise.all([db.oneOrNone(select), getMigrateFile()]))
.then(([qResult, migrateFile]) => {
if (!qResult && migrateFile) {
return db.none('insert into migrations (id, data) values (1, $1)', [migrateFile])
}
})
.then(() => migrate.run())
.then(() => {
console.log('DB Migration succeeded.')
process.exit(0)

View file

@ -28,6 +28,13 @@ if [ "$(whoami)" != "root" ]; then
exit 3
fi
# Use a lock file so failed scripts cannot be imediately retried
# If not the backup created on this script would be replaced
if ! mkdir /var/lock/lamassu-update; then
echo "Script is locked because of a failure." >&2
exit 1
fi
decho "stopping lamassu-server"
supervisorctl stop lamassu-server >> ${LOG_FILE} 2>&1
supervisorctl stop lamassu-admin-server >> ${LOG_FILE} 2>&1
@ -53,8 +60,11 @@ decho "rebuilding npm deps"
cd $(npm root -g)/lamassu-server/ >> ${LOG_FILE} 2>&1
npm rebuild >> ${LOG_FILE} 2>&1
{
decho "running migration"
lamassu-migrate >> ${LOG_FILE} 2>&1
} || { echo "Failure running migrations" ; exit 1 ; }
lamassu-migrate-config >> ${LOG_FILE} 2>&1
decho "update to mnemonic"
@ -88,4 +98,6 @@ set -e
# reset terminal to link new executables
hash -r
rm -r /var/lock/lamassu-update
decho "Update complete!"

22
lib/db-migrate-store.js Normal file
View file

@ -0,0 +1,22 @@
const db = require('../lib/db')
const upsert = 'insert into migrations (id, data) values (1, $1) on conflict (id) do update set data = $1'
function DbMigrateStore () {
}
DbMigrateStore.prototype.save = function (set, fn) {
let insertData = JSON.stringify({
lastRun: set.lastRun,
migrations: set.migrations
})
db.none(upsert, [insertData]).then(fn).catch(err => console.log(err))
}
DbMigrateStore.prototype.load = function (fn) {
db.one('select data from migrations').then(({ data }) => {
fn(null, data)
})
}
module.exports = DbMigrateStore

View file

@ -1,18 +1,24 @@
const path = require('path')
const migrate = require('migrate')
const options = require('./options')
const DbMigrateStore = require('./db-migrate-store')
const migrateDir = path.resolve(__dirname, '..', 'migrations')
const migration = migrate.load(options.migrateStatePath, migrateDir)
const migrateOpts = {
migrationsDirectory: migrateDir,
stateStore: new DbMigrateStore(),
filterFunction: it => it.match(/^\d+.*\.js$/)
}
module.exports = { run }
function run () {
return new Promise((resolve, reject) => {
migration.up(err => {
migrate.load(migrateOpts, (err, set) => {
if (err) return reject(err)
set.up(err => {
if (err) return reject(err)
return resolve(0)
})
})
})
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
const sql =
['CREATE TABLE IF NOT EXISTS bills ( ' +
['CREATE TABLE bills ( ' +
'id uuid PRIMARY KEY, ' +
'device_fingerprint text NOT NULL, ' +
'denomination integer NOT NULL, ' +
@ -11,8 +11,7 @@ exports.up = function (next) {
'to_address text NOT NULL, ' +
'session_id uuid NOT NULL, ' +
'device_time bigint NOT NULL, ' +
'created timestamp NOT NULL DEFAULT now() ' +
')']
'created timestamp NOT NULL DEFAULT now() )']
db.multi(sql, next)
}

View file

@ -7,8 +7,7 @@ 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) {

View file

@ -11,10 +11,10 @@ exports.up = function (next) {
'published', 'authorized', 'confirmed'].map(singleQuotify).join(',')
var sqls = [
db.defineEnum('transaction_stage', stages),
db.defineEnum('transaction_authority', authorizations),
'CREATE TYPE transaction_stage AS ENUM (' + stages + ')',
'CREATE TYPE transaction_authority AS ENUM (' + authorizations + ')',
'CREATE TABLE IF NOT EXISTS transactions ( ' +
'CREATE TABLE transactions ( ' +
'id serial PRIMARY KEY, ' +
'session_id uuid NOT NULL, ' +
'device_fingerprint text, ' +
@ -33,7 +33,7 @@ exports.up = function (next) {
')',
'CREATE INDEX ON transactions (session_id)',
'CREATE TABLE IF NOT EXISTS pending_transactions ( ' +
'CREATE TABLE pending_transactions ( ' +
'id serial PRIMARY KEY, ' +
'device_fingerprint text NOT NULL, ' +
'session_id uuid UNIQUE NOT NULL, ' +
@ -44,7 +44,7 @@ exports.up = function (next) {
'updated timestamp NOT NULL DEFAULT now() ' +
')',
'CREATE TABLE IF NOT EXISTS dispenses ( ' +
'CREATE TABLE dispenses ( ' +
'id serial PRIMARY KEY, ' +
'device_fingerprint text NOT NULL, ' +
'transaction_id integer UNIQUE REFERENCES transactions(id), ' +
@ -58,7 +58,7 @@ exports.up = function (next) {
'error text, ' +
'created timestamp NOT NULL DEFAULT now() ' +
')',
db.ifColumn('dispenses', 'device_fingerprint', 'CREATE INDEX ON dispenses (device_fingerprint)')
'CREATE INDEX ON dispenses (device_fingerprint)'
]
db.multi(sqls, next)

View file

@ -2,12 +2,12 @@ var db = require('./db')
exports.up = function (next) {
var sqls = [
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')
'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.multi(sqls, next)

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('transactions', 'phone', 'text'),
'alter table transactions add phone text',
'create index on transactions (phone)'
]
db.multi(sql, next)

View file

@ -8,12 +8,12 @@ exports.up = function (next) {
.map(singleQuotify).join(',')
var sql = [
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\'')
'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.multi(sql, next)
}

View file

@ -2,12 +2,12 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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')
'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.multi(sql, next)
}

View file

@ -9,7 +9,7 @@ exports.up = function (next) {
.map(singleQuotify).join(',')
var sql = [
`CREATE TABLE IF NOT EXISTS cash_in_txs (
`create table 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 IF NOT EXISTS cash_out_txs (
`create table 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,16 +41,15 @@ exports.up = function (next) {
created timestamptz NOT NULL default now(),
confirmation_time timestamptz
)`,
db.defineEnum('cash_out_action_types', actions),
`CREATE TABLE IF NOT EXISTS cash_out_actions (
`create type cash_out_action_types AS ENUM (${actions})`,
`create table cash_out_actions (
id serial PRIMARY KEY,
session_id uuid,
session_id uuid REFERENCES cash_out_txs(session_id),
action cash_out_action_types NOT NULL,
created timestamptz NOT NULL default now()
)`,
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')
`alter table dispenses add session_id uuid`,
`alter table dispenses drop constraint dispenses_transaction_id_fkey`
]
db.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
`CREATE TABLE IF NOT EXISTS cash_out_hds (
`create table cash_out_hds (
session_id uuid PRIMARY KEY,
crypto_code text NOT NULL,
hd_serial integer NOT NULL,

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('cash_out_hds', 'last_checked', 'timestamptz not null default now()'),
db.addColumn('cash_out_hds', 'confirmed', 'boolean not null default false'),
'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',
'create index on cash_out_hds (confirmed, last_checked)'
]
db.multi(sql, next)

View file

@ -2,31 +2,31 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.renameColumn('bills', 'device_fingerprint', 'device_id'),
db.renameColumn('bills', 'satoshis', 'crypto_atoms'),
db.renameColumn('bills', 'session_id', 'cash_in_txs_id'),
'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('cached_responses', 'device_fingerprint', 'device_id'),
db.renameColumn('cached_responses', 'session_id', 'tx_id'),
'alter table cached_responses rename device_fingerprint to device_id',
'alter table cached_responses rename session_id to tx_id',
db.renameColumn('cash_in_txs', 'session_id', 'id'),
db.renameColumn('cash_in_txs', 'device_fingerprint', 'device_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_out_actions', 'session_id', 'cash_out_txs_id'),
'alter table cash_out_actions rename session_id to cash_out_txs_id',
db.renameColumn('cash_out_hds', 'session_id', 'id'),
'alter table cash_out_hds rename session_id to id',
db.renameColumn('cash_out_txs', 'session_id', 'id'),
db.renameColumn('cash_out_txs', 'device_fingerprint', 'device_id'),
'alter table cash_out_txs rename session_id to id',
'alter table cash_out_txs rename device_fingerprint to device_id',
db.renameColumn('devices', 'fingerprint', 'device_id'),
'alter table devices rename fingerprint to device_id',
db.renameColumn('dispenses', 'session_id', 'cash_out_txs_id'),
db.renameColumn('dispenses', 'device_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('machine_configs', 'device_fingerprint', 'device_id'),
'alter table machine_configs rename device_fingerprint to device_id',
db.renameColumn('machine_events', 'device_fingerprint', 'device_id')
'alter table machine_events rename device_fingerprint to device_id'
]
db.multi(sql, next)
}

View file

@ -2,9 +2,9 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.dropColumn('devices', 'authorized'),
db.dropColumn('devices', 'unpair'),
`CREATE TABLE IF NOT EXISTS paired_devices (
'alter table devices drop authorized',
'alter table devices drop unpair',
`create table paired_devices (
device_id text PRIMARY KEY,
created timestamptz NOT NULL default now()
)`

View file

@ -3,7 +3,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
'drop table if exists cached_responses',
`create table if not exists idempotents (
`create table idempotents (
request_id text PRIMARY KEY,
device_id text NOT NULL,
body json NOT NULL,

View file

@ -3,12 +3,12 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
'drop table if exists users',
`create table if not exists user_tokens (
`create table user_tokens (
token text PRIMARY KEY,
name text NOT NULL,
created timestamptz NOT NULL default now()
)`,
`create table if not exists one_time_passes (
`create table one_time_passes (
token text PRIMARY KEY,
name text NOT NULL,
created timestamptz NOT NULL default now()

View file

@ -4,7 +4,7 @@ exports.up = function (next) {
var sql = [
'drop table if exists devices',
'drop table if exists paired_devices',
`create table if not exists devices (
`create table 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()
)`,
db.addColumn('pairing_tokens', 'name', 'text NOT NULL')
'alter table pairing_tokens add column name text NOT NULL'
]
db.multi(sql, next)
}

View file

@ -2,9 +2,9 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.dropColumn('dispenses', 'count1'),
db.dropColumn('dispenses', 'count2'),
db.dropColumn('dispenses', 'refill')
'alter table dispenses drop column count1',
'alter table dispenses drop column count2',
'alter table dispenses drop column refill'
]
db.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
`create table if not exists server_events (
`create table server_events (
id serial PRIMARY KEY,
event_type text NOT NULL,
created timestamptz NOT NULL default now()

View file

@ -2,9 +2,11 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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)')
'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.multi(sql, next)
}

View file

@ -2,13 +2,13 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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')
'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.multi(sql, next)
}

View file

@ -2,15 +2,15 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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'
'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.multi(sql, next)
}

View file

@ -2,18 +2,18 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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 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',
'create unique index on cash_out_txs (hd_index)',
'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'
'drop table cash_out_hds',
'drop table cash_out_actions',
'drop table transactions',
'drop table idempotents',
'drop table machine_configs',
'drop table pending_transactions'
]
db.multi(sql, next)
}

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.defineEnum('trade_type', "'buy', 'sell'"),
`create table if not exists trades (
"create type trade_type as enum ('buy', 'sell')",
`create table trades (
id serial PRIMARY KEY,
type trade_type not null,
crypto_code text not null,

View file

@ -2,15 +2,15 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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')
'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.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
`create table if not exists cash_in_actions (
`create table cash_in_actions (
id serial primary key,
tx_id uuid not null,
action text not null,

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
`create table if not exists cash_out_actions (
`create table 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()
)`,
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')
'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.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('user_config', 'valid', 'boolean not null')
'alter table user_config add column valid boolean not null'
]
db.multi(sql, next)
}

View file

@ -2,10 +2,10 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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')
'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.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ const db = require('./db')
exports.up = function (next) {
const sql = [
db.dropColumn('devices', 'name')
'alter table devices drop column name'
]
db.multi(sql, next)
}

View file

@ -2,13 +2,13 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
`create table if not exists machine_pings (
`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 if not exists aggregated_machine_pings (
`create table aggregated_machine_pings (
id uuid PRIMARY KEY,
device_id text not null,
dropped_pings integer not null,
@ -18,8 +18,8 @@ exports.up = function (next) {
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')
'alter table machine_events drop column device_time',
'alter table machine_events add column device_time timestamptz'
]
db.multi(sql, next)
}

View file

@ -2,12 +2,12 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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')
'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.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('cash_out_txs', 'error_code', 'text')
'alter table cash_out_txs add column error_code text'
]
db.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
`create table if not exists cash_out_refills (
`create table cash_out_refills (
id uuid PRIMARY KEY,
device_id text not null,
user_id integer not null,
@ -11,7 +11,7 @@ exports.up = function (next) {
denomination1 integer not null,
denomination2 integer not null,
created timestamptz not null default now())`,
`create table if not exists cash_in_refills (
`create table cash_in_refills (
id uuid PRIMARY KEY,
device_id text not null,
user_id integer not null,

View file

@ -3,7 +3,7 @@ var anonymous = require('../lib/constants').anonymousCustomer
exports.up = function (next) {
const sql =
[`create table if not exists customers (
[`create table customers (
id uuid PRIMARY KEY,
phone text unique,
phone_at timestamptz,
@ -20,9 +20,9 @@ exports.up = function (next) {
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}'`)
`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}'`
]
db.multi(sql, next)

View file

@ -1,9 +1,9 @@
var db = require('./db')
exports.up = function (next) {
const sql = [
db.defineEnum('compliance_types', "'manual', 'sanctions', 'sanctions_override'"),
`create table if not exists compliance_authorizations (
const sql =
[ "create type compliance_types as enum ('manual', 'sanctions', 'sanctions_override')",
`create table compliance_authorizations (
id uuid PRIMARY KEY,
customer_id uuid REFERENCES customers (id),
compliance_type compliance_types NOT NULL,

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
const sql = [
db.dropColumn('cash_in_txs', 'device_time'),
db.dropColumn('cash_out_txs', 'device_time')
'alter table cash_in_txs drop column device_time',
'alter table cash_out_txs drop column device_time'
]
db.multi(sql, next)

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
const sql = [
db.addColumn('cash_in_txs', 'tx_version', 'integer not null'),
db.addColumn('cash_out_txs', 'tx_version', 'integer not null')
'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.multi(sql, next)

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
const sql = [
db.addColumn('cash_out_txs', 'published_at', 'timestamptz'),
db.renameColumn('cash_out_txs', 'confirmation_time', 'confirmed_at')
'alter table cash_out_txs add column published_at timestamptz',
'alter table cash_out_txs rename column confirmation_time to confirmed_at'
]
db.multi(sql, next)

View file

@ -12,38 +12,39 @@ exports.up = function (next) {
*
* @see {@link http://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/}
*/
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 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('verification_type', "'verified', 'blocked', 'automatic'"),
"create type verification_type as enum ('verified', 'blocked', 'automatic')",
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 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',
'alter table compliance_authorizations rename to 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')
'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.multi(sql, next)

View file

@ -4,21 +4,21 @@ const db = require('./db')
exports.up = function (next) {
const sql = [
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'),
'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',
/**
* Replace all compliance_type enum values
*
@ -27,10 +27,11 @@ exports.up = function (next) {
*
* @see {@link http://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/}
*/
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')
'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.multi(sql, next)

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
const sql =
[`create table if not exists logs (
[`create table logs (
id uuid PRIMARY KEY,
device_id text,
log_level text,

View file

@ -2,11 +2,11 @@ var db = require('./db')
exports.up = function (next) {
const sql =
[`create table if not exists support_logs (
[`create table support_logs (
id uuid PRIMARY KEY,
device_id text,
timestamp timestamptz not null default now() )`,
db.addColumn('logs', 'server_timestamp', 'timestamptz not null default now()')
'alter table logs add column server_timestamp timestamptz not null default now() '
]
db.multi(sql, next)

View file

@ -5,17 +5,17 @@ exports.up = function (next) {
return migrateTools.migrateNames()
.then(updateSql => {
const sql = [
db.addColumn('devices', 'name', 'text'),
'alter table devices add column name text',
updateSql,
db.alterColumn('devices', 'name', 'set not null')
'alter table devices alter column name set not null'
]
return db.multi(sql, next)
})
.catch(() => {
const sql = [
db.addColumn('devices', 'name', 'text'),
db.alterColumn('devices', 'name', 'set not null')
'alter table devices add column name text',
'alter table devices alter column name set not null'
]
return db.multi(sql, next)
@ -23,8 +23,6 @@ exports.up = function (next) {
}
exports.down = function (next) {
const sql = [
db.dropColumn('devices', 'name')
]
const sql = ['alter table devices drop column name']
db.multi(sql, next)
}

View file

@ -1,9 +1,7 @@
const db = require('./db')
exports.up = function (next) {
const sql = [
db.addColumn('logs', 'serial', 'integer not null default 0')
]
const sql = ['alter table logs add column serial integer not null default 0']
db.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
const sql =
[`create table if not exists sanctions_logs (
[`create table sanctions_logs (
id uuid PRIMARY KEY,
device_id text not null,
sanctioned_id text not null,

View file

@ -2,13 +2,13 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
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)')
'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.multi(sql, next)
}

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('devices', 'last_online', 'timestamptz not null default now()'),
db.addColumn('devices', 'location', 'json not null default \'{}\'')
'alter table devices add column last_online timestamptz not null default now()',
"alter table devices add column location json not null default '{}'"
]
db.multi(sql, next)
}

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
const sql = [
db.addColumn('cash_in_txs', 'terms_accepted', 'boolean not null default false'),
db.addColumn('cash_out_txs', 'terms_accepted', 'boolean not null default false')
'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.multi(sql, next)

View file

@ -2,8 +2,8 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('cash_out_txs', 'layer_2_address', 'text null'),
db.addColumn('cash_out_actions', 'layer_2_address', 'text null')
'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.multi(sql, next)
}

View file

@ -2,7 +2,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
db.addColumn('cash_out_actions', 'device_id', 'text not null default \'\'')
"alter table cash_out_actions add device_id text not null default ''"
]
db.multi(sql, next)
}

View file

@ -1,22 +1,7 @@
const _ = require('lodash/fp')
const db = require('../lib/db')
const sequential = require('promise-sequential')
module.exports = {
multi,
defineEnum,
dropEnum,
renameEnum,
alterColumn,
addColumn,
dropColumn,
renameColumn,
dropConstraint,
addConstraint,
addSequence,
alterSequence,
ifColumn
}
module.exports = {multi}
function multi (sqls, cb) {
const doQuery = s => {
@ -36,139 +21,3 @@ 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, newName, `
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
`)
}

1171
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -37,7 +37,7 @@
"mailgun-js": "^0.21.0",
"make-dir": "^1.0.0",
"mem": "^1.1.0",
"migrate": "^0.2.2",
"migrate": "^1.6.2",
"minimist": "^1.2.0",
"moment": "^2.17.0",
"morgan": "^1.8.2",