0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Added createColumnMigration helper

no-issue
This commit is contained in:
Fabien O'Carroll 2019-09-04 12:11:53 +08:00
parent b1c61e4e84
commit 7382967613

View file

@ -133,6 +133,40 @@ function checkTables(transaction) {
}
}
const createLog = type => msg => common.logging[type](msg);
function createColumnMigration(...migrations) {
async function runColumnMigration(conn, migration) {
const {
table,
column,
dbIsInCorrectState,
operation,
operationVerb,
columnDefinition
} = migration;
const hasColumn = await conn.schema.hasColumn(table, column);
const isInCorrectState = dbIsInCorrectState(hasColumn);
const log = createLog(isInCorrectState ? 'info' : 'warn');
log(`${operationVerb} ${table}.${column}`);
if (!isInCorrectState) {
await operation(table, column, conn, columnDefinition);
}
}
return async function columnMigration(options) {
const conn = options.transacting || options.connection;
for (const migration of migrations) {
await runColumnMigration(conn, migration);
}
};
}
module.exports = {
checkTables: checkTables,
createTable: createTable,
@ -143,5 +177,6 @@ module.exports = {
dropUnique: dropUnique,
addColumn: addColumn,
dropColumn: dropColumn,
getColumns: getColumns
getColumns: getColumns,
createColumnMigration
};