diff --git a/ghost/core/core/server/data/migrations/versions/5.55/2023-07-07-14-15-32-add-built-in-collections.js b/ghost/core/core/server/data/migrations/versions/5.55/2023-07-07-14-15-32-add-built-in-collections.js new file mode 100644 index 0000000000..1f86b570f3 --- /dev/null +++ b/ghost/core/core/server/data/migrations/versions/5.55/2023-07-07-14-15-32-add-built-in-collections.js @@ -0,0 +1,60 @@ +const logging = require('@tryghost/logging'); +const {default: ObjectID} = require('bson-objectid'); +const {createTransactionalMigration} = require('../../utils'); + +module.exports = createTransactionalMigration( + async function up(knex) { + logging.info('Creating built in collections'); + + const existingIndexCollection = await knex('collections') + .where({ + slug: 'index' + }) + .first(); + + if (existingIndexCollection) { + logging.warn('Index collection already exists, skipping'); + } else { + await knex('collections').insert({ + id: (new ObjectID()).toHexString(), + name: 'Index', + slug: 'index', + description: 'Collection with all posts', + type: 'automatic', + filter: '', + created_at: knex.raw('current_timestamp') + }); + } + + const existingFeaturedCollection = await knex('collections') + .where({ + slug: 'featured' + }) + .first(); + + if (existingFeaturedCollection) { + logging.warn('Index collection already exists, skipping'); + } else { + await knex('collections').insert({ + id: (new ObjectID()).toHexString(), + name: 'Featured', + slug: 'featured', + description: 'Collection of featured posts', + type: 'automatic', + filter: 'featured:true', + created_at: knex.raw('current_timestamp') + }); + } + }, + async function down(knex) { + logging.info('Deleting built in collections'); + + await knex('collections').where({ + slug: 'index' + }).del(); + + await knex('collections').where({ + slug: 'featured' + }).del(); + } +); diff --git a/ghost/core/core/server/data/schema/fixtures/fixtures.json b/ghost/core/core/server/data/schema/fixtures/fixtures.json index 0211ba49fd..c90fe896ae 100644 --- a/ghost/core/core/server/data/schema/fixtures/fixtures.json +++ b/ghost/core/core/server/data/schema/fixtures/fixtures.json @@ -1,5 +1,24 @@ { "models": [ + { + "name": "Collection", + "entries": [ + { + "title": "Index", + "slug": "index", + "description": "Collection with all posts", + "type": "automatic", + "filter": "" + }, + { + "title": "Featured", + "slug": "featured", + "description": "Collection of featured posts", + "type": "automatic", + "filter": "featured:true" + } + ] + }, { "name": "Product", "entries": [ diff --git a/ghost/core/core/server/services/collections/service.js b/ghost/core/core/server/services/collections/service.js index 85be196429..5abb70b349 100644 --- a/ghost/core/core/server/services/collections/service.js +++ b/ghost/core/core/server/services/collections/service.js @@ -33,28 +33,6 @@ class CollectionsServiceWrapper { return; } - const existingBuiltins = await this.api.getAll({filter: 'slug:featured'}); - - if (!existingBuiltins.data.length) { - await this.api.createCollection({ - title: 'Index', - slug: 'index', - description: 'Collection with all posts', - type: 'automatic', - deletable: false, - filter: 'status:published' - }); - - await this.api.createCollection({ - title: 'Featured Posts', - slug: 'featured', - description: 'Collection of featured posts', - type: 'automatic', - deletable: false, - filter: 'featured:true' - }); - } - this.api.subscribeToEvents(); require('./intercept-events')(); } diff --git a/ghost/core/test/unit/server/data/schema/integrity.test.js b/ghost/core/test/unit/server/data/schema/integrity.test.js index eb6dcbaa17..993d09a9c3 100644 --- a/ghost/core/test/unit/server/data/schema/integrity.test.js +++ b/ghost/core/test/unit/server/data/schema/integrity.test.js @@ -36,7 +36,7 @@ const validateRouteSettings = require('../../../../../core/server/services/route describe('DB version integrity', function () { // Only these variables should need updating const currentSchemaHash = '8e299caf33efbcf8a12c9cefaf8f6500'; - const currentFixturesHash = '93c3b3cb8bca34a733634e74ee514172'; + const currentFixturesHash = '03f3d9ddc0a1190e909a85808a591a04'; const currentSettingsHash = '4f23a583335dcb4cb3fae553122ea200'; const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01'; diff --git a/ghost/core/test/utils/fixtures/fixtures.json b/ghost/core/test/utils/fixtures/fixtures.json index 70887da92b..9b1e8ccf6f 100644 --- a/ghost/core/test/utils/fixtures/fixtures.json +++ b/ghost/core/test/utils/fixtures/fixtures.json @@ -1,5 +1,24 @@ { "models": [ + { + "name": "Collection", + "entries": [ + { + "title": "Latest", + "slug": "latest", + "description": "Collection with all posts", + "type": "automatic", + "filter": "" + }, + { + "title": "Featured Posts", + "slug": "featured", + "description": "Collection of featured posts", + "type": "automatic", + "filter": "featured:true" + } + ] + }, { "name": "Product", "entries": [