diff --git a/ghost/admin/package.json b/ghost/admin/package.json index c5f27927c0..cd1c249474 100644 --- a/ghost/admin/package.json +++ b/ghost/admin/package.json @@ -1,6 +1,6 @@ { "name": "ghost-admin", - "version": "5.63.0", + "version": "5.64.0", "description": "Ember.js admin client for Ghost", "author": "Ghost Foundation", "homepage": "http://ghost.org", diff --git a/ghost/core/core/server/data/migrations/versions/5.64/2023-09-19-04-25-40-truncate-stale-built-in-collections-posts.js b/ghost/core/core/server/data/migrations/versions/5.64/2023-09-19-04-25-40-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.64/2023-09-19-04-25-40-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.64/2023-09-19-04-34-10-repopulate-built-in-collection-posts.js b/ghost/core/core/server/data/migrations/versions/5.64/2023-09-19-04-34-10-repopulate-built-in-collection-posts.js new file mode 100644 index 0000000000..6f699083e9 --- /dev/null +++ b/ghost/core/core/server/data/migrations/versions/5.64/2023-09-19-04-34-10-repopulate-built-in-collection-posts.js @@ -0,0 +1,69 @@ +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('Clearing collections_posts table'); + await knex('collections_posts').truncate(); + } +); diff --git a/ghost/core/package.json b/ghost/core/package.json index 543b3d58a3..eb9c4f55d3 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -1,6 +1,6 @@ { "name": "ghost", - "version": "5.63.0", + "version": "5.64.0", "description": "The professional publishing platform", "author": "Ghost Foundation", "homepage": "https://ghost.org",