Identity attribute deletion on generated migration file

This commit is contained in:
Aurélien Georget 2016-03-23 16:53:42 +01:00
parent aad090c62d
commit 05fb6478db
3 changed files with 44 additions and 16 deletions

View File

@ -130,8 +130,30 @@ module.exports = function (scope, cb) {
// Set new attributes object // Set new attributes object
_.set(scope.models[modelName], 'newAttributes', {}); _.set(scope.models[modelName], 'newAttributes', {});
// Parse every attribute. // Identity added, updated and removed attributes
_.forEach(scope.models[modelName].attributes, function (details, attribute) { 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 it's a new attribute.
if (!scope.models[modelName].oldAttributes.hasOwnProperty(attribute)) { if (!scope.models[modelName].oldAttributes.hasOwnProperty(attribute)) {
// Save the attribute as a new attribute. // Save the attribute as a new attribute.

View File

@ -20,7 +20,7 @@ const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
* Relationship templates * Relationship templates
*/ */
module.exports = function (models, modelName, details, attribute) { module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
let tplRelationUp; let tplRelationUp;
let tplRelationDown; let tplRelationDown;

View File

@ -15,7 +15,7 @@ const _ = require('lodash');
* Template types * 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. // Template: create a new column thanks to the attribute's type.
// Firt, make sure we know the attribute type. If not, just do it // 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'); const tplTypeDelete = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn.template'), 'utf8');
// UP // UP
models[modelName].attributes[attribute].create = {}; _.set(models[modelName].attributes, attribute + '.create', {});
if (!_.isUndefined(toDrop) && toDrop) { if (!_.isUndefined(toDrop) && toDrop) {
// Template: delete a specific column. // Template: delete a specific column.
@ -40,11 +40,14 @@ module.exports = function (models, modelName, details, attribute, toDrop) {
})); }));
} }
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplTypeCreate)({ // Create when it's not an onlyDrop action
tableName: modelName, if (_.isUndefined(onlyDrop) || onlyDrop === false) {
attribute: attribute, models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplTypeCreate)({
details: details tableName: modelName,
})); attribute: attribute,
details: details
}));
}
// Template: make the column chainable with the `defaultTo` template // Template: make the column chainable with the `defaultTo` template
// if a default value is needed. // if a default value is needed.
@ -70,7 +73,7 @@ module.exports = function (models, modelName, details, attribute, toDrop) {
} }
// DOWN // DOWN
models[modelName].attributes[attribute].delete = {}; _.set(models[modelName].attributes, attribute + '.delete', {});
if (!_.isUndefined(toDrop) && toDrop) { if (!_.isUndefined(toDrop) && toDrop) {
let tplTypeDeleteCreate; 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'); tplTypeDeleteCreate = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'types', 'specificType.template'), 'utf8');
} }
// Template: delete a specific column. // Create when it's not an onlyDrop action
models[modelName].attributes[attribute].delete.drop = _.unescape(_.template(tplTypeDelete)({ if (_.isUndefined(onlyDrop) || onlyDrop === false) {
tableName: modelName, // Template: delete a specific column.
attribute: attribute models[modelName].attributes[attribute].delete.drop = _.unescape(_.template(tplTypeDelete)({
})); tableName: modelName,
attribute: attribute
}));
}
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplTypeDeleteCreate)({ models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplTypeDeleteCreate)({
tableName: modelName, tableName: modelName,