2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const db = require('../../../data/db');
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2022-03-02 15:03:56 +03:00
|
|
|
const doRawAndFlatten = function doRaw(query, transaction = db.knex, flattenFn) {
|
|
|
|
return transaction.raw(query).then(function (response) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return _.flatten(flattenFn(response));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getTables = function getTables(transaction) {
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRawAndFlatten('show tables', transaction, function (response) {
|
2017-11-01 16:44:54 +03:00
|
|
|
return _.map(response[0], function (entry) {
|
|
|
|
return _.values(entry);
|
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getIndexes = function getIndexes(table, transaction) {
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRawAndFlatten('SHOW INDEXES from ' + table, transaction, function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.map(response[0], 'Key_name');
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getColumns = function getColumns(table, transaction) {
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRawAndFlatten('SHOW COLUMNS FROM ' + table, transaction, function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.map(response[0], 'Field');
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2022-03-02 15:03:56 +03:00
|
|
|
const checkPostTable = function checkPostTable(transaction = db.knex) {
|
|
|
|
return transaction.raw('SHOW FIELDS FROM posts where Field ="html" OR Field = "markdown"').then(function (response) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return _.flatten(_.map(response[0], function (entry) {
|
|
|
|
if (entry.Type.toLowerCase() !== 'mediumtext') {
|
2016-07-14 13:59:42 +03:00
|
|
|
return (transaction || db.knex).raw('ALTER TABLE posts MODIFY ' + entry.Field + ' MEDIUMTEXT');
|
2014-07-10 19:10:00 +04:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
checkPostTable: checkPostTable,
|
2017-11-01 16:44:54 +03:00
|
|
|
getTables: getTables,
|
2014-07-10 19:10:00 +04:00
|
|
|
getIndexes: getIndexes,
|
|
|
|
getColumns: getColumns
|
2014-08-17 10:17:23 +04:00
|
|
|
};
|