0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

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
This commit is contained in:
Naz 2021-02-04 13:37:05 +13:00
parent 6b61bcf123
commit 4b74230624

View file

@ -103,31 +103,16 @@ function createTable(table, transaction, tableSpec = schema[table]) {
} }
return (transaction || db.knex).schema.createTable(table, function (t) { return (transaction || db.knex).schema.createTable(table, function (t) {
let tableIndexes = []; Object.keys(tableSpec)
let tableUniqueConstraints = []; .filter(column => !(column.startsWith('@@')))
.forEach(column => addTableColumn(table, t, column, tableSpec[column]));
const columnKeys = _.keys(tableSpec); if (tableSpec['@@INDEXES@@']) {
_.each(columnKeys, function (column) { tableSpec['@@INDEXES@@'].forEach(index => t.index(index));
if (column === '@@INDEXES@@') { }
tableIndexes = tableSpec['@@INDEXES@@']; if (tableSpec['@@UNIQUE_CONSTRAINTS@@']) {
return; tableSpec['@@UNIQUE_CONSTRAINTS@@'].forEach(unique => t.unique(unique));
} }
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);
});
}); });
}); });
} }