2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
const pluralize = require('pluralize');
|
|
|
|
|
2016-03-23 15:18:37 +01:00
|
|
|
// Collections utils.
|
2016-10-31 12:51:05 +01:00
|
|
|
const utilsModels = require('strapi-utils').models;
|
2016-03-18 11:12:50 +01:00
|
|
|
const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
|
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Template builder.
|
|
|
|
const selectTable = require('./selectTable');
|
|
|
|
|
2016-03-18 11:12:50 +01:00
|
|
|
/**
|
|
|
|
* Relationship templates
|
|
|
|
*/
|
|
|
|
|
2016-07-11 13:03:35 +02:00
|
|
|
module.exports = (rootModels, modelName, details, attribute, toDrop, onlyDrop, history) => {
|
2016-03-18 11:12:50 +01:00
|
|
|
let tplRelationUp;
|
|
|
|
let tplRelationDown;
|
2016-04-15 16:31:27 +02:00
|
|
|
let infos = {};
|
|
|
|
let oldInfos = {};
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (!onlyDrop && toDrop) {
|
|
|
|
infos = utilsModels.getNature(details, attribute, rootModels);
|
|
|
|
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
|
|
|
|
|
2016-04-19 17:29:19 +02:00
|
|
|
const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);
|
2016-04-15 16:31:27 +02:00
|
|
|
|
|
|
|
if (isDifferentVerbose) {
|
|
|
|
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
|
|
|
|
handleRelation(infos, rootModels, modelName, details, attribute);
|
2016-03-18 11:12:50 +01:00
|
|
|
} else {
|
2016-04-15 16:31:27 +02:00
|
|
|
handleRelation(infos, rootModels, modelName, details, attribute, true, true);
|
|
|
|
}
|
2016-04-19 17:07:21 +02:00
|
|
|
} else if (onlyDrop || toDrop) {
|
2016-04-15 16:31:27 +02:00
|
|
|
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
|
|
|
|
|
|
|
|
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
|
|
|
|
} else {
|
|
|
|
infos = utilsModels.getNature(details, attribute, rootModels);
|
|
|
|
|
|
|
|
handleRelation(infos, rootModels, modelName, details, attribute);
|
|
|
|
}
|
|
|
|
|
2016-04-19 17:29:19 +02:00
|
|
|
function handleRelation(infos, models, modelName, details, attribute, toDrop, onlyDrop) {
|
2016-04-19 17:07:21 +02:00
|
|
|
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.create'))) {
|
|
|
|
_.set(rootModels[modelName].attributes, attribute + '.create', {
|
|
|
|
drop: '',
|
|
|
|
others: ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.delete'))) {
|
|
|
|
_.set(rootModels[modelName].attributes, attribute + '.delete', {
|
|
|
|
drop: '',
|
|
|
|
others: ''
|
|
|
|
});
|
|
|
|
}
|
2016-04-15 16:31:27 +02:00
|
|
|
|
|
|
|
// If it's a "one-to-one" relationship.
|
|
|
|
if (infos.verbose === 'hasOne') {
|
|
|
|
// Force singular foreign key.
|
|
|
|
details.attribute = pluralize.singular(details.model);
|
|
|
|
|
|
|
|
// Define PK column.
|
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
|
|
|
|
2016-04-19 17:07:21 +02:00
|
|
|
if (!toDrop) {
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'hasOne.template'), 'utf8');
|
|
|
|
models[modelName].attributes[attribute].create.others += _.unescape(_.template(tplRelationUp)({
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-15 16:31:27 +02:00
|
|
|
}));
|
|
|
|
|
2016-04-19 17:07:21 +02:00
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
|
|
|
|
models[modelName].attributes[attribute].delete.others += _.unescape(_.template(tplRelationDown)({
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-15 16:31:27 +02:00
|
|
|
}));
|
|
|
|
} else {
|
2016-04-19 17:07:21 +02:00
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
|
|
|
|
models[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-15 16:31:27 +02:00
|
|
|
}));
|
|
|
|
|
2016-04-19 17:07:21 +02:00
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'hasOne.template'), 'utf8');
|
|
|
|
models[modelName].attributes[attribute].delete.drop += _.unescape(_.template(tplRelationUp)({
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-15 16:31:27 +02:00
|
|
|
}));
|
|
|
|
}
|
2016-04-19 17:07:21 +02:00
|
|
|
} else if (infos.verbose === 'belongsTo') {
|
|
|
|
// Force singular foreign key.
|
|
|
|
details.attribute = pluralize.singular(details.model);
|
|
|
|
|
|
|
|
// Define PK column.
|
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
|
|
|
|
|
|
|
if (infos.nature === 'oneToMany' || infos.nature === 'oneWay') {
|
|
|
|
if (!toDrop) {
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].create.others += _.unescape(_.template(tplRelationUp)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details,
|
2016-04-19 17:07:21 +02:00
|
|
|
nature: infos.nature
|
|
|
|
}));
|
|
|
|
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].delete.drop += _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-19 17:07:21 +02:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropForeign.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-19 17:07:21 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].delete.others += _.unescape(_.template(tplRelationUp)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details,
|
2016-04-19 17:07:21 +02:00
|
|
|
nature: infos.nature
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!toDrop) {
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo-unique.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].create.others += _.unescape(_.template(tplRelationUp)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-19 17:07:21 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].delete.drop += _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-19 17:07:21 +02:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-19 17:07:21 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].delete.others += _.unescape(_.template(tplRelationUp)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details,
|
2016-04-19 17:07:21 +02:00
|
|
|
nature: infos.nature
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (infos.verbose === 'hasMany') {
|
|
|
|
if (toDrop) {
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropForeign.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details
|
2016-04-19 17:07:21 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
|
|
|
rootModels[modelName].attributes[attribute].delete.others += _.unescape(_.template(tplRelationUp)({
|
|
|
|
tableName: modelName,
|
2016-11-07 16:31:34 +01:00
|
|
|
attribute,
|
|
|
|
details,
|
2016-04-19 17:07:21 +02:00
|
|
|
nature: infos.nature
|
|
|
|
}));
|
|
|
|
}
|
2016-04-15 16:31:27 +02:00
|
|
|
} else if (infos.verbose === 'belongsToMany') {
|
|
|
|
// Otherwise if it's a "many-to-many" relationship.
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-04-14 13:18:17 +02:00
|
|
|
// Save the relationship.
|
2016-04-15 16:31:27 +02:00
|
|
|
const relationship = models[details.collection].attributes[details.via];
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-04-14 13:18:17 +02:00
|
|
|
// Construct relation table name.
|
2016-07-11 13:03:35 +02:00
|
|
|
const relationTable = _.map(_.sortBy([relationship, details], 'collection'), table => {
|
2016-04-14 13:18:17 +02:00
|
|
|
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
|
|
|
|
}).join('__');
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-04-14 13:18:17 +02:00
|
|
|
// Force singular foreign key.
|
|
|
|
relationship.attribute = pluralize.singular(relationship.collection);
|
|
|
|
details.attribute = pluralize.singular(details.collection);
|
|
|
|
|
|
|
|
// Define PK column.
|
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
|
|
|
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Avoid to create table both times.
|
|
|
|
if (!rootModels.hasOwnProperty(relationTable) || !_.isEmpty(_.get(rootModels, relationTable + '.up.drop'))) {
|
|
|
|
// Set objects
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
|
|
|
|
_.set(rootModels, relationTable + '.up.others', '');
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels, relationTable + '.up.drop'))) {
|
|
|
|
_.set(rootModels, relationTable + '.up.drop', '');
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels, relationTable + '.down.others'))) {
|
|
|
|
_.set(rootModels, relationTable + '.down.others', '');
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels, relationTable + '.down.drop'))) {
|
|
|
|
_.set(rootModels, relationTable + '.down.drop', '');
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels, relationTable + '.attributes'))) {
|
|
|
|
_.set(rootModels, relationTable + '.attributes', {});
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (!toDrop) {
|
|
|
|
// 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');
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Create relationships table for many-to-many.
|
|
|
|
rootModels[relationTable].up.others += _.unescape(_.template(tplTableUp)({
|
2016-11-07 16:31:34 +01:00
|
|
|
models,
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: relationTable,
|
2016-11-07 16:31:34 +01:00
|
|
|
details,
|
|
|
|
relationship
|
2016-04-15 16:31:27 +02:00
|
|
|
}));
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels, relationTable + '.attributes.fk'))) {
|
|
|
|
// Load templates.
|
|
|
|
const tplFKDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropForeign.template'), 'utf8');
|
|
|
|
const tplSelectTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'select', 'down.template'), 'utf8');
|
|
|
|
|
|
|
|
// Drop current relationships table on migration rollback.
|
|
|
|
rootModels[relationTable].down.others += _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Remove foreign key current relationships table before drop the table on migration rollback.
|
|
|
|
rootModels[relationTable].attributes.fk = {
|
|
|
|
delete: {
|
|
|
|
drop: _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: details.attribute + '_' + details.column
|
|
|
|
})) + _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: relationship.attribute + '_' + relationship.column
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
rootModels[relationTable].down.drop += _.unescape(_.template(tplSelectTableDown)({
|
2016-11-07 16:31:34 +01:00
|
|
|
models,
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: relationTable,
|
|
|
|
attributes: models[relationTable].attributes,
|
|
|
|
toDrop: true
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
const dropMigrationTable = _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].down.drop.indexOf(dropMigrationTable) === -1) {
|
|
|
|
// Drop current relationships table on migration rollback.
|
|
|
|
rootModels[relationTable].down.drop += dropMigrationTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (onlyDrop) {
|
2016-04-12 15:20:13 +02:00
|
|
|
// Load templates.
|
2016-04-15 16:31:27 +02:00
|
|
|
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');
|
2016-04-12 15:20:13 +02:00
|
|
|
const tplFKDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropForeign.template'), 'utf8');
|
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
const dropMigrationTable = _.unescape(_.template(tplTableDown)({
|
2016-04-12 15:20:13 +02:00
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels[relationTable].attributes, 'fk.delete')) && _.get(rootModels[modelName].newAttributes[attribute], 'isRemoved') !== true) {
|
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].up.drop.indexOf(dropMigrationTable) === -1) {
|
|
|
|
// Drop current relationships table on migration run.
|
|
|
|
rootModels[relationTable].up.drop += _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
2016-04-12 15:20:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// We have to this to be in the up template loop
|
|
|
|
_.set(rootModels[relationTable], 'newAttributes.fk', {});
|
|
|
|
_.set(rootModels[relationTable].attributes, 'fk', {
|
|
|
|
delete: {
|
|
|
|
drop: ''
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Drop first FK on migration relation table.
|
|
|
|
const dropMigrationFK1 = _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: details.attribute + '_' + details.column
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].attributes.fk.delete.drop.indexOf(dropMigrationFK1) === -1) {
|
|
|
|
// Remove foreign key current relationships table before drop the table on migration rollback.
|
|
|
|
rootModels[relationTable].attributes.fk.delete.drop += dropMigrationFK1;
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Drop first FK on migration relation table.
|
|
|
|
const dropMigrationFK2 = _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: relationship.attribute + '_' + relationship.column
|
|
|
|
}));
|
2016-04-14 12:00:39 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].attributes.fk.delete.drop.indexOf(dropMigrationFK2) === -1) {
|
|
|
|
rootModels[relationTable].attributes.fk.delete.drop += dropMigrationFK2;
|
|
|
|
}
|
2016-04-14 12:00:39 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Builder: select the table.
|
|
|
|
selectTable(rootModels, relationTable);
|
|
|
|
} else if (_.get(rootModels[modelName].newAttributes[attribute], 'isRemoved') === true) {
|
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].up.others.indexOf(dropMigrationTable) === -1) {
|
|
|
|
// Drop current relationships table on migration run.
|
|
|
|
rootModels[relationTable].up.others += _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
}
|
2016-04-14 12:00:39 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
if (_.isUndefined(_.get(rootModels[relationTable].attributes, 'fk.create'))) {
|
|
|
|
// We have to this to be in the up template loop
|
|
|
|
_.set(rootModels[relationTable], 'newAttributes.fk.create', {});
|
|
|
|
_.set(rootModels[relationTable].attributes, 'fk', {
|
|
|
|
create: {
|
|
|
|
drop: ''
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Drop first FK on migration relation table.
|
|
|
|
const dropMigrationFK1 = _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: details.attribute + '_' + details.column
|
|
|
|
}));
|
2016-04-14 13:18:17 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].attributes.fk.create.drop.indexOf(dropMigrationFK1) === -1) {
|
|
|
|
// Remove foreign key current relationships table before drop the table on migration rollback.
|
|
|
|
rootModels[relationTable].attributes.fk.create.drop += dropMigrationFK1;
|
|
|
|
}
|
2016-04-14 12:00:39 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Drop first FK on migration relation table.
|
|
|
|
const dropMigrationFK2 = _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: relationship.attribute + '_' + relationship.column
|
|
|
|
}));
|
2016-04-12 15:20:13 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].attributes.fk.create.drop.indexOf(dropMigrationFK2) === -1) {
|
|
|
|
rootModels[relationTable].attributes.fk.create.drop += dropMigrationFK2;
|
|
|
|
}
|
2016-04-14 13:18:17 +02:00
|
|
|
|
2016-04-15 16:31:27 +02:00
|
|
|
// Builder: select the table.
|
|
|
|
selectTable(rootModels, relationTable);
|
2016-04-12 15:20:13 +02:00
|
|
|
}
|
2016-04-15 16:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Eliminate duplicate
|
|
|
|
if (rootModels[relationTable].down.others.indexOf('createTableIfNotExists(\'' + relationTable + '\'') === -1) {
|
|
|
|
// Create previous relationships table on migration rollback.
|
|
|
|
rootModels[relationTable].down.others += _.unescape(_.template(tplTableUp)({
|
2016-11-07 16:31:34 +01:00
|
|
|
models,
|
2016-04-15 16:31:27 +02:00
|
|
|
tableName: relationTable || relationTable,
|
2016-11-07 16:31:34 +01:00
|
|
|
details,
|
|
|
|
relationship
|
2016-04-15 16:31:27 +02:00
|
|
|
}));
|
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|