mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-15 07:34:42 -04:00
Run database migrations automatically on startup
This commit removes the need for separate migrations with the sequelize-cli by running them with umzug on application startup. This is a port of #384 Co-authored-by: Sheogorath <sheogorath@shivering-isles.com> Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
6186e0f48f
commit
0db862f3c9
4 changed files with 54 additions and 315 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue