Skip to content

Commit

Permalink
馃彈 Migrated posts.page column to posts.type (#11111)
Browse files Browse the repository at this point in the history
refs #10922

- adds migrations to...
  1. add `post.type` column
  2. populate `post.type` column based on `post.page` value
  3. drop `post.page` column
- updates all code paths to work with `post.type` in place of `post.page`
- adds `nql-map-key-values` transformer for mapping `page`->`type` in `filter` params when using the v2 API
- modifies importer to handle `post.page`->`post.type` transformation when importing older export files
  • Loading branch information
kevinansfield committed Sep 16, 2019
1 parent d2bd397 commit e57e19e
Show file tree
Hide file tree
Showing 52 changed files with 628 additions and 212 deletions.
19 changes: 18 additions & 1 deletion core/frontend/services/url/UrlGenerator.js
Expand Up @@ -23,6 +23,8 @@ const _ = require('lodash'),
replacement: 'primary_author.slug'
}];

const mapNQLKeyValues = require('../../../shared/nql-map-key-values');

/**
* The UrlGenerator class is responsible to generate urls based on a router's conditions.
* It is the component which sits between routers and resources and connects them together.
Expand All @@ -44,7 +46,22 @@ class UrlGenerator {
// CASE: routers can define custom filters, but not required.
if (this.router.getFilter()) {
this.filter = this.router.getFilter();
this.nql = nql(this.filter, {expansions: EXPANSIONS});
this.nql = nql(this.filter, {
expansions: EXPANSIONS,
transformer: mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
})
});
debug('filter', this.filter);
}

Expand Down
4 changes: 2 additions & 2 deletions core/frontend/services/url/configs/canary.js
Expand Up @@ -8,7 +8,7 @@ module.exports = [
type: 'posts',
modelOptions: {
modelName: 'Post',
filter: 'visibility:public+status:published+page:false',
filter: 'visibility:public+status:published+type:post',
exclude: [
'title',
'mobiledoc',
Expand Down Expand Up @@ -79,7 +79,7 @@ module.exports = [
'primary_tag',
'primary_author'
],
filter: 'visibility:public+status:published+page:true'
filter: 'visibility:public+status:published+type:page'
},
events: {
add: 'page.published',
Expand Down
4 changes: 2 additions & 2 deletions core/frontend/services/url/configs/v2.js
Expand Up @@ -8,7 +8,7 @@ module.exports = [
type: 'posts',
modelOptions: {
modelName: 'Post',
filter: 'visibility:public+status:published+page:false',
filter: 'visibility:public+status:published+type:post',
exclude: [
'title',
'mobiledoc',
Expand Down Expand Up @@ -79,7 +79,7 @@ module.exports = [
'primary_tag',
'primary_author'
],
filter: 'visibility:public+status:published+page:true'
filter: 'visibility:public+status:published+type:page'
},
events: {
add: 'page.published',
Expand Down
4 changes: 2 additions & 2 deletions core/frontend/services/url/configs/v3.js
Expand Up @@ -8,7 +8,7 @@ module.exports = [
type: 'posts',
modelOptions: {
modelName: 'Post',
filter: 'visibility:public+status:published+page:false',
filter: 'visibility:public+status:published+type:post',
exclude: [
'title',
'mobiledoc',
Expand Down Expand Up @@ -79,7 +79,7 @@ module.exports = [
'primary_tag',
'primary_author'
],
filter: 'visibility:public+status:published+page:true'
filter: 'visibility:public+status:published+type:page'
},
events: {
add: 'page.published',
Expand Down
25 changes: 21 additions & 4 deletions core/server/api/canary/utils/serializers/input/pages.js
@@ -1,10 +1,25 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:pages');
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const converters = require('../../../../../lib/mobiledoc/converters');
const url = require('./utils/url');
const localUtils = require('../../index');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;

const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});

function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
Expand Down Expand Up @@ -62,9 +77,9 @@ function handlePostsMeta(frame) {
*/
const forcePageFilter = (frame) => {
if (frame.options.filter) {
frame.options.filter = `(${frame.options.filter})+page:true`;
frame.options.filter = `(${frame.options.filter})+type:page`;
} else {
frame.options.filter = 'page:true';
frame.options.filter = 'type:page';
}
};

Expand Down Expand Up @@ -93,6 +108,8 @@ module.exports = {
defaultRelations(frame);
}

frame.options.mongoTransformer = replacePageWithType;

debug(frame.options);
},

