2016-01-25 20:50:04 +03:00
|
|
|
var _ = require('lodash'),
|
2014-07-10 19:10:00 +04:00
|
|
|
errors = require('../../errors'),
|
2016-01-25 20:50:04 +03:00
|
|
|
commands = require('../schema').commands,
|
2014-07-10 19:10:00 +04:00
|
|
|
schema = require('../schema').tables,
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../../i18n'),
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
// private
|
|
|
|
logInfo,
|
|
|
|
|
|
|
|
// public
|
|
|
|
getDeleteCommands,
|
|
|
|
getAddCommands,
|
|
|
|
addColumnCommands,
|
2016-02-07 19:23:24 +03:00
|
|
|
dropColumnCommands,
|
2014-07-10 19:10:00 +04:00
|
|
|
modifyUniqueCommands;
|
|
|
|
|
|
|
|
logInfo = function logInfo(message) {
|
2015-11-12 15:29:45 +03:00
|
|
|
errors.logInfo(i18n.t('notices.data.migration.commands.migrations'), message);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
getDeleteCommands = function getDeleteCommands(oldTables, newTables) {
|
|
|
|
var deleteTables = _.difference(oldTables, newTables);
|
|
|
|
return _.map(deleteTables, function (table) {
|
|
|
|
return function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.commands.deletingTable', {table: table}));
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.deleteTable(table);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2016-02-07 19:23:24 +03:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
getAddCommands = function getAddCommands(oldTables, newTables) {
|
|
|
|
var addTables = _.difference(newTables, oldTables);
|
|
|
|
return _.map(addTables, function (table) {
|
|
|
|
return function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.commands.creatingTable', {table: table}));
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.createTable(table);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2016-02-07 19:23:24 +03:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
addColumnCommands = function addColumnCommands(table, columns) {
|
|
|
|
var columnKeys = _.keys(schema[table]),
|
|
|
|
addColumns = _.difference(columnKeys, columns);
|
|
|
|
|
|
|
|
return _.map(addColumns, function (column) {
|
|
|
|
return function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.commands.addingColumn', {table: table, column: column}));
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.addColumn(table, column);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2016-02-07 19:23:24 +03:00
|
|
|
|
|
|
|
dropColumnCommands = function dropColumnCommands(table, columns) {
|
|
|
|
var columnKeys = _.keys(schema[table]),
|
|
|
|
dropColumns = _.difference(columns, columnKeys);
|
|
|
|
|
|
|
|
return _.map(dropColumns, function (column) {
|
|
|
|
return function () {
|
|
|
|
logInfo(i18n.t('notices.data.migration.commands.droppingColumn', {table: table, column: column}));
|
|
|
|
return commands.dropColumn(table, column);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
modifyUniqueCommands = function modifyUniqueCommands(table, indexes) {
|
|
|
|
var columnKeys = _.keys(schema[table]);
|
|
|
|
return _.map(columnKeys, function (column) {
|
2014-11-20 17:50:01 +03:00
|
|
|
if (schema[table][column].unique === true) {
|
2014-07-10 19:10:00 +04:00
|
|
|
if (!_.contains(indexes, table + '_' + column + '_unique')) {
|
|
|
|
return function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.commands.addingUnique', {table: table, column: column}));
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.addUnique(table, column);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (!schema[table][column].unique) {
|
|
|
|
if (_.contains(indexes, table + '_' + column + '_unique')) {
|
|
|
|
return function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.commands.droppingUnique', {table: table, column: column}));
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.dropUnique(table, column);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getDeleteCommands: getDeleteCommands,
|
|
|
|
getAddCommands: getAddCommands,
|
|
|
|
addColumnCommands: addColumnCommands,
|
2016-02-07 19:23:24 +03:00
|
|
|
dropColumnCommands: dropColumnCommands,
|
2014-07-10 19:10:00 +04:00
|
|
|
modifyUniqueCommands: modifyUniqueCommands
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|