diff options
author | David Mehren | 2021-03-04 20:33:30 +0100 |
---|---|---|
committer | GitHub | 2021-03-04 20:33:30 +0100 |
commit | 64a2fb8be71792e15b76fceeb948ddfc3c9de704 (patch) | |
tree | 4bf5d1771c172d052246530f76316b5b997dd9c0 /lib | |
parent | 6186e0f48f3414e9de4ab5f864c2d153aa995c51 (diff) | |
parent | b47f1e80b43839de20591544dacd016822bacb63 (diff) |
Merge pull request #969 from hedgedoc/feature/port_automated_migrations
Diffstat (limited to 'lib')
-rw-r--r-- | lib/models/index.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/models/index.js b/lib/models/index.js index e752a993..4558b25e 100644 --- a/lib/models/index.js +++ b/lib/models/index.js @@ -4,6 +4,7 @@ const fs = require('fs') const path = require('path') const Sequelize = require('sequelize') const { cloneDeep } = require('lodash') +const Umzug = require('umzug') // core const config = require('../config') @@ -57,6 +58,36 @@ Object.keys(db).forEach(function (modelName) { } }) +const umzug = new Umzug({ + migrations: { + path: path.resolve(__dirname, '..', 'migrations'), + params: [ + sequelize.getQueryInterface(), + Sequelize.DataTypes + ] + }, + // Required wrapper function required to prevent winstion issue + // https://github.com/winstonjs/winston/issues/1577 + logging: message => { + logger.info(message) + }, + storage: 'sequelize', + storageOptions: { + sequelize: sequelize + } +}) + +db.runMigrations = async function runMigrations () { + // checks migrations and run them if they are not already applied + // exit in case of unsuccessful migrations + await umzug.up().catch(error => { + logger.error(error) + logger.error('Database migration failed. Exiting…') + process.exit(1) + }) + logger.info('All migrations performed successfully') +} + db.sequelize = sequelize db.Sequelize = Sequelize |