Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create locale column when migrating from v4 #20261

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
];