From 8d0275280280b70f5481206e1efc66f453259ea1 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 22 Sep 2023 14:56:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20Repopulate=20featured?= =?UTF-8?q?=20collection=20records?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Arch/issues/95 refs https://github.com/TryGhost/Ghost/commit/00e5f84d88488a5b915d191093151e7dff208465 - Maintaining "latest" collection's static content is causing performance issues we are not able to overcome in a short period of time. Instead we leave it as a "dynamic" collection, which equals the contents of the Posts API. The "featured" collection will be saved in a "static" form as previously. - This is roughly the same migration as in 5.64 version (see refed commit). It wipes out all of the stored collections data and only populates the featured collection. --- ...uncate-stale-built-in-collections-posts.js | 12 +++++ ...late-built-in-featured-collection-posts.js | 49 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-15-truncate-stale-built-in-collections-posts.js create mode 100644 ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-55-repopulate-built-in-featured-collection-posts.js diff --git a/ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-15-truncate-stale-built-in-collections-posts.js b/ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-15-truncate-stale-built-in-collections-posts.js new file mode 100644 index 0000000000..f59076b22e --- /dev/null +++ b/ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-15-truncate-stale-built-in-collections-posts.js @@ -0,0 +1,12 @@ +const logging = require('@tryghost/logging'); +const {createNonTransactionalMigration} = require('../../utils'); + +module.exports = createNonTransactionalMigration( + async function up(knex) { + logging.info('Clearing collections_posts table'); + await knex('collections_posts').truncate(); + }, + async function down() { + logging.info('Not doing anything - collections_posts table has been truncated'); + } +); diff --git a/ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-55-repopulate-built-in-featured-collection-posts.js b/ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-55-repopulate-built-in-featured-collection-posts.js new file mode 100644 index 0000000000..fc1d59a1cc --- /dev/null +++ b/ghost/core/core/server/data/migrations/versions/5.65/2023-09-22-06-42-55-repopulate-built-in-featured-collection-posts.js @@ -0,0 +1,49 @@ +const logging = require('@tryghost/logging'); +const {default: ObjectID} = require('bson-objectid'); +const {createTransactionalMigration} = require('../../utils'); + +const insertPostCollections = async (knex, collectionId, postIds) => { + logging.warn(`Batch inserting ${postIds.length} collection posts for collection ${collectionId}`); + + const collectionPosts = postIds.map((postId) => { + return { + id: (new ObjectID()).toHexString(), + collection_id: collectionId, + post_id: postId, + sort_order: 0 + }; + }); + + await knex.batchInsert('collections_posts', collectionPosts, 1000); +}; + +module.exports = createTransactionalMigration( + async function up(knex) { + logging.info('Populating built-in featured collection'); + + const existingFeaturedCollection = await knex('collections') + .where({ + slug: 'featured' + }) + .first(); + + if (!existingFeaturedCollection) { + logging.warn('Featured collection does not exist, skipping'); + } else { + const featuredPostsRows = await knex('posts') + .select('id') + .where({ + featured: true, + type: 'post' + }); + + const featuredPostsIds = featuredPostsRows.map(row => row.id); + + await insertPostCollections(knex, existingFeaturedCollection.id, featuredPostsIds); + } + }, + async function down(knex) { + logging.info('Clearing collections_posts table'); + await knex('collections_posts').truncate(); + } +);