mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 19:04:38 +00:00
Identity attribute deletion on generated migration file
This commit is contained in:
parent
aad090c62d
commit
05fb6478db
@ -130,8 +130,30 @@ module.exports = function (scope, cb) {
|
||||
// Set new attributes object
|
||||
_.set(scope.models[modelName], 'newAttributes', {});
|
||||
|
||||
// Parse every attribute.
|
||||
_.forEach(scope.models[modelName].attributes, function (details, attribute) {
|
||||
// Identity added, updated and removed attributes
|
||||
const attributesRemoved = _.difference(_.keys(scope.models[modelName].oldAttributes), _.keys(scope.models[modelName].attributes));
|
||||
const attributesAddedOrUpdated = _.difference(_.keys(scope.models[modelName].attributes), attributesRemoved);
|
||||
|
||||
// Parse every attribute which has been removed.
|
||||
_.forEach(attributesRemoved, function (attribute) {
|
||||
const details = scope.models[modelName].oldAttributes[attribute];
|
||||
|
||||
// Save the attribute as a new attribute.
|
||||
scope.models[modelName].newAttributes[attribute] = _.cloneDeep(details);
|
||||
|
||||
// Builder: create template for each attribute-- either with a column type
|
||||
// or with a relationship.
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
// Parse every attribute which has been added or updated.
|
||||
_.forEach(attributesAddedOrUpdated, function (attribute) {
|
||||
const details = scope.models[modelName].attributes[attribute];
|
||||
|
||||
// If it's a new attribute.
|
||||
if (!scope.models[modelName].oldAttributes.hasOwnProperty(attribute)) {
|
||||
// Save the attribute as a new attribute.
|
||||
|
||||
@ -20,7 +20,7 @@ const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
|
||||
* Relationship templates
|
||||
*/
|
||||
|
||||
module.exports = function (models, modelName, details, attribute) {
|
||||
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
|
||||
let tplRelationUp;
|
||||
let tplRelationDown;
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ const _ = require('lodash');
|
||||
* Template types
|
||||
*/
|
||||
|
||||
module.exports = function (models, modelName, details, attribute, toDrop) {
|
||||
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
|
||||
|
||||
// Template: create a new column thanks to the attribute's type.
|
||||
// Firt, make sure we know the attribute type. If not, just do it
|
||||
@ -30,7 +30,7 @@ module.exports = function (models, modelName, details, attribute, toDrop) {
|
||||
const tplTypeDelete = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn.template'), 'utf8');
|
||||
|
||||
// UP
|
||||
models[modelName].attributes[attribute].create = {};
|
||||
_.set(models[modelName].attributes, attribute + '.create', {});
|
||||
|
||||
if (!_.isUndefined(toDrop) && toDrop) {
|
||||
// Template: delete a specific column.
|
||||
@ -40,11 +40,14 @@ module.exports = function (models, modelName, details, attribute, toDrop) {
|
||||
}));
|
||||
}
|
||||
|
||||
// Create when it's not an onlyDrop action
|
||||
if (_.isUndefined(onlyDrop) || onlyDrop === false) {
|
||||
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplTypeCreate)({
|
||||
tableName: modelName,
|
||||
attribute: attribute,
|
||||
details: details
|
||||
}));
|
||||
}
|
||||
|
||||
// Template: make the column chainable with the `defaultTo` template
|
||||
// if a default value is needed.
|
||||
@ -70,7 +73,7 @@ module.exports = function (models, modelName, details, attribute, toDrop) {
|
||||
}
|
||||
|
||||
// DOWN
|
||||
models[modelName].attributes[attribute].delete = {};
|
||||
_.set(models[modelName].attributes, attribute + '.delete', {});
|
||||
|
||||
if (!_.isUndefined(toDrop) && toDrop) {
|
||||
let tplTypeDeleteCreate;
|
||||
@ -80,11 +83,14 @@ module.exports = function (models, modelName, details, attribute, toDrop) {
|
||||
tplTypeDeleteCreate = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'types', 'specificType.template'), 'utf8');
|
||||
}
|
||||
|
||||
// Create when it's not an onlyDrop action
|
||||
if (_.isUndefined(onlyDrop) || onlyDrop === false) {
|
||||
// Template: delete a specific column.
|
||||
models[modelName].attributes[attribute].delete.drop = _.unescape(_.template(tplTypeDelete)({
|
||||
tableName: modelName,
|
||||
attribute: attribute
|
||||
}));
|
||||
}
|
||||
|
||||
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplTypeDeleteCreate)({
|
||||
tableName: modelName,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user