From 4b742306245124f7c5dd455b62ea12fb3655198b Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 4 Feb 2021 13:37:05 +1300 Subject: [PATCH] Refactored createTable migration utility refs https://github.com/TryGhost/Ghost/issues/12567 - The method was using unneeded lodash dependency and was too complex for what it was doing - Reshuffled internal code to use native JS filter/forEach iterators --- core/server/data/schema/commands.js | 33 ++++++++--------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/core/server/data/schema/commands.js b/core/server/data/schema/commands.js index 34bfacca1c..835b9650c5 100644 --- a/core/server/data/schema/commands.js +++ b/core/server/data/schema/commands.js @@ -103,31 +103,16 @@ function createTable(table, transaction, tableSpec = schema[table]) { } return (transaction || db.knex).schema.createTable(table, function (t) { - let tableIndexes = []; - let tableUniqueConstraints = []; + Object.keys(tableSpec) + .filter(column => !(column.startsWith('@@'))) + .forEach(column => addTableColumn(table, t, column, tableSpec[column])); - const columnKeys = _.keys(tableSpec); - _.each(columnKeys, function (column) { - if (column === '@@INDEXES@@') { - tableIndexes = tableSpec['@@INDEXES@@']; - return; - } - - if (column === '@@UNIQUE_CONSTRAINTS@@') { - tableUniqueConstraints = tableSpec['@@UNIQUE_CONSTRAINTS@@']; - return; - } - - return addTableColumn(table, t, column, tableSpec[column]); - }); - - _.each(tableIndexes, function (index) { - t.index(index); - }); - - _.each(tableUniqueConstraints, function (unique) { - t.unique(unique); - }); + if (tableSpec['@@INDEXES@@']) { + tableSpec['@@INDEXES@@'].forEach(index => t.index(index)); + } + if (tableSpec['@@UNIQUE_CONSTRAINTS@@']) { + tableSpec['@@UNIQUE_CONSTRAINTS@@'].forEach(unique => t.unique(unique)); + } }); }); }