From 10776de54f7134a73976b2c6f8cd70d992b007b3 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 8 Oct 2023 16:56:56 +0200 Subject: [PATCH] fix(migrations): use migration file extension according to runtime We need to use .ts only if we run inside ts-node or other tools that use it. In all other cases, we need to refer to the .js migration files. Signed-off-by: David Mehren --- backend/src/app.module.ts | 7 ++++++- backend/src/utils/detectTsNode.ts | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 backend/src/utils/detectTsNode.ts diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 02416dd57..170d2a012 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -38,6 +38,7 @@ import { WebsocketModule } from './realtime/websocket/websocket.module'; import { RevisionsModule } from './revisions/revisions.module'; import { SessionModule } from './sessions/session.module'; import { UsersModule } from './users/users.module'; +import { detectTsNode } from './utils/detectTsNode'; const routes: Routes = [ { @@ -70,7 +71,11 @@ const routes: Routes = [ autoLoadEntities: true, logging: true, logger: logger, - migrations: [`**/migrations/${databaseConfig.type}-*{.ts,.js}`], + migrations: [ + `**/migrations/${databaseConfig.type}-*.${ + detectTsNode() ? 'ts' : 'js' + }`, + ], migrationsRun: true, }; }, diff --git a/backend/src/utils/detectTsNode.ts b/backend/src/utils/detectTsNode.ts new file mode 100644 index 000000000..e866ae1fe --- /dev/null +++ b/backend/src/utils/detectTsNode.ts @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2018 Martin Adámek + * + * SPDX-License-Identifier: MIT + */ + +/** + * Stolen from https://github.com/mikro-orm/mikro-orm/blob/20179ec839def5f8144e56f3a6bc89131f7e72a4/packages/core/src/utils/Utils.ts#L689 + */ +export function detectTsNode(): boolean { + return ( + process.argv[0].endsWith('ts-node') || // running via ts-node directly + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore TS7053 + !!process[Symbol.for('ts-node.register.instance')] || // check if internal ts-node symbol exists + !!process.env.TS_JEST || // check if ts-jest is used (works only with v27.0.4+) + process.argv.slice(1).some((arg) => arg.includes('ts-node')) || // registering ts-node runner + (require.extensions && !!require.extensions['.ts']) + ); // check if the extension is registered +}