fix: inconsistent size field in DB !! yarn migrate

we used to store number directly into the string size field, and
during the conversion it seemed to always add ".0" at the final string,
probably because the driver or sqlite3 itself assumes float

please run yarn migrate after pulling this commit
if you skip converting the DB, file duplicates check will fail to
function

and in the future im planning to do size statistics in bigint, which
will also fail if not converted
This commit is contained in:
Bobby Wibowo 2022-06-29 14:35:00 +07:00
parent 9d38c431dc
commit 38e673226f
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 15 additions and 2 deletions

View File

@ -836,7 +836,7 @@ self.storeFilesToDb = async (req, res, user, infoMap) => {
})
.where({
hash: info.data.hash,
size: info.data.size
size: String(info.data.size)
})
// Select expirydate to display expiration date of existing files as well
.select('name', 'expirydate')
@ -861,7 +861,7 @@ self.storeFilesToDb = async (req, res, user, infoMap) => {
name: info.data.filename,
original: info.data.originalname,
type: info.data.mimetype,
size: info.data.size,
size: String(info.data.size),
hash: info.data.hash,
// Only disable if explicitly set to false in config
ip: config.uploads.storeIP !== false ? req.ip : null,

View File

@ -70,6 +70,19 @@ const map = {
})
}
const files = await db.table('files')
.where('size', 'like', '%.0')
if (files.length) {
console.log(`Found ${files.length} files with outdated "size" field, converting\u2026`)
for (const file of files) {
const size = file.size.replace(/\.0$/, '')
await db.table('files')
.update('size', size)
.where('id', file.id)
done++
}
}
let status = 'Database migration was not required.'
if (done) {
status = `Completed ${done} database migration task(s).`