2021-03-02 17:09:35 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-04-08 18:05:12 +02:00
|
|
|
const { difference, keys, intersection, isEmpty } = require('lodash/fp');
|
2021-06-09 18:10:44 +02:00
|
|
|
const { getService } = require('../../utils');
|
2021-06-28 22:37:19 +02:00
|
|
|
|
2021-03-18 18:27:15 +01:00
|
|
|
const migrateForBookshelf = require('./migrate-for-bookshelf');
|
2021-03-02 17:09:35 +01:00
|
|
|
|
|
|
|
// Migration when i18n is disabled on a field of a content-type that have i18n enabled
|
|
|
|
const after = async ({ model, definition, previousDefinition, ORM }) => {
|
2021-04-09 11:59:40 +02:00
|
|
|
const { isLocalizedContentType, getLocalizedAttributes } = getService('content-types');
|
2021-03-02 17:09:35 +01:00
|
|
|
|
2021-04-09 11:59:40 +02:00
|
|
|
if (!isLocalizedContentType(model) || !isLocalizedContentType(previousDefinition)) {
|
2021-03-02 17:09:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-09 11:59:40 +02:00
|
|
|
const localizedAttributes = getLocalizedAttributes(definition);
|
|
|
|
const prevLocalizedAttributes = getLocalizedAttributes(previousDefinition);
|
2021-03-02 17:09:35 +01:00
|
|
|
const attributesDisabled = difference(prevLocalizedAttributes, localizedAttributes);
|
2021-04-09 11:09:34 +02:00
|
|
|
const attributesToMigrate = intersection(keys(definition.attributes), attributesDisabled);
|
2021-03-02 17:09:35 +01:00
|
|
|
|
2021-04-09 11:09:34 +02:00
|
|
|
if (isEmpty(attributesToMigrate)) {
|
2021-03-02 17:09:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-28 22:37:19 +02:00
|
|
|
await migrateForBookshelf({ ORM, model, attributesToMigrate });
|
2021-03-02 17:09:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const before = () => {};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
};
|