0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Refactored 'add table' migrations to use helper

- this removes a lot of code duplication and streamlines how we do
  things across the codebase
This commit is contained in:
Daniel Lockyer 2020-11-19 09:53:58 +00:00
parent ed2a21bef4
commit 5c3439dc86
13 changed files with 34 additions and 453 deletions

View file

@ -1,36 +1,2 @@
const Promise = require('bluebird');
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'webhooks';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = function addWebhooksTable(options) {
let connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return Promise.resolve();
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = function removeWebhooksTable(options) {
let connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return Promise.resolve();
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('webhooks');

View file

@ -1,36 +1,2 @@
const Promise = require('bluebird');
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'posts_authors';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = function addMultipleAuthorsTable(options) {
let connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return Promise.resolve();
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = function removeMultipleAuthorsTable(options) {
let connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return Promise.resolve();
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('posts_authors');

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'actions';
const message1 = `Adding table: ${table}`;
const message2 = `Dropping table: ${table}`;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('actions');

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'sessions';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('sessions');

View file

@ -1,34 +1,6 @@
const Promise = require('bluebird');
const commands = require('../../../schema').commands;
const logging = require('../../../../../shared/logging');
const {addTable, combineNonTransactionalMigrations} = require('../../utils');
const tables = ['integrations', 'api_keys'];
const _private = {};
_private.addOrRemoveTable = (type, table, options) => {
const isAdding = type === 'Adding';
const operation = isAdding ? commands.createTable : commands.deleteTable;
const message = `${type} ${table} table`;
return options.connection.schema.hasTable(table)
.then((exists) => {
if ((isAdding && exists || !isAdding && !exists)) {
logging.warn(message);
return Promise.resolve();
}
logging.info(message);
return operation(table, options.connection);
});
};
_private.handle = (migrationOptions) => {
return (options) => {
return Promise.each(tables, (table) => {
return _private.addOrRemoveTable(migrationOptions.type, table, options);
});
};
};
module.exports.up = _private.handle({type: 'Adding'});
module.exports.down = _private.handle({type: 'Dropping'});
module.exports = combineNonTransactionalMigrations(
addTable('integrations'),
addTable('api_keys')
);

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'mobiledoc_revisions';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('mobiledoc_revisions');

View file

@ -1,34 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema/commands');
module.exports = {
config: {
transaction: true
},
async up(options){
const conn = options.transacting || options.connection;
const hasTable = await conn.schema.hasTable('members_stripe_customers_subscriptions');
if (hasTable) {
logging.warn('Adding table: members_stripe_customers_subscriptions');
return;
}
logging.info('Adding table: members_stripe_customers_subscriptions');
return commands.createTable('members_stripe_customers_subscriptions', conn);
},
async down(options){
const conn = options.transacting || options.connection;
const hasTable = await conn.schema.hasTable('members_stripe_customers_subscriptions');
if (!hasTable) {
logging.warn('Dropping table: members_stripe_customers_subscriptions');
return;
}
logging.info('Dropping table: members_stripe_customers_subscriptions');
return commands.deleteTable('members_stripe_customers_subscriptions', conn);
}
};
const {addTable} = require('../../utils');
module.exports = addTable('members_stripe_customers_subscriptions');

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'members';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('members');

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'posts_meta';
const message1 = `Adding table: ${table}`;
const message2 = `Dropping table: ${table}`;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('posts_meta');

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'emails';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('emails');

View file

@ -1,35 +1,8 @@
const Promise = require('bluebird');
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const {addTable, combineNonTransactionalMigrations} = require('../../utils');
module.exports = {
async up({connection}) {
// table creation order is important because of foreign key constraints,
// email_recipients references email_batches so email_batches has to exist when creating
return Promise.each(['email_batches', 'email_recipients'], async (table) => {
const tableExists = await connection.schema.hasTable(table);
if (tableExists) {
return logging.warn(`Skipping add table "${table}" - already exists`);
}
logging.info(`Adding table: ${table}`);
return commands.createTable(table, connection);
});
},
async down({connection}) {
// table deletion order is important because of foreign key constraints,
// email_recipients references email_batches so it has to be deleted first to not break constraints
return Promise.each(['email_recipients', 'email_batches'], async (table) => {
const tableExists = await connection.schema.hasTable(table);
if (!tableExists) {
return logging.warn(`Skipping drop table "${table}" - does not exist`);
}
logging.info(`Dropping table: ${table}`);
return commands.deleteTable(table, connection);
});
}
};
// table order is important because of foreign key constraints,
// email_recipients references email_batches so email_batches has to exist when creating
module.exports = combineNonTransactionalMigrations(
addTable('email_batches'),
addTable('email_recipients')
);

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'labels';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('labels');

View file

@ -1,35 +1,2 @@
const logging = require('../../../../../shared/logging');
const commands = require('../../../schema').commands;
const table = 'members_labels';
const message1 = 'Adding table: ' + table;
const message2 = 'Dropping table: ' + table;
module.exports.up = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (exists) {
logging.warn(message1);
return;
}
logging.info(message1);
return commands.createTable(table, connection);
});
};
module.exports.down = (options) => {
const connection = options.connection;
return connection.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logging.warn(message2);
return;
}
logging.info(message2);
return commands.deleteTable(table, connection);
});
};
const {addTable} = require('../../utils');
module.exports = addTable('members_labels');