Expand Down Expand Up @@ -130,7 +147,7 @@ module.exports = {

// @NOTE: force storing page
if (options.add) {
frame.data.pages[0].page = true;
frame.data.pages[0].type = 'page';
}

// CASE: Transform short to long format
Expand Down Expand Up @@ -172,7 +189,7 @@ module.exports = {
destroy(apiConfig, frame) {
frame.options.destroyBy = {
id: frame.options.id,
page: true
type: 'page'
};

defaultFormat(frame);
Expand Down
25 changes: 21 additions & 4 deletions core/server/api/canary/utils/serializers/input/posts.js
@@ -1,11 +1,26 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:posts');
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const url = require('./utils/url');
const localUtils = require('../../index');
const labs = require('../../../../../services/labs');
const converters = require('../../../../../lib/mobiledoc/converters');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;

const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});

function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
Expand Down Expand Up @@ -71,9 +86,9 @@ function handlePostsMeta(frame) {
*/
const forcePageFilter = (frame) => {
if (frame.options.filter) {
frame.options.filter = `(${frame.options.filter})+page:false`;
frame.options.filter = `(${frame.options.filter})+type:post`;
} else {
frame.options.filter = 'page:false';
frame.options.filter = 'type:post';
}
};

Expand Down Expand Up @@ -115,6 +130,8 @@ module.exports = {
defaultRelations(frame);
}

frame.options.mongoTransformer = replacePageWithType;

debug(frame.options);
},

Expand Down Expand Up @@ -165,7 +182,7 @@ module.exports = {

// @NOTE: force adding post
if (options.add) {
frame.data.posts[0].page = false;
frame.data.posts[0].type = 'post';
}

// CASE: Transform short to long format
Expand Down Expand Up @@ -205,7 +222,7 @@ module.exports = {
destroy(apiConfig, frame) {
frame.options.destroyBy = {
id: frame.options.id,
page: false
type: 'post'
};

defaultFormat(frame);
Expand Down
Expand Up @@ -108,6 +108,7 @@ const post = (attrs, frame) => {
delete attrs.locale;
delete attrs.visibility;
delete attrs.author;
delete attrs.type;

return attrs;
};
Expand Down
Expand Up @@ -36,6 +36,10 @@ const mapPost = (model, frame) => {
url.forPost(model.id, jsonModel, frame);

if (utils.isContentAPI(frame)) {
// Content api v2 still expects page prop
if (jsonModel.type === 'page') {
jsonModel.page = true;
}
date.forPost(jsonModel);
members.forPost(jsonModel, frame);
}
Expand Down
25 changes: 21 additions & 4 deletions core/server/api/v2/utils/serializers/input/pages.js
@@ -1,10 +1,25 @@
const _ = require('lodash');
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:pages');
const converters = require('../../../../../lib/mobiledoc/converters');
const url = require('./utils/url');
const localUtils = require('../../index');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;

const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});

function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
Expand Down Expand Up @@ -62,9 +77,9 @@ function handlePostsMeta(frame) {
*/
const forcePageFilter = (frame) => {
if (frame.options.filter) {
frame.options.filter = `(${frame.options.filter})+page:true`;
frame.options.filter = `(${frame.options.filter})+type:page`;
} else {
frame.options.filter = 'page:true';
frame.options.filter = 'type:page';
}
};

Expand Down Expand Up @@ -93,6 +108,8 @@ module.exports = {
defaultRelations(frame);
}

frame.options.mongoTransformer = replacePageWithType;

debug(frame.options);
},

Expand Down Expand Up @@ -130,7 +147,7 @@ module.exports = {

// @NOTE: force storing page
if (options.add) {
frame.data.pages[0].page = true;
frame.data.pages[0].type = 'page';
}

// CASE: Transform short to long format
Expand Down Expand Up @@ -172,7 +189,7 @@ module.exports = {
destroy(apiConfig, frame) {
frame.options.destroyBy = {
id: frame.options.id,
page: true
type: 'page'
};

defaultFormat(frame);
Expand Down
25 changes: 21 additions & 4 deletions core/server/api/v2/utils/serializers/input/posts.js
@@ -1,11 +1,26 @@
const _ = require('lodash');
const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:posts');
const url = require('./utils/url');
const localUtils = require('../../index');
const labs = require('../../../../../services/labs');
const converters = require('../../../../../lib/mobiledoc/converters');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;

const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});

function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
Expand Down Expand Up @@ -71,9 +86,9 @@ function handlePostsMeta(frame) {
*/
const forcePageFilter = (frame) => {
if (frame.options.filter) {
frame.options.filter = `(${frame.options.filter})+page:false`;
frame.options.filter = `(${frame.options.filter})+type:post`;
} else {
frame.options.filter = 'page:false';
frame.options.filter = 'type:post';
}
};

Expand Down Expand Up @@ -115,6 +130,8 @@ module.exports = {
defaultRelations(frame);
}

frame.options.mongoTransformer = replacePageWithType;

debug(frame.options);
},

Expand Down Expand Up @@ -165,7 +182,7 @@ module.exports = {

// @NOTE: force adding post
if (options.add) {
frame.data.posts[0].page = false;
frame.data.posts[0].type = 'post';
}

// CASE: Transform short to long format
Expand Down Expand Up @@ -205,7 +222,7 @@ module.exports = {
destroy(apiConfig, frame) {
frame.options.destroyBy = {
id: frame.options.id,
page: false
type: 'post'
};

defaultFormat(frame);
Expand Down
1 change: 1 addition & 0 deletions core/server/api/v2/utils/serializers/output/utils/clean.js
Expand Up @@ -108,6 +108,7 @@ const post = (attrs, frame) => {
delete attrs.locale;
delete attrs.visibility;
delete attrs.author;
delete attrs.type;

return attrs;
};
Expand Down
4 changes: 4 additions & 0 deletions core/server/api/v2/utils/serializers/output/utils/mapper.js
Expand Up @@ -36,6 +36,10 @@ const mapPost = (model, frame) => {
url.forPost(model.id, jsonModel, frame);

if (utils.isContentAPI(frame)) {
// Content api v2 still expects page prop
if (jsonModel.type === 'page') {
jsonModel.page = true;
}
date.forPost(jsonModel);
members.forPost(jsonModel, frame);
}
Expand Down

0 comments on commit e57e19e

Please sign in to comment.