From 38e673226f89c825f2f091619904aa96d7167afa Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Wed, 29 Jun 2022 14:35:00 +0700 Subject: [PATCH] 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 --- controllers/uploadController.js | 4 ++-- scripts/migrate.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/controllers/uploadController.js b/controllers/uploadController.js index e95fac7..6412844 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -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, diff --git a/scripts/migrate.js b/scripts/migrate.js index 1e5b5e8..4f1a971 100644 --- a/scripts/migrate.js +++ b/scripts/migrate.js @@ -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).`