-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create locale column when migrating from v4 (#20261)
* fix: create locale column * fix: typo * fix: rewrite comment * feat: create locale column migration
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/core/database/src/migrations/internal-migrations/5.0.0-03-locale.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters