Ghost/core/server/data/schema/clients/mysql.js
Daniel Lockyer bf45ef4a87 Cleaned up DB connection fallback in migrations commands
- throughout the migration utils we use the passed in DB connection or
  fallback to the `db.knex` instance
- this works, but it means we have places we need to make sure to
  implement `(transaction || db.knex)` everywhere
- as I'm working on refactoring the utils, this was also causing
  problems because I'd be checking the `transaction` instance but that may
  be null/undefined
- this commit pulls the fallback into the function parameters where it's
  evaluated at runtime
- this also has the added benefit that we get jsdoc typing now because
  the types are easy to figure out
- note: `transaction` should probably be renamed to `connection` because
  it's not necessary a transaction... but baby steps 🤓
2022-03-02 13:27:21 +01:00

50 lines
1.8 KiB
JavaScript

const _ = require('lodash');
const db = require('../../../data/db');
const doRawAndFlatten = function doRaw(query, transaction = db.knex, flattenFn) {
return transaction.raw(query).then(function (response) {
return _.flatten(flattenFn(response));
});
};
const getTables = function getTables(transaction) {
return doRawAndFlatten('show tables', transaction, function (response) {
return _.map(response[0], function (entry) {
return _.values(entry);
});
});
};
const getIndexes = function getIndexes(table, transaction) {
return doRawAndFlatten('SHOW INDEXES from ' + table, transaction, function (response) {
return _.map(response[0], 'Key_name');
});
};
const getColumns = function getColumns(table, transaction) {
return doRawAndFlatten('SHOW COLUMNS FROM ' + table, transaction, function (response) {
return _.map(response[0], 'Field');
});
};
// This function changes the type of posts.html and posts.markdown columns to mediumtext. Due to
// a wrong datatype in schema.js some installations using mysql could have been created using the
// data type text instead of mediumtext.
// For details see: https://github.com/TryGhost/Ghost/issues/1947
const checkPostTable = function checkPostTable(transaction = db.knex) {
return transaction.raw('SHOW FIELDS FROM posts where Field ="html" OR Field = "markdown"').then(function (response) {
return _.flatten(_.map(response[0], function (entry) {
if (entry.Type.toLowerCase() !== 'mediumtext') {
return (transaction || db.knex).raw('ALTER TABLE posts MODIFY ' + entry.Field + ' MEDIUMTEXT');
}
}));
});
};
module.exports = {
checkPostTable: checkPostTable,
getTables: getTables,
getIndexes: getIndexes,
getColumns: getColumns
};