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
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!"