2014-07-29 01:41:45 +04:00
|
|
|
var when = require('when'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
validation = require('../validation'),
|
|
|
|
errors = require('../../errors'),
|
|
|
|
validate,
|
|
|
|
handleErrors,
|
|
|
|
cleanError;
|
|
|
|
|
|
|
|
cleanError = function cleanError(error) {
|
|
|
|
var temp,
|
|
|
|
message,
|
|
|
|
offendingProperty,
|
|
|
|
value;
|
|
|
|
|
|
|
|
if (error.raw.message.toLowerCase().indexOf('unique') !== -1) {
|
|
|
|
// This is a unique constraint failure
|
|
|
|
if (error.raw.message.indexOf('ER_DUP_ENTRY') !== -1) {
|
|
|
|
temp = error.raw.message.split('\'');
|
|
|
|
if (temp.length === 5) {
|
|
|
|
value = temp[1];
|
|
|
|
temp = temp[3].split('_');
|
|
|
|
offendingProperty = temp.length === 3 ? temp[0] + '.' + temp[1] : error.model;
|
|
|
|
}
|
|
|
|
} else if (error.raw.message.indexOf('SQLITE_CONSTRAINT') !== -1) {
|
|
|
|
temp = error.raw.message.split('failed: ');
|
|
|
|
offendingProperty = temp.length === 2 ? temp[1] : error.model;
|
|
|
|
temp = offendingProperty.split('.');
|
|
|
|
value = temp.length === 2 ? error.data[temp[1]] : 'unknown';
|
|
|
|
}
|
|
|
|
message = 'Duplicate entry found. Multiple values of "' + value + '" found for ' + offendingProperty + '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
offendingProperty = offendingProperty || error.model;
|
|
|
|
value = value || 'unknown';
|
|
|
|
message = message || error.raw.message;
|
|
|
|
|
|
|
|
return new errors.DataImportError(message, offendingProperty, value);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
handleErrors = function handleErrors(errorList) {
|
|
|
|
var processedErrors = [];
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
if (!_.isArray(errorList)) {
|
|
|
|
return when.reject(errorList);
|
|
|
|
}
|
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
_.each(errorList, function (error) {
|
|
|
|
if (!error.raw) {
|
|
|
|
// These are validation errors
|
|
|
|
processedErrors.push(error);
|
|
|
|
} else if (_.isArray(error.raw)) {
|
|
|
|
processedErrors = processedErrors.concat(error.raw);
|
|
|
|
} else {
|
|
|
|
processedErrors.push(cleanError(error));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.reject(processedErrors);
|
|
|
|
};
|
|
|
|
|
|
|
|
validate = function validate(data) {
|
|
|
|
var validateOps = [];
|
|
|
|
|
|
|
|
_.each(_.keys(data.data), function (tableName) {
|
|
|
|
_.each(data.data[tableName], function (importValues) {
|
|
|
|
validateOps.push(validation.validateSchema(tableName, importValues));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.settle(validateOps).then(function (descriptors) {
|
|
|
|
var errorList = [];
|
|
|
|
|
|
|
|
_.each(descriptors, function (d) {
|
|
|
|
if (d.state === 'rejected') {
|
|
|
|
errorList = errorList.concat(d.reason);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.isEmpty(errorList)) {
|
|
|
|
return when.reject(errorList);
|
|
|
|
}
|
|
|
|
|
|
|
|
return when.resolve();
|
|
|
|
});
|
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = function (version, data) {
|
|
|
|
var importer;
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
return validate(data).then(function () {
|
|
|
|
try {
|
|
|
|
importer = require('./' + version);
|
|
|
|
} catch (ignore) {
|
|
|
|
// Zero effs given
|
|
|
|
}
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
if (!importer) {
|
|
|
|
return when.reject('No importer found');
|
|
|
|
}
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
return importer.importData(data);
|
|
|
|
}).catch(function (result) {
|
|
|
|
return handleErrors(result);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|