From 3764e023fbb988d0e63e0905c080acf669cb59d6 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 16 Aug 2019 15:58:44 +0100 Subject: [PATCH] No-op'd post.page->post.type migrations, added cleanup post.type->post.page migrations --- .../versions/2.28/6-add-type-column.js | 48 +-------- .../versions/2.28/7-populate-type-column.js | 96 +----------------- .../versions/2.28/8-remove-page-column.js | 53 +--------- .../versions/2.29/1-add-post-page-column.js | 48 +++++++++ .../2.29/2-populate-post-page-column.js | 98 +++++++++++++++++++ .../2.29/3-remove-page-type-column.js | 48 +++++++++ core/server/data/schema/schema.js | 2 +- 7 files changed, 209 insertions(+), 184 deletions(-) create mode 100644 core/server/data/migrations/versions/2.29/1-add-post-page-column.js create mode 100644 core/server/data/migrations/versions/2.29/2-populate-post-page-column.js create mode 100644 core/server/data/migrations/versions/2.29/3-remove-page-type-column.js diff --git a/core/server/data/migrations/versions/2.28/6-add-type-column.js b/core/server/data/migrations/versions/2.28/6-add-type-column.js index d525b80b33..12fd425f06 100644 --- a/core/server/data/migrations/versions/2.28/6-add-type-column.js +++ b/core/server/data/migrations/versions/2.28/6-add-type-column.js @@ -1,44 +1,6 @@ -const common = require('../../../../lib/common'); -const commands = require('../../../schema').commands; +const Promise = require('bluebird'); -const createLog = type => msg => common.logging[type](msg); - -function createColumnMigration({table, column, dbIsInCorrectState, operation, operationVerb}) { - return function columnMigrations({transacting}) { - return transacting.schema.hasColumn(table, column) - .then(dbIsInCorrectState) - .then((isInCorrectState) => { - const log = createLog(isInCorrectState ? 'warn' : 'info'); - - log(`${operationVerb} ${table}.${column}`); - - if (!isInCorrectState) { - return operation(table, column, transacting); - } - }); - }; -} - -module.exports.up = createColumnMigration({ - table: 'posts', - column: 'type', - dbIsInCorrectState(columnExists) { - return columnExists === true; - }, - operation: commands.addColumn, - operationVerb: 'Adding' -}); - -module.exports.down = createColumnMigration({ - table: 'posts', - column: 'type', - dbIsInCorrectState(columnExists) { - return columnExists === false; - }, - operation: commands.dropColumn, - operationVerb: 'Removing' -}); - -module.exports.config = { - transaction: true -}; +// adding/removing columns is too slow for a minor release +// noop'd, will be re-introduced in Ghost 3.0 +module.exports.up = () => Promise.resolve(); +module.exports.down = () => Promise.resolve(); diff --git a/core/server/data/migrations/versions/2.28/7-populate-type-column.js b/core/server/data/migrations/versions/2.28/7-populate-type-column.js index b0918574c1..12fd425f06 100644 --- a/core/server/data/migrations/versions/2.28/7-populate-type-column.js +++ b/core/server/data/migrations/versions/2.28/7-populate-type-column.js @@ -1,94 +1,6 @@ const Promise = require('bluebird'); -const toPairs = require('lodash/toPairs'); -const common = require('../../../../lib/common'); -/* - * @param from: object with a SINGLE entry { 'fromColumn': 'fromValue' } - * @param to: object with a SINGLE entry { 'toColumn': 'toValue' } - */ -const createColumnToColumnMap = ({from, to, tableName}) => (connection) => { - return connection.schema.hasTable(tableName) - .then((tableExists) => { - if (!tableExists) { - common.logging.warn( - `Table ${tableName} does not exist` - ); - return; - } - - const [fromColumn, fromValue] = toPairs(from)[0]; - const [toColumn, toValue] = toPairs(to)[0]; - - return Promise.all([ - connection.schema.hasColumn(tableName, fromColumn), - connection.schema.hasColumn(tableName, toColumn) - ]).then(([fromColumnExists, toColumnExists]) => { - if (!fromColumnExists) { - common.logging.warn( - `Table '${tableName}' does not have column '${fromColumn}'` - ); - } - if (!toColumnExists) { - common.logging.warn( - `Table '${tableName}' does not have column '${toColumn}'` - ); - } - if (!fromColumnExists || !toColumnExists) { - return; - } - - common.logging.info( - `Updating ${tableName}, setting "${toColumn}" column to "${toValue}" where "${fromColumn}" column is "${fromValue}"` - ); - - return connection(tableName) - .where(fromColumn, fromValue) - .update(toColumn, toValue); - }); - }); -}; - -const createColumnToColumnMigration = ({tableName, from, to}) => { - return { - up: createColumnToColumnMap({from, to, tableName}), - down: createColumnToColumnMap({from: to, to: from, tableName}) - }; -}; - -const pageColumnToPageType = createColumnToColumnMigration({ - tableName: 'posts', - from: { - page: true - }, - to: { - type: 'page' - } -}); - -const pageColumnToPostType = createColumnToColumnMigration({ - tableName: 'posts', - from: { - page: false - }, - to: { - type: 'post' - } -}); - -module.exports.up = ({transacting}) => { - return Promise.all([ - pageColumnToPageType.up(transacting), - pageColumnToPostType.up(transacting) - ]); -}; - -module.exports.down = ({transacting}) => { - return Promise.all([ - pageColumnToPageType.down(transacting), - pageColumnToPostType.down(transacting) - ]); -}; - -module.exports.config = { - transaction: true -}; +// adding/removing columns is too slow for a minor release +// noop'd, will be re-introduced in Ghost 3.0 +module.exports.up = () => Promise.resolve(); +module.exports.down = () => Promise.resolve(); diff --git a/core/server/data/migrations/versions/2.28/8-remove-page-column.js b/core/server/data/migrations/versions/2.28/8-remove-page-column.js index e6be1701ad..12fd425f06 100644 --- a/core/server/data/migrations/versions/2.28/8-remove-page-column.js +++ b/core/server/data/migrations/versions/2.28/8-remove-page-column.js @@ -1,49 +1,6 @@ -const common = require('../../../../lib/common'); -const commands = require('../../../schema').commands; +const Promise = require('bluebird'); -const createLog = type => msg => common.logging[type](msg); - -function createColumnMigration({table, column, dbIsInCorrectState, operation, operationVerb, columnDefinition}) { - return function columnMigrations({transacting}) { - return transacting.schema.hasColumn(table, column) - .then(dbIsInCorrectState) - .then((isInCorrectState) => { - const log = createLog(isInCorrectState ? 'warn' : 'info'); - - log(`${operationVerb} ${table}.${column}`); - - if (!isInCorrectState) { - return operation(table, column, transacting, columnDefinition); - } - }); - }; -} - -module.exports.up = createColumnMigration({ - table: 'posts', - column: 'page', - dbIsInCorrectState(columnExists) { - return columnExists === false; - }, - operation: commands.dropColumn, - operationVerb: 'Removing' -}); - -module.exports.down = createColumnMigration({ - table: 'posts', - column: 'page', - dbIsInCorrectState(columnExists) { - return columnExists === true; - }, - operation: commands.addColumn, - operationVerb: 'Adding', - columnDefinition: { - type: 'bool', - nullable: false, - defaultTo: false - } -}); - -module.exports.config = { - transaction: true -}; +// adding/removing columns is too slow for a minor release +// noop'd, will be re-introduced in Ghost 3.0 +module.exports.up = () => Promise.resolve(); +module.exports.down = () => Promise.resolve(); diff --git a/core/server/data/migrations/versions/2.29/1-add-post-page-column.js b/core/server/data/migrations/versions/2.29/1-add-post-page-column.js new file mode 100644 index 0000000000..e04d5bd4f8 --- /dev/null +++ b/core/server/data/migrations/versions/2.29/1-add-post-page-column.js @@ -0,0 +1,48 @@ +const common = require('../../../../lib/common'); +const commands = require('../../../schema').commands; + +const createLog = type => msg => common.logging[type](msg); + +function createColumnMigration({table, column, dbIsInCorrectState, operation, operationVerb}) { + return function columnMigrations({transacting}) { + return transacting.schema.hasColumn(table, column) + .then(dbIsInCorrectState) + .then((isInCorrectState) => { + const log = createLog(isInCorrectState ? 'warn' : 'info'); + + log(`${operationVerb} ${table}.${column}`); + + if (!isInCorrectState) { + return operation(table, column, transacting); + } + }); + }; +} + +module.exports.up = function (options) { + return options.transacting.schema.hasColumn('posts', 'type').then((hasTypeColumn) => { + if (!hasTypeColumn) { + // no-op'd post.page->post.type migrations were never run + return Promise.resolve(); + } + + return createColumnMigration({ + table: 'posts', + column: 'page', + dbIsInCorrectState(columnExists) { + return columnExists === true; + }, + operation: commands.addColumn, + operationVerb: 'Adding' + })(options); + }); +}; + +// `up` only runs in order to fix a previous migration so we don't want to do +// anything in `down` because it would put previously-fine sites into the wrong +// state +module.exports.down = () => Promise.resolve(); + +module.exports.config = { + transaction: true +}; diff --git a/core/server/data/migrations/versions/2.29/2-populate-post-page-column.js b/core/server/data/migrations/versions/2.29/2-populate-post-page-column.js new file mode 100644 index 0000000000..802e861edb --- /dev/null +++ b/core/server/data/migrations/versions/2.29/2-populate-post-page-column.js @@ -0,0 +1,98 @@ +const toPairs = require('lodash/toPairs'); +const common = require('../../../../lib/common'); + +/* + * @param from: object with a SINGLE entry { 'fromColumn': 'fromValue' } + * @param to: object with a SINGLE entry { 'toColumn': 'toValue' } + */ +const createColumnToColumnMap = ({from, to, tableName}) => (connection) => { + return connection.schema.hasTable(tableName) + .then((tableExists) => { + if (!tableExists) { + common.logging.warn( + `Table ${tableName} does not exist` + ); + return; + } + + const [fromColumn, fromValue] = toPairs(from)[0]; + const [toColumn, toValue] = toPairs(to)[0]; + + return Promise.all([ + connection.schema.hasColumn(tableName, fromColumn), + connection.schema.hasColumn(tableName, toColumn) + ]).then(([fromColumnExists, toColumnExists]) => { + if (!fromColumnExists) { + common.logging.warn( + `Table '${tableName}' does not have column '${fromColumn}'` + ); + } + if (!toColumnExists) { + common.logging.warn( + `Table '${tableName}' does not have column '${toColumn}'` + ); + } + if (!fromColumnExists || !toColumnExists) { + return; + } + + common.logging.info( + `Updating ${tableName}, setting "${toColumn}" column to "${toValue}" where "${fromColumn}" column is "${fromValue}"` + ); + + return connection(tableName) + .where(fromColumn, fromValue) + .update(toColumn, toValue); + }); + }); +}; + +const createColumnToColumnMigration = ({tableName, from, to}) => { + return { + up: createColumnToColumnMap({from, to, tableName}), + down: createColumnToColumnMap({from: to, to: from, tableName}) + }; +}; + +const typeColumnToPageTrue = createColumnToColumnMigration({ + tableName: 'posts', + from: { + type: 'page' + }, + to: { + page: true + } +}); + +const typeColumnToPageFalse = createColumnToColumnMigration({ + tableName: 'posts', + from: { + type: 'post' + }, + to: { + page: false + } +}); + +module.exports.up = ({transacting}) => { + return transacting.schema.hasColumn('posts', 'type').then((hasTypeColumn) => { + if (!hasTypeColumn) { + // no-op'd post.page->post.type migrations were never run + return Promise.resolve(); + } + + return Promise.all([ + typeColumnToPageTrue.up(transacting), + typeColumnToPageFalse.up(transacting) + ]); + }); +}; + +// `up` only runs in order to fix a previous migration so we don't want to do +// anything in `down` because it would put previously-fine sites into the wrong +// state +module.exports.down = () => Promise.resolve(); + +module.exports.config = { + transaction: true +}; diff --git a/core/server/data/migrations/versions/2.29/3-remove-page-type-column.js b/core/server/data/migrations/versions/2.29/3-remove-page-type-column.js new file mode 100644 index 0000000000..c5cbc3cfad --- /dev/null +++ b/core/server/data/migrations/versions/2.29/3-remove-page-type-column.js @@ -0,0 +1,48 @@ +const common = require('../../../../lib/common'); +const commands = require('../../../schema').commands; + +const createLog = type => msg => common.logging[type](msg); + +function createColumnMigration({table, column, dbIsInCorrectState, operation, operationVerb, columnDefinition}) { + return function columnMigrations({transacting}) { + return transacting.schema.hasColumn(table, column) + .then(dbIsInCorrectState) + .then((isInCorrectState) => { + const log = createLog(isInCorrectState ? 'warn' : 'info'); + + log(`${operationVerb} ${table}.${column}`); + + if (!isInCorrectState) { + return operation(table, column, transacting, columnDefinition); + } + }); + }; +} + +module.exports.up = function (options) { + return options.transacting.schema.hasColumn('posts', 'type').then((hasTypeColumn) => { + if (!hasTypeColumn) { + // no-op'd post.page->post.type migrations were never run + return Promise.resolve(); + } + + return createColumnMigration({ + table: 'posts', + column: 'type', + dbIsInCorrectState(columnExists) { + return columnExists === false; + }, + operation: commands.dropColumn, + operationVerb: 'Removing' + })(options); + }); +}; + +// `up` only runs in order to fix a previous migration so we don't want to do +// anything in `down` because it would put previously-fine sites into the wrong +// state +module.exports.down = () => Promise.resolve(); + +module.exports.config = { + transaction: true +}; diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index 5621db15d9..7f0cf495aa 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -19,7 +19,7 @@ module.exports = { plaintext: {type: 'text', maxlength: 1000000000, fieldtype: 'long', nullable: true}, feature_image: {type: 'string', maxlength: 2000, nullable: true}, featured: {type: 'bool', nullable: false, defaultTo: false}, - type: {type: 'string', maxlength: 50, nullable: false, defaultTo: 'post', validations: {isIn: [['post', 'page']]}}, + page: {type: 'bool', nullable: false, defaultTo: false}, status: {type: 'string', maxlength: 50, nullable: false, defaultTo: 'draft'}, locale: {type: 'string', maxlength: 6, nullable: true}, visibility: {