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-03-22 15:50:33 +01:00
|
|
|
const utilsModels = require('strapi/lib/configuration/hooks/models/utils/');
|
2016-03-18 11:12:50 +01:00
|
|
|
const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Relationship templates
|
|
|
|
*/
|
|
|
|
|
2016-04-14 12:00:39 +02:00
|
|
|
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop, history) {
|
2016-03-18 11:12:50 +01:00
|
|
|
let tplRelationUp;
|
|
|
|
let tplRelationDown;
|
|
|
|
|
2016-04-14 13:18:17 +02:00
|
|
|
const infos = toDrop ? utilsModels.getNature(details, attribute, history) : utilsModels.getNature(details, attribute, models);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
_.set(models[modelName].attributes, attribute + '.create', {});
|
|
|
|
_.set(models[modelName].attributes, attribute + '.delete', {});
|
|
|
|
|
2016-03-18 11:12:50 +01:00
|
|
|
// If it's a "one-to-one" relationship.
|
|
|
|
if (infos.verbose === 'hasOne') {
|
2016-03-23 18:11:10 +01:00
|
|
|
// Force singular foreign key.
|
2016-03-18 11:12:50 +01:00
|
|
|
details.attribute = pluralize.singular(details.model);
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// Define PK column.
|
2016-03-22 15:50:33 +01:00
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
|
|
|
// Template: create a new column thanks to the attribute's relation.
|
|
|
|
// Simply make a `create` template for this attribute wich will be added
|
|
|
|
// to the table template-- either `./builder/tables/selectTable` or
|
|
|
|
// `./builder/tables/createTableIfNotExists`.
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'hasOne.template'), 'utf8');
|
2016-03-23 18:11:10 +01:00
|
|
|
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
|
2016-03-18 11:12:50 +01:00
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Template: drop the column.
|
|
|
|
// Simply make a `delete` template for this attribute wich drop the column
|
|
|
|
// with the `./builder/columns/dropColumn` template.
|
2016-04-07 16:36:21 +02:00
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
|
2016-03-23 18:11:10 +01:00
|
|
|
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplRelationDown)({
|
2016-03-18 11:12:50 +01:00
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
2016-03-23 18:11:10 +01:00
|
|
|
} else if (infos.verbose === 'belongsTo') {
|
|
|
|
// Force singular foreign key.
|
2016-03-18 11:12:50 +01:00
|
|
|
details.attribute = pluralize.singular(details.model);
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// Define PK column.
|
2016-03-22 15:50:33 +01:00
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
|
|
|
if (infos.nature === 'oneToMany' || infos.nature === 'oneWay') {
|
|
|
|
// Template: create a new column thanks to the attribute's relation.
|
|
|
|
// Simply make a `create` template for this attribute wich will be added
|
|
|
|
// to the table template-- either `./builder/tables/selectTable` or
|
|
|
|
// `./builder/tables/createTableIfNotExists`.
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
2016-03-23 18:11:10 +01:00
|
|
|
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
|
2016-03-18 11:12:50 +01:00
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details,
|
|
|
|
nature: infos.nature
|
|
|
|
}));
|
2016-04-07 16:36:21 +02:00
|
|
|
|
|
|
|
// Template: drop the column.
|
|
|
|
// Simply make a `delete` template for this attribute wich drop the column
|
|
|
|
// with the `./builder/columns/dropColumn` template.
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn.template'), 'utf8');
|
|
|
|
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
2016-03-18 11:12:50 +01:00
|
|
|
} else {
|
|
|
|
// Template: create a new column thanks to the attribute's relation.
|
|
|
|
// Simply make a `create` template for this attribute wich will be added
|
|
|
|
// to the table template-- either `./builder/tables/selectTable` or
|
|
|
|
// `./builder/tables/createTableIfNotExists`.
|
|
|
|
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo-unique.template'), 'utf8');
|
2016-03-23 18:11:10 +01:00
|
|
|
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
|
2016-03-18 11:12:50 +01:00
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
2016-04-07 15:43:20 +02:00
|
|
|
|
2016-04-07 16:36:21 +02:00
|
|
|
// Template: drop the column.
|
|
|
|
// Simply make a `delete` template for this attribute wich drop the column
|
|
|
|
// with the `./builder/columns/dropColumn` template.
|
|
|
|
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
|
|
|
|
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplRelationDown)({
|
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
|
|
|
}
|
2016-03-23 18:11:10 +01: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
|
|
|
let relationship;
|
|
|
|
let relationTable;
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-04-14 13:18:17 +02:00
|
|
|
if (!onlyDrop) {
|
|
|
|
// Save the relationship.
|
|
|
|
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.
|
|
|
|
relationTable = _.map(_.sortBy([relationship, details], 'collection'), function (table) {
|
|
|
|
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-03-23 18:11:10 +01:00
|
|
|
// Avoid to create table both times.
|
2016-04-14 12:00:39 +02:00
|
|
|
if (!models.hasOwnProperty(relationTable) || !_.isEmpty(_.get(models, relationTable + '.up.drop'))) {
|
2016-04-12 15:20:13 +02:00
|
|
|
// Set objects
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
|
|
|
|
_.set(models, relationTable + '.up.others', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.up.drop'))) {
|
|
|
|
_.set(models, relationTable + '.up.drop', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.down.others'))) {
|
|
|
|
_.set(models, relationTable + '.down.others', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.down.drop'))) {
|
|
|
|
_.set(models, relationTable + '.down.drop', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.attributes'))) {
|
|
|
|
_.set(models, relationTable + '.attributes', {});
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
// Create relationships table for many-to-many.
|
|
|
|
models[relationTable].up.others += _.unescape(_.template(tplTableUp)({
|
|
|
|
models: models,
|
|
|
|
tableName: relationTable,
|
|
|
|
details: details,
|
|
|
|
relationship: relationship
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (_.isUndefined(_.get(models, 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.
|
|
|
|
models[relationTable].down.others += _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Remove foreign key current relationships table before drop the table on migration rollback.
|
|
|
|
models[relationTable].attributes.fk = {
|
|
|
|
delete: {
|
|
|
|
drop: _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: details.attribute + '_' + details.column
|
|
|
|
})) + _.unescape(_.template(tplFKDown)({
|
|
|
|
attribute: relationship.attribute + '_' + relationship.column
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
models[relationTable].down.drop += _.unescape(_.template(tplSelectTableDown)({
|
|
|
|
models: models,
|
|
|
|
tableName: relationTable,
|
|
|
|
attributes: models[relationTable].attributes,
|
|
|
|
toDrop: true
|
|
|
|
}));
|
2016-04-14 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const dropMigrationTable = _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Eliminate duplicate
|
|
|
|
if (models[relationTable].down.drop.indexOf(dropMigrationTable) === -1) {
|
2016-04-14 12:00:39 +02:00
|
|
|
// Drop current relationships table on migration rollback.
|
2016-04-14 14:52:41 +02:00
|
|
|
models[relationTable].down.drop += dropMigrationTable;
|
2016-04-07 18:15:58 +02:00
|
|
|
}
|
2016-04-12 15:20:13 +02:00
|
|
|
} 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');
|
|
|
|
|
2016-04-14 12:00:39 +02:00
|
|
|
// Save the old relationship.
|
2016-04-14 13:18:17 +02:00
|
|
|
const oldRelationship = history[details.collection].attributes[details.via];
|
2016-04-14 12:00:39 +02:00
|
|
|
|
|
|
|
// 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);
|
2016-04-14 13:18:17 +02:00
|
|
|
details.attribute = pluralize.singular(details.collection);
|
2016-04-14 12:00:39 +02:00
|
|
|
|
|
|
|
// Define PK column.
|
|
|
|
oldRelationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
|
2016-04-14 13:18:17 +02:00
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
|
|
|
|
2016-04-14 12:00:39 +02:00
|
|
|
|
2016-04-14 13:18:17 +02:00
|
|
|
const dropMigrationTable = _.unescape(_.template(tplTableDown)({
|
2016-04-14 12:00:39 +02:00
|
|
|
tableName: oldRelationTable || relationTable
|
2016-04-12 15:20:13 +02:00
|
|
|
}));
|
|
|
|
|
2016-04-14 14:52:41 +02:00
|
|
|
// Eliminate duplicate
|
2016-04-14 13:18:17 +02:00
|
|
|
if (models[relationTable].up.drop.indexOf(dropMigrationTable) === -1) {
|
|
|
|
// Drop current relationships table on migration run.
|
|
|
|
models[relationTable].up.drop += _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: oldRelationTable || relationTable
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:20:13 +02:00
|
|
|
// Drop current relationships table on migration rollback.
|
|
|
|
// This allows us to identify, if this is an update on already existing many-to-many relationship or if this is a basic addition.
|
|
|
|
if (_.isUndefined(_.get(models, relationTable + '.attributes.fk'))) {
|
|
|
|
models[relationTable].attributes.fk = {
|
|
|
|
delete: {
|
|
|
|
drop: _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
};
|
2016-04-14 13:18:17 +02:00
|
|
|
} else {
|
|
|
|
// Create previous relationships table on migration rollback.
|
|
|
|
models[relationTable].down.others += _.unescape(_.template(tplTableUp)({
|
|
|
|
models: models,
|
|
|
|
tableName: oldRelationTable || relationTable,
|
|
|
|
details: details,
|
|
|
|
relationship: oldRelationship || relationship
|
|
|
|
}));
|
2016-04-12 15:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|