mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 08:08:05 +00:00
Handle many-to-many basics updates
This commit is contained in:
parent
5c85e46326
commit
3efce9592e
@ -138,8 +138,6 @@ module.exports = function (scope, cb) {
|
|||||||
_.forEach(attributesRemoved, function (attribute) {
|
_.forEach(attributesRemoved, function (attribute) {
|
||||||
const details = scope.models[modelName].oldAttributes[attribute];
|
const details = scope.models[modelName].oldAttributes[attribute];
|
||||||
|
|
||||||
console.log(attribute + ' has been removed!');
|
|
||||||
|
|
||||||
// Save the attribute as a new attribute.
|
// Save the attribute as a new attribute.
|
||||||
scope.models[modelName].newAttributes[attribute] = _.cloneDeep(details);
|
scope.models[modelName].newAttributes[attribute] = _.cloneDeep(details);
|
||||||
|
|
||||||
@ -148,7 +146,7 @@ module.exports = function (scope, cb) {
|
|||||||
if (details.type && _.isString(details.type)) {
|
if (details.type && _.isString(details.type)) {
|
||||||
builder.types(scope.models, modelName, scope.models[modelName].newAttributes[attribute], attribute, true, true);
|
builder.types(scope.models, modelName, scope.models[modelName].newAttributes[attribute], attribute, true, true);
|
||||||
} else if (_.isString(details.collection) || _.isString(details.model)) {
|
} 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 it's a new attribute.
|
||||||
if (!scope.models[modelName].oldAttributes.hasOwnProperty(attribute)) {
|
if (!scope.models[modelName].oldAttributes.hasOwnProperty(attribute)) {
|
||||||
console.log(attribute + ' is new!');
|
|
||||||
// Save the attribute as a new attribute.
|
// Save the attribute as a new attribute.
|
||||||
scope.models[modelName].newAttributes[attribute] = _.cloneDeep(details);
|
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.
|
// The attribute has been updated.
|
||||||
// We will drop it then create it again with the new options.
|
// We will drop it then create it again with the new options.
|
||||||
if (toDrop) {
|
if (toDrop) {
|
||||||
|
|||||||
@ -20,7 +20,7 @@ const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
|
|||||||
* Relationship templates
|
* Relationship templates
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
|
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop, history) {
|
||||||
let tplRelationUp;
|
let tplRelationUp;
|
||||||
let tplRelationDown;
|
let tplRelationDown;
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ module.exports = function (models, modelName, details, attribute, toDrop, onlyDr
|
|||||||
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
|
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
|
||||||
|
|
||||||
// Avoid to create table both times.
|
// 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
|
// Set objects
|
||||||
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
|
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
|
||||||
_.set(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,
|
attributes: models[relationTable].attributes,
|
||||||
toDrop: true
|
toDrop: true
|
||||||
}));
|
}));
|
||||||
}
|
} else {
|
||||||
|
console.log("test", attribute, modelName);
|
||||||
// Drop current relationships table on migration rollback.
|
// Drop current relationships table on migration rollback.
|
||||||
models[relationTable].down.drop += _.unescape(_.template(tplTableDown)({
|
models[relationTable].down.drop += _.unescape(_.template(tplTableDown)({
|
||||||
tableName: relationTable
|
tableName: relationTable
|
||||||
}));
|
}));
|
||||||
|
}
|
||||||
} else if (onlyDrop) {
|
} else if (onlyDrop) {
|
||||||
// Load templates.
|
// Load templates.
|
||||||
const tplTableUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsToMany.template'), 'utf8');
|
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');
|
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.
|
// Drop current relationships table on migration run.
|
||||||
models[relationTable].up.drop += _.unescape(_.template(tplTableDown)({
|
models[relationTable].up.drop += _.unescape(_.template(tplTableDown)({
|
||||||
tableName: relationTable
|
tableName: oldRelationTable || relationTable
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Drop current relationships table on migration rollback.
|
// 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.
|
// Create previous relationships table on migration rollback.
|
||||||
models[relationTable].down.others += _.unescape(_.template(tplTableUp)({
|
models[relationTable].down.others += _.unescape(_.template(tplTableUp)({
|
||||||
models: models,
|
models: models,
|
||||||
tableName: relationTable,
|
tableName: oldRelationTable || relationTable,
|
||||||
details: details,
|
details: details,
|
||||||
relationship: relationship
|
relationship: oldRelationship || relationship
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,7 +46,6 @@ exports.down = function(connection, Promise) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(models);
|
|
||||||
console.log("DROPPPED " + dropped);
|
console.log("DROPPPED " + dropped);
|
||||||
|
|
||||||
if (dropped) { %>
|
if (dropped) { %>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user