From 54512c6049036766f607b890c1b87c4a4e69caf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Tue, 2 Mar 2021 17:09:35 +0100 Subject: [PATCH] split code into files --- .../functions/migrations/field/index.js | 42 ++++++++ .../migrateForBookshelf.js} | 99 +------------------ .../migrations/field/migrateForMongoose.js | 41 ++++++++ .../functions/migrations/field/utils.js | 27 +++++ 4 files changed, 112 insertions(+), 97 deletions(-) create mode 100644 packages/strapi-plugin-i18n/config/functions/migrations/field/index.js rename packages/strapi-plugin-i18n/config/functions/migrations/{field.js => field/migrateForBookshelf.js} (55%) create mode 100644 packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForMongoose.js create mode 100644 packages/strapi-plugin-i18n/config/functions/migrations/field/utils.js diff --git a/packages/strapi-plugin-i18n/config/functions/migrations/field/index.js b/packages/strapi-plugin-i18n/config/functions/migrations/field/index.js new file mode 100644 index 0000000000..1bd866f512 --- /dev/null +++ b/packages/strapi-plugin-i18n/config/functions/migrations/field/index.js @@ -0,0 +1,42 @@ +'use strict'; + +const { difference, orderBy, intersection } = require('lodash/fp'); +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'); + const localeService = getService('locales'); + + if (!ctService.isLocalized(model)) { + 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; + } + + let locales = await localeService.find(); + locales = await localeService.setIsDefault(locales); + locales = orderBy(['isDefault', 'code'], ['desc', 'asc'])(locales); // Put default locale first + + if (model.orm === 'bookshelf') { + await migrateForBookshelf({ ORM, model, attributesToMigrate, locales }); + } else if (model.orm === 'mongoose') { + await migrateForMongoose({ model, attributesToMigrate, locales }); + } +}; + +const before = () => {}; + +module.exports = { + before, + after, +}; diff --git a/packages/strapi-plugin-i18n/config/functions/migrations/field.js b/packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForBookshelf.js similarity index 55% rename from packages/strapi-plugin-i18n/config/functions/migrations/field.js rename to packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForBookshelf.js index 1d32d62ce8..90da51e16f 100644 --- a/packages/strapi-plugin-i18n/config/functions/migrations/field.js +++ b/packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForBookshelf.js @@ -1,33 +1,9 @@ 'use strict'; -const { difference, pick, orderBy, prop, intersection } = require('lodash/fp'); -const { getService } = require('../../../utils'); +const { shouldBeProcesseed, getUpdatesInfo } = require('./utils'); const BATCH_SIZE = 1000; -// Common functions - -const shouldBeProcesseed = processedLocaleCodes => entry => { - return ( - entry.localizations.length > 1 && - intersection(entry.localizations.map(prop('locale')), processedLocaleCodes).length === 0 - ); -}; - -const getUpdatesInfo = ({ entriesToProcess, locale, attributesToMigrate }) => { - const updates = []; - for (const entry of entriesToProcess) { - const attributesValues = pick(attributesToMigrate, entry); - const entriesIdsToUpdate = entry.localizations - .filter(related => related.locale !== locale.code) - .map(prop('id')); - updates.push({ entriesIdsToUpdate, attributesValues }); - } - return updates; -}; - -// Bookshelf - const TMP_TABLE_NAME = '__tmp__i18n_field_migration'; const batchInsertInTmpTable = async (updatesInfo, trx) => { @@ -146,75 +122,4 @@ const migrateForBookshelf = async ({ ORM, model, attributesToMigrate, locales }) } }; -// Mongoose - -const migrateForMongoose = async ({ model, attributesToMigrate, locales }) => { - const processedLocaleCodes = []; - for (const locale of locales) { - let batchCount = BATCH_SIZE; - let lastId; - while (batchCount === BATCH_SIZE) { - const findParams = { locale: locale.code }; - if (lastId) { - findParams._id = { $gt: lastId }; - } - - const batch = await model - .find(findParams, [...attributesToMigrate, 'locale', 'localizations']) - .sort({ _id: 1 }) - .limit(BATCH_SIZE); - - if (batch.length > 0) { - lastId = batch[batch.length - 1]._id; - } - batchCount = batch.length; - - const entriesToProcess = batch.filter(shouldBeProcesseed); - - const updatesInfo = getUpdatesInfo({ entriesToProcess, locale, attributesToMigrate }); - const updates = updatesInfo.map(({ entriesIdsToUpdate, attributesValues }) => ({ - updateMany: { filter: { _id: { $in: entriesIdsToUpdate } }, update: attributesValues }, - })); - - await model.bulkWrite(updates); - } - processedLocaleCodes.push(locale.code); - } -}; - -// 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'); - const localeService = getService('locales'); - - if (!ctService.isLocalized(model)) { - return; - } - - const localizedAttributes = ctService.getLocalizedAttributes(definition); - const prevLocalizedAttributes = ctService.getLocalizedAttributes(previousDefinition); - const attributesDisabled = difference(prevLocalizedAttributes, localizedAttributes); - const attributesToMigrate = intersection(Object.keys(definition.attributes), attributesDisabled); - - if (attributesToMigrate.length === 0) { - return; - } - - let locales = await localeService.find(); - locales = await localeService.setIsDefault(locales); - locales = orderBy(['isDefault', 'code'], ['desc', 'asc'])(locales); // Put default locale first - - if (model.orm === 'bookshelf') { - await migrateForBookshelf({ ORM, model, attributesToMigrate, locales }); - } else if (model.orm === 'mongoose') { - await migrateForMongoose({ model, attributesToMigrate, locales }); - } - throw new Error('Done'); -}; - -const before = () => {}; - -module.exports = { - before, - after, -}; +module.exports = migrateForBookshelf; diff --git a/packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForMongoose.js b/packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForMongoose.js new file mode 100644 index 0000000000..8bbe44bc65 --- /dev/null +++ b/packages/strapi-plugin-i18n/config/functions/migrations/field/migrateForMongoose.js @@ -0,0 +1,41 @@ +'use strict'; + +const { shouldBeProcesseed, getUpdatesInfo } = require('./utils'); + +const BATCH_SIZE = 1000; + +const migrateForMongoose = async ({ model, attributesToMigrate, locales }) => { + const processedLocaleCodes = []; + for (const locale of locales) { + let batchCount = BATCH_SIZE; + let lastId; + while (batchCount === BATCH_SIZE) { + const findParams = { locale: locale.code }; + if (lastId) { + findParams._id = { $gt: lastId }; + } + + const batch = await model + .find(findParams, [...attributesToMigrate, 'locale', 'localizations']) + .sort({ _id: 1 }) + .limit(BATCH_SIZE); + + if (batch.length > 0) { + lastId = batch[batch.length - 1]._id; + } + batchCount = batch.length; + + const entriesToProcess = batch.filter(shouldBeProcesseed); + + const updatesInfo = getUpdatesInfo({ entriesToProcess, locale, attributesToMigrate }); + const updates = updatesInfo.map(({ entriesIdsToUpdate, attributesValues }) => ({ + updateMany: { filter: { _id: { $in: entriesIdsToUpdate } }, update: attributesValues }, + })); + + await model.bulkWrite(updates); + } + processedLocaleCodes.push(locale.code); + } +}; + +module.exports = migrateForMongoose; diff --git a/packages/strapi-plugin-i18n/config/functions/migrations/field/utils.js b/packages/strapi-plugin-i18n/config/functions/migrations/field/utils.js new file mode 100644 index 0000000000..c5db345031 --- /dev/null +++ b/packages/strapi-plugin-i18n/config/functions/migrations/field/utils.js @@ -0,0 +1,27 @@ +'use strict'; + +const { pick, prop, intersection } = require('lodash/fp'); + +const shouldBeProcesseed = processedLocaleCodes => entry => { + return ( + entry.localizations.length > 1 && + intersection(entry.localizations.map(prop('locale')), processedLocaleCodes).length === 0 + ); +}; + +const getUpdatesInfo = ({ entriesToProcess, locale, attributesToMigrate }) => { + const updates = []; + for (const entry of entriesToProcess) { + const attributesValues = pick(attributesToMigrate, entry); + const entriesIdsToUpdate = entry.localizations + .filter(related => related.locale !== locale.code) + .map(prop('id')); + updates.push({ entriesIdsToUpdate, attributesValues }); + } + return updates; +}; + +module.exports = { + shouldBeProcesseed, + getUpdatesInfo, +};