mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 09:45:37 -04:00
Run database migrations automatically on startup
Instead of using sequelize-cli and ensure migrations by shellscript, this patch automates database migrations properly to the umzug library. The sequelize CLI becomes a dev dependencies as it's still useful for generating migrations. This should eliminate the need for crude generating of database config files and alike. Instead we utilize the pre-configured sequelize connection that CodiMD will use anyway. Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
This commit is contained in:
parent
2c3522992b
commit
6c1ca5bd8d
19 changed files with 70 additions and 35 deletions
|
@ -1,5 +1,6 @@
|
|||
import { Sequelize } from 'sequelize-typescript'
|
||||
import { cloneDeep } from 'lodash'
|
||||
import * as path from 'path'
|
||||
import { Author } from './author'
|
||||
import { Note } from './note'
|
||||
import { Revision } from './revision'
|
||||
|
@ -7,6 +8,8 @@ import { Temp } from './temp'
|
|||
import { User } from './user'
|
||||
import { logger } from '../logger'
|
||||
import { config } from '../config'
|
||||
import Umzug from 'umzug'
|
||||
import SequelizeTypes from 'sequelize'
|
||||
|
||||
const dbconfig = cloneDeep(config.db)
|
||||
dbconfig.logging = config.debug ? (data): void => {
|
||||
|
@ -22,6 +25,36 @@ if (config.dbURL) {
|
|||
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
|
||||
}
|
||||
|
||||
const umzug = new Umzug({
|
||||
migrations: {
|
||||
path: path.resolve(__dirname, '..', 'migrations'),
|
||||
params: [
|
||||
sequelize.getQueryInterface(),
|
||||
SequelizeTypes
|
||||
]
|
||||
},
|
||||
// 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
|
||||
}
|
||||
})
|
||||
|
||||
export async function runMigrations(): Promise<void> {
|
||||
// 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')
|
||||
}
|
||||
|
||||
sequelize.addModels([Author, Note, Revision, Temp, User])
|
||||
|
||||
export { Author, Note, Revision, Temp, User }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue