2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./../config')
|
2018-01-23 20:06:30 +00:00
|
|
|
const db = require('knex')(config.database)
|
2018-10-13 11:06:58 +00:00
|
|
|
const perms = require('./../controllers/permissionController')
|
2017-10-04 05:05:38 +00:00
|
|
|
|
2018-04-05 03:10:10 +00:00
|
|
|
const map = {
|
|
|
|
albums: {
|
|
|
|
editedAt: 'integer',
|
2018-04-28 17:26:39 +00:00
|
|
|
zipGeneratedAt: 'integer',
|
|
|
|
download: 'integer',
|
2018-12-13 09:09:46 +00:00
|
|
|
public: 'integer',
|
|
|
|
description: 'string'
|
2018-04-05 03:10:10 +00:00
|
|
|
},
|
|
|
|
users: {
|
|
|
|
enabled: 'integer',
|
2018-10-09 19:52:41 +00:00
|
|
|
fileLength: 'integer',
|
|
|
|
permission: 'integer'
|
2018-04-05 03:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const migration = {}
|
2017-10-04 05:05:38 +00:00
|
|
|
migration.start = async () => {
|
2018-04-05 03:10:10 +00:00
|
|
|
const tables = Object.keys(map)
|
|
|
|
await Promise.all(tables.map(table => {
|
|
|
|
const columns = Object.keys(map[table])
|
|
|
|
return Promise.all(columns.map(async column => {
|
2018-09-07 15:34:00 +00:00
|
|
|
if (await db.schema.hasColumn(table, column)) {
|
2018-12-13 09:09:46 +00:00
|
|
|
return // console.log(`SKIP: ${column} => ${table}.`)
|
2018-09-07 15:34:00 +00:00
|
|
|
}
|
2018-04-05 03:10:10 +00:00
|
|
|
const columnType = map[table][column]
|
|
|
|
return db.schema.table(table, t => { t[columnType](column) })
|
2018-12-13 09:09:46 +00:00
|
|
|
.then(() => console.log(`OK: ${column} (${columnType}) => ${table}.`))
|
2018-04-05 03:10:10 +00:00
|
|
|
.catch(console.error)
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
await db.table('users')
|
|
|
|
.where('username', 'root')
|
|
|
|
.first()
|
|
|
|
.update({
|
2018-10-13 11:06:58 +00:00
|
|
|
permission: perms.permissions.superadmin
|
2018-10-09 19:52:41 +00:00
|
|
|
})
|
2018-10-12 09:42:16 +00:00
|
|
|
.then(rows => {
|
2018-12-08 03:33:57 +00:00
|
|
|
// NOTE: permissionController.js actually have a hard-coded check for "root" account so that
|
|
|
|
// it will always have "superadmin" permission regardless of its permission value in database
|
2018-10-12 09:42:16 +00:00
|
|
|
if (!rows) { return console.log('Unable to update root\'s permission into superadmin.') }
|
2018-10-13 11:06:58 +00:00
|
|
|
console.log(`Updated root's permission to ${perms.permissions.superadmin} (superadmin).`)
|
2018-10-12 09:42:16 +00:00
|
|
|
})
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2018-12-13 09:09:46 +00:00
|
|
|
console.log('Migration finished! Now you may start lolisafe normally.')
|
2018-03-14 06:57:09 +00:00
|
|
|
process.exit(0)
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 05:05:38 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
migration.start()
|