0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Added migration util for adding new tables

no-issue

This pulls out some commonly used logic into a util for future use
This commit is contained in:
Fabien O'Carroll 2020-09-16 15:55:52 +01:00 committed by Fabien 'egg' O'Carroll
parent ac146f1e3b
commit ed7adf16a2

View file

@ -1,8 +1,37 @@
const ObjectId = require('bson-objectid').default;
const logging = require('../../../shared/logging');
const commands = require('../schema').commands;
const MIGRATION_USER = 1;
/**
* Creates a migrations which will add a new table from schema.js to the database
*/
function addTable(name) {
return createNonTransactionalMigration(
async function up(connection) {
const tableExists = await connection.schema.hasTable(name);
if (tableExists) {
logging.warn(`Skipping adding table: ${name} - table already exists`);
return;
}
logging.info(`Adding table: ${name}`);
return commands.createTable(name, connection);
},
async function down(connection) {
const tableExists = await connection.schema.hasTable(name);
if (!tableExists) {
logging.warn(`Skipping dropping table: ${name} - table does not exist`);
return;
}
logging.info(`Dropping table: ${name}`);
return commands.deleteTable(name, connection);
}
);
}
/**
* Creates a migration which will add a permission to the database
*
@ -172,6 +201,26 @@ function addPermissionWithRoles(config, roles) {
);
}
/**
* @param {(connection: import('knex')) => Promise<void>} up
* @param {(connection: import('knex')) => Promise<void>} down
*
* @returns {Migration}
*/
function createNonTransactionalMigration(up, down) {
return {
config: {
transaction: false
},
async up(config) {
await up(config.connection);
},
async down(config) {
await down(config.connection);
}
};
}
/**
* @param {(connection: import('knex')) => Promise<void>} up
* @param {(connection: import('knex')) => Promise<void>} down
@ -218,10 +267,12 @@ function combineTransactionalMigrations(...migrations) {
}
module.exports = {
addTable,
addPermission,
addPermissionToRole,
addPermissionWithRoles,
createTransactionalMigration,
createNonTransactionalMigration,
combineTransactionalMigrations,
meta: {
MIGRATION_USER