Updates and merged changes from 'master'

* Removing files from an album will now update the state of the said album. Which means, the Download Album as zip feature will properly make a new zip instead of re-using the old one which would obviously still have the said files.

* "migration.js" will no longer try to create column if it already exists.

* Changed type of all columns that used to be DATETIME to INTEGER. Apparently SQLite would have stored them as INTEGER anyways, so you don't have to change anything else.
This commit is contained in:
Bobby Wibowo 2018-04-05 10:10:10 +07:00
commit aea89ecbd4
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
3 changed files with 36 additions and 9 deletions

View File

@ -248,6 +248,8 @@ albumsController.addFiles = async (req, res, next) => {
if (typeof albumid !== 'number') { albumid = parseInt(albumid) } if (typeof albumid !== 'number') { albumid = parseInt(albumid) }
if (isNaN(albumid) || (albumid < 0)) { albumid = null } if (isNaN(albumid) || (albumid < 0)) { albumid = null }
const albumIds = []
if (albumid !== null) { if (albumid !== null) {
const album = await db.table('albums') const album = await db.table('albums')
.where({ .where({
@ -262,6 +264,8 @@ albumsController.addFiles = async (req, res, next) => {
description: 'Album doesn\'t exist or it doesn\'t belong to the user.' description: 'Album doesn\'t exist or it doesn\'t belong to the user.'
}) })
} }
albumIds.push(albumid)
} }
const files = await db.table('files') const files = await db.table('files')
@ -275,6 +279,10 @@ albumsController.addFiles = async (req, res, next) => {
const failedIds = ids.filter(id => !files.find(file => file.id === id)) const failedIds = ids.filter(id => !files.find(file => file.id === id))
await Promise.all(files.map(file => { await Promise.all(files.map(file => {
if (file.albumid && !albumIds.includes(file.albumid)) {
albumIds.push(file.albumid)
}
return db.table('files') return db.table('files')
.where('id', file.id) .where('id', file.id)
.update('albumid', albumid) .update('albumid', albumid)
@ -285,11 +293,11 @@ albumsController.addFiles = async (req, res, next) => {
})) }))
if (failedIds.length < ids.length) { if (failedIds.length < ids.length) {
if (albumid !== null) { await Promise.all(albumIds.map(albumid => {
await db.table('albums') return db.table('albums')
.where('id', albumid) .where('id', albumid)
.update('editedAt', Math.floor(Date.now() / 1000)) .update('editedAt', Math.floor(Date.now() / 1000))
} }))
return res.json({ return res.json({
success: true, success: true,
failedIds failedIds

View File

@ -9,8 +9,8 @@ let init = function (db) {
table.string('identifier') table.string('identifier')
table.integer('enabled') table.integer('enabled')
table.integer('timestamp') table.integer('timestamp')
table.dateTime('editedAt') table.integer('editedAt')
table.dateTime('zipGeneratedAt') table.integer('zipGeneratedAt')
}).then(() => {}) }).then(() => {})
} }
}) })

View File

@ -1,12 +1,31 @@
const config = require('../config.js') const config = require('../config.js')
const db = require('knex')(config.database) const db = require('knex')(config.database)
const map = {
albums: {
editedAt: 'integer',
zipGeneratedAt: 'integer'
},
users: {
enabled: 'integer',
fileLength: 'integer'
}
}
const migration = {} const migration = {}
migration.start = async () => { migration.start = async () => {
await db.schema.table('albums', t => t.dateTime('editedAt')).catch(error => console.warn(error.message)) const tables = Object.keys(map)
await db.schema.table('albums', t => t.dateTime('zipGeneratedAt')).catch(error => console.warn(error.message)) await Promise.all(tables.map(table => {
await db.schema.table('users', t => t.dateTime('enabled')).catch(error => console.warn(error.message)) const columns = Object.keys(map[table])
await db.schema.table('users', t => t.dateTime('fileLength')).catch(error => console.warn(error.message)) return Promise.all(columns.map(async column => {
if (await db.schema.hasColumn(table, column)) { return }
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)
}))
}))
console.log('Migration finished! Now start lolisafe normally') console.log('Migration finished! Now start lolisafe normally')
process.exit(0) process.exit(0)
} }