Fixed created_at newsletter migration (#14568)

refs https://github.com/TryGhost/Team/issues/1551

- Updated existing migration to insert a nullable created_at column
- Added a migration to update all the created_at values to now
- Added a migration to drop nullable
- Also includes new helper methods to set and drop nullable
This commit is contained in:
Simon Backx 2022-04-26 11:22:07 +02:00 committed by Matt Hanley
parent 5f31f52139
commit ce5f55e43f
5 changed files with 77 additions and 1 deletions

View File

@ -440,6 +440,44 @@ function createDropColumnMigration(table, column, columnDefinition) {
);
}
/**
* @param {string} table
* @param {string} column
*
* @returns {Migration}
*/
function createSetNullableMigration(table, column) {
return createNonTransactionalMigration(
async function up(knex) {
logging.info(`Setting nullable: ${table}.${column}`);
await commands.setNullable(table, column, knex);
},
async function down(knex) {
logging.info(`Dropping nullable: ${table}.${column}`);
await commands.dropNullable(table, column, knex);
}
);
}
/**
* @param {string} table
* @param {string} column
*
* @returns {Migration}
*/
function createDropNullableMigration(table, column) {
return createNonTransactionalMigration(
async function up(knex) {
logging.info(`Dropping nullable: ${table}.${column}`);
await commands.dropNullable(table, column, knex);
},
async function down(knex) {
logging.info(`Setting nullable: ${table}.${column}`);
await commands.setNullable(table, column, knex);
}
);
}
/**
* Creates a migration which will insert a new setting in settings table
* @param {object} settingSpec - setting key, value, group and type
@ -505,6 +543,8 @@ module.exports = {
combineNonTransactionalMigrations,
createAddColumnMigration,
createDropColumnMigration,
createSetNullableMigration,
createDropNullableMigration,
meta: {
MIGRATION_USER
}

View File

@ -2,5 +2,5 @@ const {createAddColumnMigration} = require('../../utils');
module.exports = createAddColumnMigration('newsletters', 'created_at', {
type: 'dateTime',
nullable: false
nullable: true
});

View File

@ -0,0 +1,19 @@
const logging = require('@tryghost/logging');
const {createTransactionalMigration} = require('../../utils');
module.exports = createTransactionalMigration(
async function up(knex) {
logging.info('Setting missing created_at values for existing newsletters');
const now = knex.raw('CURRENT_TIMESTAMP');
const updatedRows = await knex('newsletters')
.where('created_at', null)
.update('created_at', now);
logging.info(`Updated ${updatedRows} newsletters with created_at = now`);
},
async function down() {
// Not required: we would lose information here.
}
);

View File

@ -0,0 +1,3 @@
const {createDropNullableMigration} = require('../../utils');
module.exports = createDropNullableMigration('newsletters', 'created_at');

View File

@ -59,6 +59,18 @@ function addTableColumn(tableName, table, columnName, columnSpec = schema[tableN
}
}
function setNullable(tableName, column, transaction = db.knex) {
return transaction.schema.table(tableName, function (table) {
table.setNullable(column);
});
}
function dropNullable(tableName, column, transaction = db.knex) {
return transaction.schema.table(tableName, function (table) {
table.dropNullable(column);
});
}
function addColumn(tableName, column, transaction = db.knex, columnSpec) {
return transaction.schema.table(tableName, function (table) {
addTableColumn(tableName, table, column, columnSpec);
@ -412,6 +424,8 @@ module.exports = {
dropForeign: dropForeign,
addColumn: addColumn,
dropColumn: dropColumn,
setNullable: setNullable,
dropNullable: dropNullable,
getColumns: getColumns,
createColumnMigration,
// NOTE: below are exposed for testing purposes only