2016-02-12 14:56:27 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
db = require('../../../data/db'),
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
// private
|
2014-07-10 19:10:00 +04:00
|
|
|
doRawAndFlatten,
|
|
|
|
|
|
|
|
// public
|
|
|
|
getTables,
|
|
|
|
getIndexes,
|
|
|
|
getColumns,
|
|
|
|
checkPostTable;
|
|
|
|
|
|
|
|
doRawAndFlatten = function doRaw(query, flattenFn) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex.raw(query).then(function (response) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return _.flatten(flattenFn(response));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
getTables = function getTables() {
|
|
|
|
return doRawAndFlatten('show tables', function (response) {
|
|
|
|
return _.map(response[0], function (entry) { return _.values(entry); });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
getIndexes = function getIndexes(table) {
|
|
|
|
return doRawAndFlatten('SHOW INDEXES from ' + table, function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.map(response[0], 'Key_name');
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
getColumns = function getColumns(table) {
|
|
|
|
return doRawAndFlatten('SHOW COLUMNS FROM ' + table, 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
|
|
|
|
checkPostTable = function checkPostTable() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex.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-02-12 14:56:27 +03:00
|
|
|
return db.knex.raw('ALTER TABLE posts MODIFY ' + entry.Field + ' MEDIUMTEXT');
|
2014-07-10 19:10:00 +04:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
checkPostTable: checkPostTable,
|
|
|
|
getTables: getTables,
|
|
|
|
getIndexes: getIndexes,
|
|
|
|
getColumns: getColumns
|
2014-08-17 10:17:23 +04:00
|
|
|
};
|