2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-07-29 01:41:45 +04:00
|
|
|
_ = require('lodash'),
|
|
|
|
validation = require('../validation'),
|
|
|
|
errors = require('../../errors'),
|
2014-08-22 23:04:52 +04:00
|
|
|
uuid = require('node-uuid'),
|
2014-12-11 18:50:10 +03:00
|
|
|
importer = require('./data-importer'),
|
2014-08-22 23:04:52 +04:00
|
|
|
tables = require('../schema').tables,
|
2014-07-29 01:41:45 +04:00
|
|
|
validate,
|
|
|
|
handleErrors,
|
2014-09-25 05:18:34 +04:00
|
|
|
checkDuplicateAttributes,
|
2014-08-22 23:04:52 +04:00
|
|
|
sanitize,
|
2014-12-29 21:33:47 +03:00
|
|
|
cleanError,
|
|
|
|
doImport;
|
2014-07-29 01:41:45 +04:00
|
|
|
|
|
|
|
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';
|
2014-08-27 00:41:11 +04:00
|
|
|
} else if (error.raw.detail) {
|
|
|
|
value = error.raw.detail;
|
|
|
|
offendingProperty = error.model;
|
2014-07-29 01:41:45 +04:00
|
|
|
}
|
|
|
|
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)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(errorList);
|
2014-08-09 23:16:54 +04:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(processedErrors);
|
2014-07-29 01:41:45 +04:00
|
|
|
};
|
|
|
|
|
2014-09-25 05:18:34 +04:00
|
|
|
checkDuplicateAttributes = function checkDuplicateAttributes(data, comparedValue, attribs) {
|
|
|
|
// Check if any objects in data have the same attribute values
|
|
|
|
return _.find(data, function (datum) {
|
|
|
|
return _.all(attribs, function (attrib) {
|
|
|
|
return datum[attrib] === comparedValue[attrib];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-22 23:04:52 +04:00
|
|
|
sanitize = function sanitize(data) {
|
2014-09-25 05:18:34 +04:00
|
|
|
var allProblems = {},
|
|
|
|
tableNames = _.sortBy(_.keys(data.data), function (tableName) {
|
|
|
|
// We want to guarantee posts and tags go first
|
|
|
|
if (tableName === 'posts') {
|
|
|
|
return 1;
|
|
|
|
} else if (tableName === 'tags') {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 3;
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(tableNames, function (tableName) {
|
2014-12-11 18:50:10 +03:00
|
|
|
// Sanitize the table data for duplicates and valid uuid and created_at values
|
2014-09-25 05:18:34 +04:00
|
|
|
var sanitizedTableData = _.transform(data.data[tableName], function (memo, importValues) {
|
2014-08-22 23:04:52 +04:00
|
|
|
var uuidMissing = (!importValues.uuid && tables[tableName].uuid) ? true : false,
|
2014-12-11 18:50:10 +03:00
|
|
|
uuidMalformed = (importValues.uuid && !validation.validator.isUUID(importValues.uuid)) ? true : false,
|
2014-09-25 05:18:34 +04:00
|
|
|
isDuplicate,
|
|
|
|
problemTag;
|
2014-08-22 23:04:52 +04:00
|
|
|
|
2014-12-11 18:50:10 +03:00
|
|
|
// Check for correct UUID and fix if necessary
|
2014-08-22 23:04:52 +04:00
|
|
|
if (uuidMissing || uuidMalformed) {
|
|
|
|
importValues.uuid = uuid.v4();
|
|
|
|
}
|
2014-09-25 05:18:34 +04:00
|
|
|
|
|
|
|
// Custom sanitize for posts, tags and users
|
|
|
|
if (tableName === 'posts') {
|
|
|
|
// Check if any previously added posts have the same
|
|
|
|
// title and slug
|
|
|
|
isDuplicate = checkDuplicateAttributes(memo.data, importValues, ['title', 'slug']);
|
|
|
|
|
|
|
|
// If it's a duplicate add to the problems and continue on
|
|
|
|
if (isDuplicate) {
|
|
|
|
// TODO: Put the reason why it was a problem?
|
|
|
|
memo.problems.push(importValues);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (tableName === 'tags') {
|
|
|
|
// Check if any previously added posts have the same
|
|
|
|
// name and slug
|
|
|
|
isDuplicate = checkDuplicateAttributes(memo.data, importValues, ['name', 'slug']);
|
|
|
|
|
|
|
|
// If it's a duplicate add to the problems and continue on
|
|
|
|
if (isDuplicate) {
|
|
|
|
// TODO: Put the reason why it was a problem?
|
|
|
|
// Remember this tag so it can be updated later
|
|
|
|
importValues.duplicate = isDuplicate;
|
|
|
|
memo.problems.push(importValues);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (tableName === 'posts_tags') {
|
|
|
|
// Fix up removed tags associations
|
|
|
|
problemTag = _.find(allProblems.tags, function (tag) {
|
|
|
|
return tag.id === importValues.tag_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update the tag id to the original "duplicate" id
|
|
|
|
if (problemTag) {
|
|
|
|
importValues.tag_id = problemTag.duplicate.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memo.data.push(importValues);
|
|
|
|
}, {
|
|
|
|
data: [],
|
|
|
|
problems: []
|
2014-08-22 23:04:52 +04:00
|
|
|
});
|
2014-09-25 05:18:34 +04:00
|
|
|
|
|
|
|
// Store the table data to return
|
|
|
|
data.data[tableName] = sanitizedTableData.data;
|
|
|
|
|
|
|
|
// Keep track of all problems for all tables
|
|
|
|
if (!_.isEmpty(sanitizedTableData.problems)) {
|
|
|
|
allProblems[tableName] = sanitizedTableData.problems;
|
|
|
|
}
|
2014-08-22 23:04:52 +04:00
|
|
|
});
|
|
|
|
|
2014-09-25 05:18:34 +04:00
|
|
|
return {
|
|
|
|
data: data,
|
|
|
|
problems: allProblems
|
|
|
|
};
|
2014-08-22 23:04:52 +04:00
|
|
|
};
|
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
validate = function validate(data) {
|
|
|
|
var validateOps = [];
|
|
|
|
|
|
|
|
_.each(_.keys(data.data), function (tableName) {
|
|
|
|
_.each(data.data[tableName], function (importValues) {
|
|
|
|
validateOps.push(validation.validateSchema(tableName, importValues));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.settle(validateOps).then(function (descriptors) {
|
2014-07-29 01:41:45 +04:00
|
|
|
var errorList = [];
|
|
|
|
|
|
|
|
_.each(descriptors, function (d) {
|
2014-08-17 10:17:23 +04:00
|
|
|
if (d.isRejected()) {
|
|
|
|
errorList = errorList.concat(d.reason());
|
2014-07-29 01:41:45 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.isEmpty(errorList)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(errorList);
|
2014-07-29 01:41:45 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2014-12-29 21:33:47 +03:00
|
|
|
doImport = function (data) {
|
2014-12-11 18:50:10 +03:00
|
|
|
var sanitizeResults = sanitize(data);
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2014-09-25 05:18:34 +04:00
|
|
|
data = sanitizeResults.data;
|
2014-08-22 23:04:52 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
return validate(data).then(function () {
|
|
|
|
return importer.importData(data);
|
2014-09-25 05:18:34 +04:00
|
|
|
}).then(function () {
|
|
|
|
return sanitizeResults;
|
2014-07-29 01:41:45 +04:00
|
|
|
}).catch(function (result) {
|
|
|
|
return handleErrors(result);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2014-12-29 21:33:47 +03:00
|
|
|
|
|
|
|
module.exports.doImport = doImport;
|