Skip to content

Commit

Permalink
fix: create locale column when migrating from v4 (#20261)
Browse files Browse the repository at this point in the history
* fix: create locale column

* fix: typo

* fix: rewrite comment

* feat: create locale column migration
  • Loading branch information
Marc-Roig committed May 24, 2024
1 parent cffa227 commit eb316ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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');
},
};
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,4 +15,5 @@ import { renameIdentifiersLongerThanMaxLength } from './5.0.0-01-convert-identif
export const internalMigrations: Migration[] = [
renameIdentifiersLongerThanMaxLength,
createdDocumentId,
createdLocale,
];

0 comments on commit eb316ac

Please sign in to comment.