2014-07-10 19:10:00 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
errors = require('../../errors'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
schema = require('../schema').tables,
|
|
|
|
|
|
|
|
// private
|
|
|
|
logInfo,
|
|
|
|
|
|
|
|
// public
|
|
|
|
getDeleteCommands,
|
|
|
|
getAddCommands,
|
|
|
|
addColumnCommands,
|
|
|
|
modifyUniqueCommands;
|
|
|
|
|
|
|
|
logInfo = function logInfo(message) {
|
|
|
|
errors.logInfo('Migrations', message);
|
|
|
|
};
|
|
|
|
|
|
|
|
getDeleteCommands = function getDeleteCommands(oldTables, newTables) {
|
|
|
|
var deleteTables = _.difference(oldTables, newTables);
|
|
|
|
return _.map(deleteTables, function (table) {
|
|
|
|
return function () {
|
|
|
|
logInfo('Deleting table: ' + table);
|
|
|
|
return utils.deleteTable(table);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
getAddCommands = function getAddCommands(oldTables, newTables) {
|
|
|
|
var addTables = _.difference(newTables, oldTables);
|
|
|
|
return _.map(addTables, function (table) {
|
|
|
|
return function () {
|
|
|
|
logInfo('Creating table: ' + table);
|
|
|
|
return utils.createTable(table);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
addColumnCommands = function addColumnCommands(table, columns) {
|
|
|
|
var columnKeys = _.keys(schema[table]),
|
|
|
|
addColumns = _.difference(columnKeys, columns);
|
|
|
|
|
|
|
|
return _.map(addColumns, function (column) {
|
|
|
|
return function () {
|
|
|
|
logInfo('Adding column: ' + table + '.' + column);
|
2014-07-18 21:58:29 +04:00
|
|
|
return utils.addColumn(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 () {
|
|
|
|
logInfo('Adding unique on: ' + table + '.' + column);
|
|
|
|
return utils.addUnique(table, column);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (!schema[table][column].unique) {
|
|
|
|
if (_.contains(indexes, table + '_' + column + '_unique')) {
|
|
|
|
return function () {
|
|
|
|
logInfo('Dropping unique on: ' + table + '.' + column);
|
|
|
|
return utils.dropUnique(table, column);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getDeleteCommands: getDeleteCommands,
|
|
|
|
getAddCommands: getAddCommands,
|
|
|
|
addColumnCommands: addColumnCommands,
|
|
|
|
modifyUniqueCommands: modifyUniqueCommands
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|