2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2016-04-20 06:03:55 -04:00
|
|
|
// external modules
|
2021-02-15 03:42:51 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const Sequelize = require('sequelize')
|
2019-05-30 18:27:56 -04:00
|
|
|
const { cloneDeep } = require('lodash')
|
2021-02-27 15:33:05 -05:00
|
|
|
const Umzug = require('umzug')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// core
|
2021-02-15 03:42:51 -05:00
|
|
|
const config = require('../config')
|
|
|
|
const logger = require('../logger')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const dbconfig = cloneDeep(config.db)
|
|
|
|
dbconfig.logging = config.debug
|
|
|
|
? (data) => {
|
|
|
|
logger.info(data)
|
|
|
|
}
|
|
|
|
: false
|
2016-12-11 21:18:12 -05:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let sequelize = null
|
2016-12-22 00:23:17 -05:00
|
|
|
|
2016-12-22 06:42:00 -05:00
|
|
|
// Heroku specific
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.dbURL) {
|
|
|
|
sequelize = new Sequelize(config.dbURL, dbconfig)
|
2017-03-08 05:45:51 -05:00
|
|
|
} else {
|
|
|
|
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
|
|
|
|
}
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-01-01 22:05:05 -05:00
|
|
|
// [Postgres] Handling NULL bytes
|
|
|
|
// https://github.com/sequelize/sequelize/issues/6485
|
2017-03-08 05:45:51 -05:00
|
|
|
function stripNullByte (value) {
|
2017-03-15 10:12:24 -04:00
|
|
|
value = '' + value
|
2018-11-14 08:10:14 -05:00
|
|
|
// eslint-disable-next-line no-control-regex
|
2017-03-08 05:45:51 -05:00
|
|
|
return value ? value.replace(/\u0000/g, '') : value
|
2017-01-01 22:05:05 -05:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
sequelize.stripNullByte = stripNullByte
|
2017-01-01 22:05:05 -05:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function processData (data, _default, process) {
|
|
|
|
if (data === undefined) return data
|
|
|
|
else return data === null ? _default : (process ? process(data) : data)
|
2017-01-01 22:05:36 -05:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
sequelize.processData = processData
|
2017-01-01 22:05:36 -05:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const db = {}
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
fs.readdirSync(__dirname)
|
2019-05-30 18:27:56 -04:00
|
|
|
.filter(function (file) {
|
|
|
|
return (file.indexOf('.') !== 0) && (file !== 'index.js')
|
|
|
|
})
|
|
|
|
.forEach(function (file) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const model = sequelize.import(path.join(__dirname, file))
|
2019-05-30 18:27:56 -04:00
|
|
|
db[model.name] = model
|
|
|
|
})
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
Object.keys(db).forEach(function (modelName) {
|
2017-03-08 05:45:51 -05:00
|
|
|
if ('associate' in db[modelName]) {
|
|
|
|
db[modelName].associate(db)
|
|
|
|
}
|
|
|
|
})
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2021-02-27 15:33:05 -05:00
|
|
|
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)
|
2021-07-20 18:05:35 -04:00
|
|
|
logger.error(`Database migration failed.
|
|
|
|
This can be the result of upgrading from quite old versions and requires manual database intervention.
|
|
|
|
See https://docs.hedgedoc.org/guides/migration-troubleshooting/ for help.
|
|
|
|
Exiting…`)
|
2021-02-27 15:33:05 -05:00
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
logger.info('All migrations performed successfully')
|
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
db.sequelize = sequelize
|
|
|
|
db.Sequelize = Sequelize
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
module.exports = db
|