38 lines
1.2 KiB
JavaScript
Raw Normal View History

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');
2021-03-18 18:27:15 +01:00
const migrateForMongoose = require('./migrate-for-mongoose');
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 }) => {
const ctService = getService('content-types');
if (!ctService.isLocalized(model) || !ctService.isLocalized(previousDefinition)) {
2021-03-02 17:09:35 +01:00
return;
}
2021-03-18 18:53:29 +01:00
const localizedAttributes = ctService.getLocalizedAttributes(definition);
const prevLocalizedAttributes = ctService.getLocalizedAttributes(previousDefinition);
2021-03-02 17:09:35 +01:00
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,
};