diff --git a/core/server/data/migrations/init/2-create-fixtures.js b/core/server/data/migrations/init/2-create-fixtures.js index 69af6c9b02bc..13f124823f1b 100644 --- a/core/server/data/migrations/init/2-create-fixtures.js +++ b/core/server/data/migrations/init/2-create-fixtures.js @@ -9,7 +9,8 @@ module.exports.config = { module.exports.up = function insertFixtures(options) { var localOptions = _.merge({ - context: {internal: true} + context: {internal: true}, + migrating: true }, options); return Promise.mapSeries(fixtures.models, function (model) { diff --git a/core/server/data/migrations/versions/1.25/1-update-koenig-beta-html.js b/core/server/data/migrations/versions/1.25/1-update-koenig-beta-html.js index 814b0b12b8cf..4aec09965d7b 100644 --- a/core/server/data/migrations/versions/1.25/1-update-koenig-beta-html.js +++ b/core/server/data/migrations/versions/1.25/1-update-koenig-beta-html.js @@ -1,7 +1,10 @@ const _ = require('lodash'); +const Promise = require('bluebird'); const common = require('../../../../lib/common'); const converters = require('../../../../lib/mobiledoc/converters'); const models = require('../../../../models'); +const message1 = 'Migrating Koenig beta post\'s mobiledoc/HTML to 2.0 format'; +const message2 = 'Migrated Koenig beta post\'s mobiledoc/HTML to 2.0 format'; const mobiledocIsCompatibleWithV1 = function mobiledocIsCompatibleWithV1(doc) { if (doc @@ -19,6 +22,10 @@ const mobiledocIsCompatibleWithV1 = function mobiledocIsCompatibleWithV1(doc) { return false; }; +module.exports.config = { + transaction: true +}; + module.exports.up = function regenerateKoenigBetaHTML(options) { let postAllColumns = ['id', 'html', 'mobiledoc']; @@ -26,7 +33,7 @@ module.exports.up = function regenerateKoenigBetaHTML(options) { context: {internal: true} }, options); - common.logging.info('Migrating Koenig beta post\'s mobiledoc/HTML to 2.0 format'); + common.logging.info(message1); return models.Post.findAll(_.merge({columns: postAllColumns}, localOptions)) .then(function (posts) { @@ -55,5 +62,8 @@ module.exports.up = function regenerateKoenigBetaHTML(options) { }, _.merge({id: post.id}, localOptions)); } }, {concurrency: 100}); + }) + .then(() => { + common.logging.info(message2); }); }; diff --git a/core/server/data/migrations/versions/1.25/2-demo-post.js b/core/server/data/migrations/versions/1.25/2-demo-post.js new file mode 100644 index 000000000000..b510356bf02d --- /dev/null +++ b/core/server/data/migrations/versions/1.25/2-demo-post.js @@ -0,0 +1,55 @@ +const _ = require('lodash'); +const common = require('../../../../lib/common'); +const models = require('../../../../models'); +const fixtures = require('../../../../data/schema/fixtures'); +const message1 = 'Adding demo post.'; +const message2 = 'Added demo post.'; +const message3 = 'Skipped: Adding demo post. Slug already in use.'; + +module.exports.config = { + transaction: true +}; + +module.exports.up = (options) => { + let localOptions = _.merge({ + context: {internal: true}, + columns: ['id'] + }, options); + + let userId; + + common.logging.info(message1); + + const demoPost = _.cloneDeep(fixtures.models[5].entries[0]); + + return models.Post.findOne({slug: demoPost.slug, status: 'all'}, localOptions) + .then((model) => { + if (model) { + common.logging.warn(message3); + return; + } + + return models.User.findOne({id: fixtures.models[4].entries[1].id}, localOptions) + .then((ghostAuthor) => { + if (ghostAuthor) { + userId = ghostAuthor.id; + return; + } + + return models.User.getOwnerUser(localOptions); + }) + .then((ownerUser) => { + if (!userId) { + userId = ownerUser.id; + } + + demoPost.created_by = userId; + demoPost.author_id = userId; + + return models.Post.add(demoPost, localOptions); + }) + .then(() => { + common.logging.info(message2); + }); + }); +}; diff --git a/core/server/data/schema/fixtures/fixtures.json b/core/server/data/schema/fixtures/fixtures.json index d281f858d8fa..2549a46a7510 100644 --- a/core/server/data/schema/fixtures/fixtures.json +++ b/core/server/data/schema/fixtures/fixtures.json @@ -358,6 +358,20 @@ { "name": "Post", "entries": [ + { + "title": "Koenig Demo Post", + "slug": "v2-demo-post", + "mobiledoc": "{\"version\":\"0.3.1\",\"atoms\":[],\"cards\":[[\"hr\",{}],[\"embed\",{\"url\":\"https://twitter.com/TryGhost/status/761119175192420352\",\"html\":\"

Fun announcement coming this afternoon šŸ™ˆ what could it be?

— Ghost (@TryGhost) August 4, 2016
\\n\\n\",\"type\":\"rich\"}],[\"image\",{\"src\":\"https://casper.ghost.org/v1.25.0/images/koenig-demo-1.jpg\",\"alt\":\"\",\"caption\":\"A regular size image\"}],[\"image\",{\"src\":\"https://casper.ghost.org/v1.25.0/images/koenig-demo-2.jpg\",\"alt\":\"\",\"cardWidth\":\"full\",\"caption\":\"It's wide\"}],[\"image\",{\"src\":\"https://casper.ghost.org/v1.25.0/images/koenig-demo-3.jpg\",\"alt\":\"\",\"cardWidth\":\"wide\",\"caption\":\"It's wider, but not widest\"}],[\"markdown\",{\"markdown\":\"Markdown content works just the way it always did, **simply** and *beautifully*.\"}],[\"code\",{\"code\":\".new-editor {\\n\\tdisplay: bock;\\n}\"}],[\"embed\",{\"url\":\"https://www.youtube.com/watch?v=CfeQTuGyiqU\",\"html\":\"\",\"type\":\"video\"}],[\"html\",{\"html\":\"
\\n hello world\\n
\"}]],\"markups\":[[\"strong\"],[\"code\"],[\"em\"],[\"a\",[\"href\",\"https://forum.ghost.org/t/ghost-2-0-theme-compatibility-help-support/2103\"]]],\"sections\":[[1,\"p\",[[0,[],0,\"Hey there! Welcome to the new Ghost editor - affectionately known as \"],[0,[0],1,\"Koenig\"],[0,[],0,\".\"]]],[1,\"p\",[[0,[],0,\"Koenig is a brand new writing experience within Ghost, and follows more of a rich writing experience which you've come to expect from the best publishing platforms. Don't worry though! You can still use Markdown too, if that's what you prefer.\"]]],[1,\"p\",[[0,[],0,\"Because there are some changes to how Ghost outputs content using its new editor, we dropped this draft post into your latest update to tell you a bit about it ā€“ and simultaneously give you a chance to preview how well your theme handles these changes. So after reading this post you should both understand how everything works, and also be able to see if there are any changes you need to make to your theme in order to upgrade to Ghost 2.0.\"]]],[10,0],[1,\"h1\",[[0,[],0,\"What's new\"]]],[1,\"p\",[[0,[],0,\"The new editor is designed to allow you have a more rich editing experience, so it's no longer limited to just text and formatting options ā€“ but it can also handle rich media objects, called cards. You can insert a card either by clicking on the \"],[0,[1],1,\"+\"],[0,[],0,\" button on a new line, or typing \"],[0,[1],1,\"/\"],[0,[],0,\" on a new line to search for a particular card. \"]]],[1,\"p\",[[0,[],0,\"Here's one now:\"]]],[10,1],[1,\"p\",[[0,[],0,\"Cards are rich objects which contain content which is more than just text. To start with there are cards for things like images, markdown, html and embeds ā€” but over time we'll introduce more cards and integrations, as well as allowing you to create your own!\"]]],[1,\"h2\",[[0,[],0,\"Some examples of possible future cards\"]]],[3,\"ul\",[[[0,[],0,\"A chart card to display dynamic data visualisations\"]],[[0,[],0,\"A recipe card to show a pre-formatted list of ingredients and instructions\"]],[[0,[],0,\"A Mailchimp card to capture new subscribers with a web form\"]],[[0,[],0,\"A recommended reading card to display a dynamic suggested story based on the current user's reading history\"]]]],[1,\"p\",[[0,[],0,\"For now, though, we're just getting started with the basics.\"]]],[1,\"h1\",[[0,[],0,\"New ways to work with images\"]]],[1,\"p\",[[0,[],0,\"Perhaps the most notable change to how you're used to interacting with Ghost is in the images. In Koenig, they're both more powerful and easier to work with in the editor itself - and in the theme, they're output slightly differently with different size options.\"]]],[1,\"p\",[[0,[],0,\"For instance, here's your plain ol' regular image:\"]]],[10,2],[1,\"p\",[[0,[],0,\"But perhaps you've got a striking panorama that you really want to stand out as your readers scroll down the page. In that case, you could use the new full-bleed image size which stretches right out to the edges of the screen:\"]]],[10,3],[1,\"p\",[[0,[],0,\"Or maybe you're looking for something in between, which will give you just a little more size to break up the vertical rhythm of the post without dominating the entire screen. If that's the case, you might like the breakout size:\"]]],[10,4],[1,\"p\",[[0,[],0,\"Each of these sizes can be selected from within the editor, and each will output a number of HTML classes for the theme to do styling with. \"]]],[1,\"p\",[[0,[],0,\"Chances are your theme will need a few small updates to take advantage of the new editor functionality. Some people might also find they need to tweak their theme layout, as the editor canvas previously output a wrapper div around its content ā€“ but no longer does. If you rely on that div for styling, you can always add it back again in your theme.\"]]],[1,\"p\",[[0,[],0,\"Oh, we have some nice new image captions, too :)\"]]],[1,\"h1\",[[0,[],0,\"What else?\"]]],[1,\"p\",[[0,[],0,\"Well, you can still write Markdown, as mentioned. In fact you'll find the entire previous Ghost editor \"],[0,[2],1,\"inside\"],[0,[],0,\" this editor. If you want to use it then just go ahead and add a Markdown card and start writing like nothing changed at all:\"]]],[10,5],[1,\"p\",[[0,[],0,\"of course you can embed code blocks\"]]],[10,6],[1,\"p\",[[0,[],0,\"or embed things from external services like YouTube...\"]]],[10,7],[1,\"p\",[[0,[],0,\"and yeah you can do full HTML if you need to, as well!\"]]],[10,8],[1,\"p\",[[0,[],0,\"So everything works, hopefully, just about how you would expect. It's like the old editor, but faster, cleaner, prettier, and a whole lot more powerful.\"]]],[1,\"h1\",[[0,[],0,\"What do I do with this information?\"]]],[1,\"p\",[[0,[],0,\"Preview this post on your site to see if it causes any issues with your theme. Click on the settings cog in the top right šŸ‘‰šŸ¼ corner of the editor, then click on '\"],[0,[0],1,\"Preview\"],[0,[],0,\"' next to the 'Post URL' input.\"]]],[1,\"p\",[[0,[],0,\"If everything looks good to you then there's nothing you need to do, you're all set! If you spot any issues with your design, or there are some funky display issues, then you might need to make some updates to your theme based on the new editor classes being output.\"]]],[1,\"p\",[[0,[],0,\"Head over to the \"],[0,[3],1,\"Ghost 2.0 Theme Compatibility\"],[0,[],0,\" forum topic to discuss any changes and get help if needed.\"]]],[1,\"p\",[[0,[],0,\"That's it!\"]]],[1,\"p\",[[0,[],0,\"We're looking forward to sharing more about the new editor soon\"]]]]}","plaintext":"Hey there! Welcome to the new Ghost editor - affectionately known as Koenig.\n\nKoenig is a brand new writing experience within Ghost, and follows more of a\nrich writing experience which you've come to expect from the best publishing\nplatforms. Don't worry though! You can still use Markdown too, if that's what\nyou prefer.\n\nBecause there are some changes to how Ghost outputs content using its new\neditor, we dropped this draft post into your latest update to tell you a bit\nabout it ā€“ and simultaneously give you a chance to preview how well your theme\nhandles these changes. So after reading this post you should both understand how\neverything works, and also be able to see if there are any changes you need to\nmake to your theme in order to upgrade to Ghost 2.0.\n\n\n--------------------------------------------------------------------------------\n\nWhat's new\nThe new editor is designed to allow you have a more rich editing experience, so\nit's no longer limited to just text and formatting options ā€“ but it can also\nhandle rich media objects, called cards. You can insert a card either by\nclicking on the + button on a new line, or typing / on a new line to search\nfor a particular card. \n\nHere's one now:\n\nFun announcement coming this afternoon šŸ™ˆ what could it be?\n\nā€” Ghost (@TryGhost) August 4, 2016\n[https://twitter.com/TryGhost/status/761119175192420352?ref_src=twsrc%5Etfw]\nCards are rich objects which contain content which is more than just text. To\nstart with there are cards for things like images, markdown, html and embeds ā€”\nbut over time we'll introduce more cards and integrations, as well as allowing\nyou to create your own!\n\nSome examples of possible future cards\n * A chart card to display dynamic data visualisations\n * A recipe card to show a pre-formatted list of ingredients and instructions\n * A Mailchimp card to capture new subscribers with a web form\n * A recommended reading card to display a dynamic suggested story based on the\n current user's reading history\n\nFor now, though, we're just getting started with the basics.\n\nNew ways to work with images\nPerhaps the most notable change to how you're used to interacting with Ghost is\nin the images. In Koenig, they're both more powerful and easier to work with in\nthe editor itself - and in the theme, they're output slightly differently with\ndifferent size options.\n\nFor instance, here's your plain ol' regular image:\n\nA regular size imageBut perhaps you've got a striking panorama that you really\nwant to stand out as your readers scroll down the page. In that case, you could\nuse the new full-bleed image size which stretches right out to the edges of the\nscreen:\n\nIt's wideOr maybe you're looking for something in between, which will give you\njust a little more size to break up the vertical rhythm of the post without\ndominating the entire screen. If that's the case, you might like the breakout\nsize:\n\nIt's wider, but not widestEach of these sizes can be selected from within the\neditor, and each will output a number of HTML classes for the theme to do\nstyling with. \n\nChances are your theme will need a few small updates to take advantage of the\nnew editor functionality. Some people might also find they need to tweak their\ntheme layout, as the editor canvas previously output a wrapper div around its\ncontent ā€“ but no longer does. If you rely on that div for styling, you can\nalways add it back again in your theme.\n\nOh, we have some nice new image captions, too :)\n\nWhat else?\nWell, you can still write Markdown, as mentioned. In fact you'll find the entire\nprevious Ghost editor inside this editor. If you want to use it then just go\nahead and add a Markdown card and start writing like nothing changed at all:\n\nMarkdown content works just the way it always did, simply and beautifully.\n\nof course you can embed code blocks\n\n.new-editor {\n\tdisplay: bock;\n}\n\nor embed things from external services like YouTube...\n\nand yeah you can do full HTML if you need to, as well!\n\nhello worldSo everything works, hopefully, just about how you would expect. It's\nlike the old editor, but faster, cleaner, prettier, and a whole lot more\npowerful.\n\nWhat do I do with this information?\nPreview this post on your site to see if it causes any issues with your theme.\nClick on the settings cog in the top right šŸ‘‰šŸ¼ corner of the editor, then click\non 'Preview' next to the 'Post URL' input.\n\nIf everything looks good to you then there's nothing you need to do, you're all\nset! If you spot any issues with your design, or there are some funky display\nissues, then you might need to make some updates to your theme based on the new\neditor classes being output.\n\nHead over to the Ghost 2.0 Theme Compatibility\n[https://forum.ghost.org/t/ghost-2-0-theme-compatibility-help-support/2103] \nforum topic to discuss any changes and get help if needed.\n\nThat's it!\n\nWe're looking forward to sharing more about the new editor soon", + "feature_image": "", + "featured": false, + "page": false, + "status": "draft", + "meta_title": null, + "meta_description": null, + "created_by": "5951f5fca366002ebd5dbef7", + "published_by": null, + "author_id": "5951f5fca366002ebd5dbef7" + }, { "title": "Setting up your own Ghost theme", "slug": "themes", diff --git a/core/server/data/schema/fixtures/utils.js b/core/server/data/schema/fixtures/utils.js index 3a2ff4a63145..0fc4fb8fc54c 100644 --- a/core/server/data/schema/fixtures/utils.js +++ b/core/server/data/schema/fixtures/utils.js @@ -98,7 +98,7 @@ fetchRelationData = function fetchRelationData(relation, options) { * @param {{name, entries}} modelFixture * @returns {Promise.<*>} */ -addFixturesForModel = function addFixturesForModel(modelFixture, options) { +addFixturesForModel = function addFixturesForModel(modelFixture, options = {}) { // Clone the fixtures as they get changed in this function. // The initial blog posts will be added a `published_at` property, which // would change the fixturesHash. @@ -115,8 +115,22 @@ addFixturesForModel = function addFixturesForModel(modelFixture, options) { } return Promise.mapSeries(modelFixture.entries, function (entry) { + let data = {}; + // CASE: if id is specified, only query by id - return models[modelFixture.name].findOne(entry.id ? {id: entry.id} : entry, options).then(function (found) { + if (entry.id) { + data.id = entry.id; + } else if (entry.slug) { + data.slug = entry.slug; + } else { + data = _.cloneDeep(entry); + } + + if (modelFixture.name === 'Post') { + data.status = 'all'; + } + + return models[modelFixture.name].findOne(data, options).then(function (found) { if (!found) { return models[modelFixture.name].add(entry, options); } diff --git a/core/server/models/base/index.js b/core/server/models/base/index.js index e0b7f8975a4c..930e825d05ba 100644 --- a/core/server/models/base/index.js +++ b/core/server/models/base/index.js @@ -49,7 +49,7 @@ ghostBookshelf.plugin(plugins.collision); // Manages nested updates (relationships) ghostBookshelf.plugin('bookshelf-relations', { - allowedOptions: ['context', 'importing'], + allowedOptions: ['context', 'importing', 'migrating'], unsetRelations: true, hooks: { belongsToMany: { @@ -124,10 +124,10 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({ if (!model.ghostEvents) { model.ghostEvents = []; - // CASE: when importing or deleting content, lot's of model queries are happening in one transaction + // CASE: when importing, deleting or migrating content, lot's of model queries are happening in one transaction // lot's of model events will be triggered. we ensure we set the max listeners to infinity. // we are using `once` - we auto remove the listener afterwards - if (options.importing || options.destroyAll) { + if (options.importing || options.destroyAll || options.migrating) { options.transacting.setMaxListeners(0); } @@ -501,7 +501,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({ } // terms to whitelist for all methods. - return ['context', 'withRelated', 'transacting', 'importing', 'forUpdate']; + return ['context', 'withRelated', 'transacting', 'importing', 'forUpdate', 'migrating']; }, /** diff --git a/core/server/models/post.js b/core/server/models/post.js index 013c7056d88f..3e35fabddd9b 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -327,7 +327,7 @@ Post = ghostBookshelf.Model.extend({ } else { // In any other case (except import), `published_by` should not be changed if (this.hasChanged('published_by') && !options.importing) { - this.set('published_by', this.previous('published_by')); + this.set('published_by', this.previous('published_by') || null); } } diff --git a/core/test/functional/routes/api/posts_spec.js b/core/test/functional/routes/api/posts_spec.js index 86234ac6741f..486a331abeb2 100644 --- a/core/test/functional/routes/api/posts_spec.js +++ b/core/test/functional/routes/api/posts_spec.js @@ -291,7 +291,7 @@ describe('Post API', function () { var jsonResponse = res.body; should.exist(jsonResponse.posts); testUtils.API.checkResponse(jsonResponse, 'posts'); - jsonResponse.posts.should.have.length(1); + jsonResponse.posts.should.have.length(2); testUtils.API.checkResponse(jsonResponse.posts[0], 'post'); testUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination'); done(); diff --git a/core/test/unit/data/schema/fixtures/utils_spec.js b/core/test/unit/data/schema/fixtures/utils_spec.js index 763c822939f0..2150915a7aa9 100644 --- a/core/test/unit/data/schema/fixtures/utils_spec.js +++ b/core/test/unit/data/schema/fixtures/utils_spec.js @@ -106,11 +106,11 @@ describe('Migration Fixture Utils', function () { fixtureUtils.addFixturesForModel(fixtures.models[5]).then(function (result) { should.exist(result); result.should.be.an.Object(); - result.should.have.property('expected', 7); - result.should.have.property('done', 7); + result.should.have.property('expected', 8); + result.should.have.property('done', 8); - postOneStub.callCount.should.eql(7); - postAddStub.callCount.should.eql(7); + postOneStub.callCount.should.eql(8); + postAddStub.callCount.should.eql(8); done(); }).catch(done); @@ -123,10 +123,10 @@ describe('Migration Fixture Utils', function () { fixtureUtils.addFixturesForModel(fixtures.models[5]).then(function (result) { should.exist(result); result.should.be.an.Object(); - result.should.have.property('expected', 7); + result.should.have.property('expected', 8); result.should.have.property('done', 0); - postOneStub.callCount.should.eql(7); + postOneStub.callCount.should.eql(8); postAddStub.callCount.should.eql(0); done(); diff --git a/core/test/unit/data/schema/integrity_spec.js b/core/test/unit/data/schema/integrity_spec.js index 841a34cc465e..6c209fc4a029 100644 --- a/core/test/unit/data/schema/integrity_spec.js +++ b/core/test/unit/data/schema/integrity_spec.js @@ -20,7 +20,7 @@ var should = require('should'), describe('DB version integrity', function () { // Only these variables should need updating var currentSchemaHash = '2073bee126f6e419ef86196f719caea6', - currentFixturesHash = 'a7633cca0c3f73e2358aac8bb56c08db'; + currentFixturesHash = 'e4e64e97d509c61df818bf4d8e46c4c2'; // If this test is failing, then it is likely a change has been made that requires a DB version bump, // and the values above will need updating as confirmation