diff --git a/packages/core/database/src/migrations/internal-migrations/5.0.0-03-locale.ts b/packages/core/database/src/migrations/internal-migrations/5.0.0-03-locale.ts new file mode 100644 index 00000000000..f5057ee86bf --- /dev/null +++ b/packages/core/database/src/migrations/internal-migrations/5.0.0-03-locale.ts @@ -0,0 +1,45 @@ +import type { Knex } from 'knex'; +import { isNil } from 'lodash/fp'; + +import type { Migration } from '../common'; + +/** + * In v4, content types with disabled i18n did not have any locale column. + * In v5, we need to add a `locale` column to all content types. + * Other downstream migrations will make use of this column. + * + * This function creates the `locale` column if it doesn't exist. + */ +const createLocaleColumn = async (db: Knex, tableName: string) => { + await db.schema.alterTable(tableName, (table) => { + table.string('locale'); + }); +}; + +export const createdLocale: Migration = { + name: '5.0.0-03-created-locale', + async up(knex, db) { + for (const meta of db.metadata.values()) { + const hasTable = await knex.schema.hasTable(meta.tableName); + + if (!hasTable) { + continue; + } + + // Ignore non-content types + const uid = meta.uid; + const model = strapi.getModel(uid); + if (!model) { + continue; + } + + // Create locale column if it doesn't exist + if (isNil(meta.attributes.locale)) { + await createLocaleColumn(knex, meta.tableName); + } + } + }, + async down() { + throw new Error('not implemented'); + }, +}; diff --git a/packages/core/database/src/migrations/internal-migrations/index.ts b/packages/core/database/src/migrations/internal-migrations/index.ts index e978d7beaac..41f3dd5210f 100644 --- a/packages/core/database/src/migrations/internal-migrations/index.ts +++ b/packages/core/database/src/migrations/internal-migrations/index.ts @@ -1,6 +1,7 @@ import type { Migration } from '../common'; import { createdDocumentId } from './5.0.0-02-document-id'; import { renameIdentifiersLongerThanMaxLength } from './5.0.0-01-convert-identifiers-long-than-max-length'; +import { createdLocale } from './5.0.0-03-locale'; /** * List of all the internal migrations. The array order will be the order in which they are executed. @@ -14,4 +15,5 @@ import { renameIdentifiersLongerThanMaxLength } from './5.0.0-01-convert-identif export const internalMigrations: Migration[] = [ renameIdentifiersLongerThanMaxLength, createdDocumentId, + createdLocale, ];