0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Repopulated collections_posts built-in collections data

closes https://github.com/TryGhost/Arch/issues/74
refs b5d1245be1

- We have turned off the collections feature flag after a unsuccessful attempt to make collections GA. With the flag turned off, collections_posts data has gone stale and needs repopulation to function properly again.
- This migration is meant to clear the data on collections_posts table and repopulated it again the same way initial migration did in 5.5/2023-07-10-05-16-55-add-built-in-collection-posts.js script.
This commit is contained in:
Naz 2023-08-15 12:52:53 +08:00
parent 826b5be002
commit 00e5f84d88
No known key found for this signature in database
2 changed files with 81 additions and 0 deletions

View file

@ -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');
}
);

View file

@ -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();
}
);