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
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/core/core/src/migrations/database/5.0.0-discard-drafts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-continue */
import { isNil } from 'lodash/fp';

import type { UID } from '@strapi/types';
import type { Database, Migration } from '@strapi/database';
import { async, contentTypes } from '@strapi/utils';
Expand Down Expand Up @@ -47,6 +49,19 @@ async function* getBatchToDiscard({
}
}

/**
* This migration makes use of the document service,
* which assumes the `locale` column always exists.
* In v4, that was not the case, for content types with i18n disabled.
*
* 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');
});
};

const migrateUp = async (trx: Knex, db: Database) => {
for (const meta of db.metadata.values()) {
const hasTable = await trx.schema.hasTable(meta.tableName);
Expand All @@ -62,6 +77,11 @@ const migrateUp = async (trx: Knex, db: Database) => {
continue;
}

// Create locale column if it doesn't exist
if (isNil(meta.attributes.locale)) {
await createLocaleColumn(trx, meta.tableName);
}

const discardDraft = async (entry: DocumentVersion) =>
strapi
.documents(uid)
Expand Down
Loading