From 74d73313fd14d0455cdf6370d3572058acec9df1 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 11 Dec 2020 15:00:20 -0500 Subject: [PATCH] feat: migration of old topic thumbs to new format closes #9099 --- src/upgrades/1.16.0/migrate_thumbs.js | 40 ++++++++++++++++++++ src/upgrades/1.16.0/topic_thumbs_settings.js | 16 -------- 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 src/upgrades/1.16.0/migrate_thumbs.js delete mode 100644 src/upgrades/1.16.0/topic_thumbs_settings.js diff --git a/src/upgrades/1.16.0/migrate_thumbs.js b/src/upgrades/1.16.0/migrate_thumbs.js new file mode 100644 index 0000000000..b5ebd4c5cc --- /dev/null +++ b/src/upgrades/1.16.0/migrate_thumbs.js @@ -0,0 +1,40 @@ +'use strict'; + +const nconf = require('nconf'); + +const db = require('../../database'); +const meta = require('../../meta'); +const topics = require('../../topics'); +const batch = require('../../batch'); + +module.exports = { + name: 'Migrate existing topic thumbnails to new format', + timestamp: Date.UTC(2020, 11, 11), + method: async function () { + const progress = this.progress; + const current = await meta.configs.get('topicThumbSize'); + + if (parseInt(current, 10) === 120) { + await meta.configs.set('topicThumbSize', 512); + } + await meta.configs.set('allowTopicsThumbnail', 1); + + await batch.processSortedSet('topics:tid', async function (tids) { + const keys = tids.map(tid => `topic:${tid}`); + const topicThumbs = (await db.getObjectsFields(keys, ['thumb'])).map(obj => (obj.thumb ? obj.thumb.replace(nconf.get('upload_url'), '') : null)); + + await Promise.all(tids.map(async (tid, idx) => { + const path = topicThumbs[idx]; + if (path) { + await topics.thumbs.associate({ id: tid, path }); + await db.deleteObjectField(keys[idx], 'thumb'); + } + + progress.incr(); + })); + }, { + batch: 500, + progress: progress, + }); + }, +}; diff --git a/src/upgrades/1.16.0/topic_thumbs_settings.js b/src/upgrades/1.16.0/topic_thumbs_settings.js deleted file mode 100644 index 27fa964780..0000000000 --- a/src/upgrades/1.16.0/topic_thumbs_settings.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -const meta = require('../../meta'); - -module.exports = { - name: 'Config changes for new topic thumbnails', - timestamp: Date.UTC(2020, 11, 8), - method: async () => { - const current = await meta.configs.get('topicThumbSize'); - - if (parseInt(current, 10) === 120) { - await meta.configs.set('topicThumbSize', 512); - } - await meta.configs.set('allowTopicsThumbnail', 1); - }, -};