Handle many-to-many basics updates

This commit is contained in:
Aurélien Georget 2016-04-14 12:00:39 +02:00
parent 5c85e46326
commit 3efce9592e
3 changed files with 26 additions and 17 deletions

View File

@ -138,8 +138,6 @@ module.exports = function (scope, cb) {
_.forEach(attributesRemoved, function (attribute) {
const details = scope.models[modelName].oldAttributes[attribute];
console.log(attribute + ' has been removed!');
// Save the attribute as a new attribute.
scope.models[modelName].newAttributes[attribute] = _.cloneDeep(details);
@ -148,7 +146,7 @@ module.exports = function (scope, cb) {
if (details.type && _.isString(details.type)) {
builder.types(scope.models, modelName, scope.models[modelName].newAttributes[attribute], attribute, true, true);
} else if (_.isString(details.collection) || _.isString(details.model)) {
builder.relations(scope.models, modelName, scope.models[modelName].newAttributes[attribute], attribute, true, true);
builder.relations(scope.models, modelName, scope.models[modelName].newAttributes[attribute], attribute, true, true, history);
}
});
@ -158,7 +156,6 @@ module.exports = function (scope, cb) {
// If it's a new attribute.
if (!scope.models[modelName].oldAttributes.hasOwnProperty(attribute)) {
console.log(attribute + ' is new!');
// Save the attribute as a new attribute.
scope.models[modelName].newAttributes[attribute] = _.cloneDeep(details);
@ -196,8 +193,6 @@ module.exports = function (scope, cb) {
}
}();
console.log(toDrop ? attribute + ' is old attribute updated!' : attribute + ' is the same');
// The attribute has been updated.
// We will drop it then create it again with the new options.
if (toDrop) {

View File

@ -20,7 +20,7 @@ const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
* Relationship templates
*/
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop, history) {
let tplRelationUp;
let tplRelationDown;
@ -128,7 +128,7 @@ module.exports = function (models, modelName, details, attribute, toDrop, onlyDr
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
// Avoid to create table both times.
if (!models.hasOwnProperty(relationTable) || !_.isUndefined(_.get(models, relationTable + '.up.drop'))) {
if (!models.hasOwnProperty(relationTable) || !_.isEmpty(_.get(models, relationTable + '.up.drop'))) {
// Set objects
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
_.set(models, relationTable + '.up.others', '');
@ -190,20 +190,35 @@ module.exports = function (models, modelName, details, attribute, toDrop, onlyDr
attributes: models[relationTable].attributes,
toDrop: true
}));
} else {
console.log("test", attribute, modelName);
// Drop current relationships table on migration rollback.
models[relationTable].down.drop += _.unescape(_.template(tplTableDown)({
tableName: relationTable
}));
}
// Drop current relationships table on migration rollback.
models[relationTable].down.drop += _.unescape(_.template(tplTableDown)({
tableName: relationTable
}));
} else if (onlyDrop) {
// Load templates.
const tplTableUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsToMany.template'), 'utf8');
const tplTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'dropTable.template'), 'utf8');
// Save the old relationship.
const oldRelationship = history[_.capitalize(details.collection)].attributes[details.via];
// Construct old relation table name.
const oldRelationTable = _.map(_.sortBy([oldRelationship, details], 'collection'), function (table) {
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
}).join('__');
// Force singular foreign key.
oldRelationship.attribute = pluralize.singular(oldRelationship.collection);
// Define PK column.
oldRelationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
// Drop current relationships table on migration run.
models[relationTable].up.drop += _.unescape(_.template(tplTableDown)({
tableName: relationTable
tableName: oldRelationTable || relationTable
}));
// Drop current relationships table on migration rollback.
@ -221,9 +236,9 @@ module.exports = function (models, modelName, details, attribute, toDrop, onlyDr
// Create previous relationships table on migration rollback.
models[relationTable].down.others += _.unescape(_.template(tplTableUp)({
models: models,
tableName: relationTable,
tableName: oldRelationTable || relationTable,
details: details,
relationship: relationship
relationship: oldRelationship || relationship
}));
}
}

View File

@ -46,7 +46,6 @@ exports.down = function(connection, Promise) {
}
});
console.log(models);
console.log("DROPPPED " + dropped);
if (dropped) { %>