filesafe/database/migration.js
Bobby Wibowo 98e6c59255
expanded gulp linter to lint server-side JS files
changed reporter formatter for gulp stylelint from verbose to string
this should now only print out issues to console

linted other server-side JS files that i missed out on previous commits

bumped versions.js to trigger github actions
2020-11-10 22:56:18 +07:00

58 lines
1.6 KiB
JavaScript

const perms = require('./../controllers/permissionController')
const config = require('./../config')
const db = require('knex')(config.database)
const map = {
files: {
expirydate: 'integer'
},
albums: {
editedAt: 'integer',
zipGeneratedAt: 'integer',
download: 'integer',
public: 'integer',
description: 'string'
},
users: {
enabled: 'integer',
permission: 'integer',
registration: 'integer'
}
}
;(async () => {
const tableNames = Object.keys(map)
for (const tableName of tableNames) {
const columnNames = Object.keys(map[tableName])
for (const columnName of columnNames) {
if (await db.schema.hasColumn(tableName, columnName)) continue
const columnType = map[tableName][columnName]
await db.schema.table(tableName, table => {
table[columnType](columnName)
})
console.log(`OK: ${tableName} <- ${columnName} (${columnType})`)
}
}
await db.table('users')
.where('username', 'root')
.first()
.update({
permission: perms.permissions.superadmin
})
.then(result => {
// NOTE: permissionController.js actually has a hard-coded check for "root" account so that
// it will always have "superadmin" permission regardless of its permission value in database
if (!result) return console.log('Unable to update root\'s permission into superadmin.')
console.log(`Updated root's permission to ${perms.permissions.superadmin} (superadmin).`)
})
console.log('Migration finished! Now you may start lolisafe normally.')
})()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})