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',
|
|
|
|
public: 'integer'
|
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)) {
|
|
|
|
return console.log(`Column "${column}" already exists in table "${table}".`)
|
|
|
|
}
|
2018-04-05 03:10:10 +00:00
|
|
|
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)
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
|
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 => {
|
|
|
|
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-01-23 20:06:30 +00:00
|
|
|
console.log('Migration finished! Now 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()
|