From b5d1245be1bfbbcb8bde9d16eedb039b511fc892 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 10 Jul 2023 15:42:22 +0800 Subject: [PATCH] Added posts_collections table population refs https://github.com/TryGhost/Ghost/pull/17247 - When the instance is started the posts_collections records should be pre-filled for built-in collections. This is to avoid expensive processing during every instance startup. --- ...-05-16-55-add-built-in-collection-posts.js | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 ghost/core/core/server/data/migrations/versions/5.55/2023-07-10-05-16-55-add-built-in-collection-posts.js diff --git a/ghost/core/core/server/data/migrations/versions/5.55/2023-07-10-05-16-55-add-built-in-collection-posts.js b/ghost/core/core/server/data/migrations/versions/5.55/2023-07-10-05-16-55-add-built-in-collection-posts.js new file mode 100644 index 0000000000..fdd35d2237 --- /dev/null +++ b/ghost/core/core/server/data/migrations/versions/5.55/2023-07-10-05-16-55-add-built-in-collection-posts.js @@ -0,0 +1,99 @@ +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 collections'); + + const existingLatestCollection = await knex('collections') + .where({ + slug: 'latest' + }) + .first(); + + if (!existingLatestCollection) { + logging.warn('Latest collection does not exists, skipping'); + } else { + const latestPostsRows = await knex('posts') + .select('id') + .where({ + type: 'post' + }); + + const latestPostsIds = latestPostsRows.map(row => row.id); + + await insertPostCollections(knex, existingLatestCollection.id, latestPostsIds); + } + + 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('Deleting built in collection_posts'); + + const existingLatestCollection = await knex('collections') + .where({ + slug: 'latest' + }) + .first(); + + if (existingLatestCollection) { + logging.info(`Deleting collection_posts for latest collection: ${existingLatestCollection.id}`); + await knex('collections_posts') + .where({ + collection_id: existingLatestCollection.id + }) + .del(); + } + + const existingFeaturedCollection = await knex('collections') + .where({ + slug: 'featured' + }) + .first(); + + if (existingFeaturedCollection) { + logging.info(`Deleting collection_posts for featured collection: ${existingFeaturedCollection.id}`); + + await knex('collections_posts') + .where({ + collection_id: existingFeaturedCollection.id + }) + .del(); + } + } +);