2021-03-02 17:09:35 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-03-15 18:51:32 +01:00
|
|
|
const { difference, intersection } = require('lodash/fp');
|
2021-03-02 17:09:35 +01:00
|
|
|
const { getService } = require('../../../../utils');
|
|
|
|
const migrateForMongoose = require('./migrateForMongoose');
|
|
|
|
const migrateForBookshelf = require('./migrateForBookshelf');
|
|
|
|
|
|
|
|
// Migration when i18n is disabled on a field of a content-type that have i18n enabled
|
|
|
|
const after = async ({ model, definition, previousDefinition, ORM }) => {
|
|
|
|
const ctService = getService('content-types');
|
|
|
|
|
2021-03-15 18:51:32 +01:00
|
|
|
if (!ctService.isLocalized(model) || !previousDefinition) {
|
2021-03-02 17:09:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const localizedAttributes = ctService.getLocalizedFields(definition);
|
|
|
|
const prevLocalizedAttributes = ctService.getLocalizedFields(previousDefinition);
|
|
|
|
const attributesDisabled = difference(prevLocalizedAttributes, localizedAttributes);
|
|
|
|
const attributesToMigrate = intersection(Object.keys(definition.attributes), attributesDisabled);
|
|
|
|
|
|
|
|
if (attributesToMigrate.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.orm === 'bookshelf') {
|
2021-03-15 18:51:32 +01:00
|
|
|
await migrateForBookshelf({ ORM, model, attributesToMigrate });
|
2021-03-02 17:09:35 +01:00
|
|
|
} else if (model.orm === 'mongoose') {
|
2021-03-15 18:51:32 +01:00
|
|
|
await migrateForMongoose({ model, attributesToMigrate });
|
2021-03-02 17:09:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const before = () => {};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
};
|