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:
parent
ac146f1e3b
commit
ed7adf16a2
1 changed files with 51 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue