filesafe/database/migration.js
2018-10-10 02:52:41 +07:00

48 lines
1.3 KiB
JavaScript

const config = require('./../config')
const db = require('knex')(config.database)
const { permissions } = require('./../controllers/authController')
const map = {
albums: {
editedAt: 'integer',
zipGeneratedAt: 'integer',
download: 'integer',
public: 'integer'
},
users: {
enabled: 'integer',
fileLength: 'integer',
permission: 'integer'
}
}
const migration = {}
migration.start = async () => {
const tables = Object.keys(map)
await Promise.all(tables.map(table => {
const columns = Object.keys(map[table])
return Promise.all(columns.map(async column => {
if (await db.schema.hasColumn(table, column)) {
return console.log(`Column "${column}" already exists in table "${table}".`)
}
const columnType = map[table][column]
return db.schema.table(table, t => { t[columnType](column) })
.then(() => console.log(`Added column "${column}" to table "${table}".`))
.catch(console.error)
}))
}))
await db.table('users')
.where('username', 'root')
.first()
.update({
permission: permissions.superadmin
})
.then(() => console.log(`Updated root's permission to ${permissions.superadmin} (superadmin).`))
console.log('Migration finished! Now start lolisafe normally')
process.exit(0)
}
migration.start()