From 1fba5ebf945268fb7db42d0ceac82e2b246da34d Mon Sep 17 00:00:00 2001 From: Brainslug Date: Tue, 2 May 2023 21:10:19 +0200 Subject: [PATCH] Load cjs custom migrations (#18385) --- .changeset/wicked-scissors-confess.md | 5 +++++ api/src/database/migrations/run.ts | 6 ++++-- docs/extensions/migrations.md | 24 ++++++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 .changeset/wicked-scissors-confess.md diff --git a/.changeset/wicked-scissors-confess.md b/.changeset/wicked-scissors-confess.md new file mode 100644 index 0000000000..a2c6f8c84e --- /dev/null +++ b/.changeset/wicked-scissors-confess.md @@ -0,0 +1,5 @@ +--- +"@directus/api": patch +--- + +Allow `*.cjs` and `*.mjs` extensions for custom migrations diff --git a/api/src/database/migrations/run.ts b/api/src/database/migrations/run.ts index 2ab03a5917..b820fdbba3 100644 --- a/api/src/database/migrations/run.ts +++ b/api/src/database/migrations/run.ts @@ -9,6 +9,7 @@ import { flushCaches } from '../../cache.js'; import env from '../../env.js'; import logger from '../../logger.js'; import type { Migration } from '../../types/index.js'; +import getModuleDefault from '../../utils/get-module-default.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -21,7 +22,8 @@ export default async function run(database: Knex, direction: 'up' | 'down' | 'la ((await fse.pathExists(customMigrationsPath)) && (await fse.readdir(customMigrationsPath))) || []; migrationFiles = migrationFiles.filter((file: string) => /^[0-9]+[A-Z]-[^.]+\.(?:js|ts)$/.test(file)); - customMigrationFiles = customMigrationFiles.filter((file: string) => file.endsWith('.js')); + + customMigrationFiles = customMigrationFiles.filter((file: string) => /\.(c|m)?js$/.test(file)); const completedMigrations = await database.select('*').from('directus_migrations').orderBy('version'); @@ -113,7 +115,7 @@ export default async function run(database: Knex, direction: 'up' | 'down' | 'la for (const migration of migrations) { if (migration.completed === false) { needsCacheFlush = true; - const { up } = await import(`file://${migration.file}`); + const { up } = getModuleDefault(await import(`file://${migration.file}`)); if (log) { logger.info(`Applying ${migration.name}...`); diff --git a/docs/extensions/migrations.md b/docs/extensions/migrations.md index 6d6cf0c5e2..93db00c6e8 100644 --- a/docs/extensions/migrations.md +++ b/docs/extensions/migrations.md @@ -22,23 +22,27 @@ for example: 20201202A-my-custom-migration.js ``` +::: tip + +For backwards compatibility it is possible to rename your existing CommonJS migrations to `.cjs`. However using ESM where possible is recommended. + +::: + ## Structure Migrations have to export an `up` and a `down` function. These functions get a [Knex](http://knexjs.org) instance that can be used to do virtually whatever. ```js -module.exports = { - async up(knex) { - await knex.schema.createTable('test', (table) => { - table.increments(); - table.string('rijk'); - }); - }, +export async function up(knex) { + await knex.schema.createTable('test', (table) => { + table.increments(); + table.string('rijk'); + }); +}; - async down(knex) { - await knex.schema.dropTable('test'); - }, +export async function down(knex) { + await knex.schema.dropTable('test'); }; ```