This commit is contained in:
Pierre Noël 2021-03-15 18:51:32 +01:00
parent b39aa3c8bd
commit ce84bd0325
3 changed files with 56 additions and 14 deletions

View File

@ -1,6 +1,6 @@
'use strict';
const { difference, orderBy, intersection } = require('lodash/fp');
const { difference, intersection } = require('lodash/fp');
const { getService } = require('../../../../utils');
const migrateForMongoose = require('./migrateForMongoose');
const migrateForBookshelf = require('./migrateForBookshelf');
@ -8,9 +8,8 @@ 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)) {
if (!ctService.isLocalized(model) || !previousDefinition) {
return;
}
@ -23,16 +22,11 @@ const after = async ({ model, definition, previousDefinition, ORM }) => {
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 });
await migrateForBookshelf({ ORM, model, attributesToMigrate });
} else if (model.orm === 'mongoose') {
await migrateForMongoose({ model, attributesToMigrate, locales });
await migrateForMongoose({ model, attributesToMigrate });
}
throw new Error('pouet');
};
const before = () => {};

View File

@ -1,7 +1,7 @@
'use strict';
const { singular } = require('pluralize');
const { has, omit, pick } = require('lodash/fp');
const { has, omit, pick, orderBy } = require('lodash/fp');
const { shouldBeProcessed, getUpdatesInfo } = require('./utils');
const BATCH_SIZE = 1000;
@ -60,7 +60,30 @@ const createTmpTable = async ({ ORM, attributesToMigrate, model }) => {
const deleteTmpTable = ({ ORM }) => ORM.knex.schema.dropTableIfExists(TMP_TABLE_NAME);
const migrateForBookshelf = async ({ ORM, model, attributesToMigrate, locales }) => {
const getSortedLocales = async ORM => {
let defaultLocale;
try {
const defaultLocaleRow = await ORM.knex('core_store')
.select('value')
.where({ key: 'plugin_i18n_default_locale' });
defaultLocale = defaultLocaleRow[0].value;
} catch (e) {
throw new Error("Could not migrate because the default locale doesn't exist");
}
let locales;
try {
locales = await ORM.knex(strapi.plugins.i18n.models.locale.collectionName).select('code');
} catch (e) {
throw new Error('Could not migrate because no locale exist');
}
locales.forEach(locale => (locale.isDefault = locale.code === defaultLocale));
return orderBy(['isDefault', 'code'], ['desc', 'asc'])(locales); // Put default locale first
};
const migrateForBookshelf = async ({ ORM, model, attributesToMigrate }) => {
const locales = await getSortedLocales(ORM);
const localizationAssoc = model.associations.find(a => a.alias === 'localizations');
const localizationTableName = localizationAssoc.tableCollectionName;
@ -125,7 +148,7 @@ const migrateForBookshelf = async ({ ORM, model, attributesToMigrate, locales })
offset += BATCH_SIZE;
const entriesToProcess = batch.filter(shouldBeProcessed(processedLocaleCodes));
const entriesToProcess = entries.filter(shouldBeProcessed(processedLocaleCodes));
const updatesInfo = getUpdatesInfo({ entriesToProcess, attributesToMigrate });
if (isPgOrMysql) {

View File

@ -1,10 +1,35 @@
'use strict';
const { orderBy } = require('lodash/fp');
const { shouldBeProcessed, getUpdatesInfo } = require('./utils');
const BATCH_SIZE = 1000;
const migrateForMongoose = async ({ model, attributesToMigrate, locales }) => {
const getSortedLocales = async () => {
let defaultLocale;
try {
const defaultLocaleRow = await strapi.models['core_store'].findOne({
key: 'plugin_i18n_default_locale',
});
defaultLocale = JSON.parse(defaultLocaleRow.value);
} catch (e) {
throw new Error("Could not migrate because the default locale doesn't exist");
}
let locales;
try {
strapi.models;
locales = await strapi.plugins.i18n.models.locale.find();
} catch (e) {
throw new Error('Could not migrate because no locale exist');
}
locales.forEach(locale => (locale.isDefault = locale.code === defaultLocale));
return orderBy(['isDefault', 'code'], ['desc', 'asc'])(locales); // Put default locale first
};
const migrateForMongoose = async ({ model, attributesToMigrate }) => {
const locales = await getSortedLocales();
const processedLocaleCodes = [];
for (const locale of locales) {
let batchCount = BATCH_SIZE;