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

Error running generated migration #298

Closed
finikm opened this issue Jan 9, 2020 · 9 comments
Closed

Error running generated migration #298

finikm opened this issue Jan 9, 2020 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@finikm
Copy link

finikm commented Jan 9, 2020

Describe the bug
I use the CLI command to generate a migration (npx mikro-orm migration:create), and when I try to run the migration (npx mikro-orm migration:up) I get: TypeError: Class constructor Migration cannot be invoked without 'new'

Stack trace

[Nest] 65039   - 01/09/2020, 6:16:32 PM   [MikroORM] == Migration20200109161524: migrating =======
mikro-orm migration:up

Migrate up to the latest version

Options:
  -t, --to       Migrate up to specific version                         [string]
  -f, --from     Start migration from specific version                  [string]
  -o, --only     Migrate only specified versions                        [string]
  -v, --version  Show version number                                   [boolean]
  -h, --help     Show help                                             [boolean]

TypeError: Class constructor Migration cannot be invoked without 'new'
    at new Migration20200109161524 (/Users/.../app/backend/src/migrations/Migration20200109161524.ts:56:42)
    at Migrator.resolve (/Users/.../app/node_modules/mikro-orm/dist/migrations/Migrator.js:67:26)
    at Object.customResolver (/Users/.../app/node_modules/mikro-orm/dist/migrations/Migrator.js:26:46)
    at Migration.migration (/Users/.../app/node_modules/umzug/lib/migration.js:63:38)
    at /Users/.../app/node_modules/umzug/lib/migration.js:123:37
    at Generator.next (<anonymous>)
    at step (/Users/.../app/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /Users/.../app/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
    at new Promise (<anonymous>)
    at new F (/Users/.../app/node_modules/babel-runtime/node_modules/core-js/library/modules/_export.js:36:28)
    at /Users/.../app/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12
    at Migration._exec (/Users/.../app/node_modules/umzug/lib/migration.js:136:7)
    at Migration.up (/Users/.../app/node_modules/umzug/lib/migration.js:90:17)
    at Umzug.<anonymous> (/Users/.../app/node_modules/umzug/lib/index.js:152:22)
    at PassThroughHandlerContext.finallyHandler (/Users/.../app/node_modules/bluebird/js/release/finally.js:57:23)
    at PassThroughHandlerContext.tryCatcher (/Users/.../app/node_modules/bluebird/js/release/util.js:16:23)

To Reproduce
Steps to reproduce the behavior:

  1. npx mikro-orm migration:create
  2. npx mikro-orm migration:up

Expected behavior
Run the migration

Additional context

  • npx mikro-orm schema:create --run works without issues, which makes me believe that the configuration is OK, regardless of the complex project structure
  • mikro-orm is used with NestJS using nestjs-mikro-orm, as used in the sample project
  • SQL code for the migration is OK
  • Replacing SQL code with "SELECT 1+1" results to the same issue

Versions

Dependency Version
node 12.13.1
typescript 3.7.4
mikro-orm 3.0.0-rc.5
pg 7.17.0
@finikm finikm added the bug Something isn't working label Jan 9, 2020
@B4nan
Copy link
Member

B4nan commented Jan 9, 2020

Sounds like a problem with your TS configuration, do you by any change transpile to ES5?

https://stackoverflow.com/questions/51860043/javascript-es6-typeerror-class-constructor-client-cannot-be-invoked-without-ne

@B4nan
Copy link
Member

B4nan commented Jan 9, 2020

How does the generated file look like and do you have useTsNode flag enabled in your package.json?

Currently the useTsNode flag is required for running TS migrations (which is currently the only option, but in future I would like to support generating JS migrations too).

@finikm
Copy link
Author

finikm commented Jan 9, 2020

I use a base tsconfig.json in project root, and I extend it frontend/backend accordingly. While both frontend and backend had "target": "es2018", base tsconfig.json had nothing set (thus ES5). Adding "target": "es2018" to base solved the issue.

Is there a way to define which tsconfig.json to be used by CLI?

In any case, thanks :)

@B4nan
Copy link
Member

B4nan commented Jan 9, 2020

You would have to wrap the call with ts-node yourself so you can feed it with the parameter. Something like this should work:

$ ts-node -p path/to/tsconfig.json node_modules/.bin/mikro-orm ...

@finikm
Copy link
Author

finikm commented Jan 9, 2020

Not as clean, so I 'll make sure the base tsconfig contains everything required by mikro-orm. Thanks for the help!

@B4nan B4nan closed this as completed in 00a1a4d Jan 9, 2020
@B4nan
Copy link
Member

B4nan commented Jan 9, 2020

In next RC version you should be able to provide the path to tsconfig via tsConfigPath in your package.json.

@onadebi
Copy link

onadebi commented Aug 18, 2021

So, this might be useful to someone who comes across this. The short answer for me that resolved this issue was setting target property of my tsconfig.json file, to "ESNEXT"

@superiums
Copy link

same issue happens.

package.json:

{
  "name": "lv-main",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "type": "commonjs",
  "mikro-orm": {
    "useTsNode": true,
    "tsConfigPath": "./tsconfig.json",
    "configPaths": [
      "./mikro-orm.config.ts"
    ]
  },
  "scripts": {
    "build:swc": "npx swc --out-dir dist -w apps libs",
    "start:swc": "nodemon dist/lv-main",
    "test": "vitest run",
    "test:watch": "vitest",
    "test:cov": "vitest run --coverage",
    "test:debug": "vitest --inspect-brk --inspect --logHeapUsage --threads=false",
    "test:e2e": "viapps/lv-main/test run --config ./vitest.config.ts",
    "mig": "ts-node ./scripts/migrate",
    "build": "nest build",
    "format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/apps/lv-main/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "testj": "jest",
    "testj:watch": "jest --watch",
    "testj:cov": "jest --coverage",
    "testj:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "testj:e2e": "jest --config ./apps/lv-main/test/jest-e2e.json"
  },
  "dependencies": {
    "@fastify/cookie": "^8.3.0",
    "@fastify/formbody": "^7.4.0",
    "@fastify/multipart": "^7.6.0",
    "@fastify/static": "^6.10.2",
    "@fastify/swagger": "^8.5.1",
    "@mikro-orm/cli": "5.7.12",
    "@mikro-orm/core": "5.7.12",
    "@mikro-orm/nestjs": "5.1.8",
    "@mikro-orm/postgresql": "5.7.12",
    "@nestjs/cache-manager": "^1.0.0",
    "@nestjs/common": "^9.4.2",
    "@nestjs/config": "^2.3.3",
    "@nestjs/core": "^9.4.2",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-fastify": "^9.4.2",
    "@nestjs/serve-static": "^3.0.1",
    "@nestjs/swagger": "^6.3.0",
    "@nestjsx/crud": "^5.0.0-alpha.3",
    "@nestjsx/crud-request": "^5.0.0-alpha.3",
    "cache-manager": "^5.2.2",
    "passport": "^0.6.0",
    "passport-cookie": "^1.0.9",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@mikro-orm/entity-generator": "^5.7.12",
    "@mikro-orm/migrations": "^5.7.12",
    "@nestjs/cli": "^9.5.0",
    "@nestjs/schematics": "^9.2.0",
    "@nestjs/testing": "^9.4.2",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.62",
    "@types/node": "^20.3.0",
    "@types/passport-jwt": "^3.0.8",
    "@types/passport-local": "^1.0.35",
    "@types/supertest": "^2.0.12",
    "@types/validator": "^13.7.17",
    "@typescript-eslint/eslint-plugin": "^5.59.9",
    "@typescript-eslint/parser": "^5.59.9",
    "@vitest/coverage-c8": "^0.32.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "nodemon": "^2.0.22",
    "prettier": "^2.8.8",
    "rimraf": "^5.0.1",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-loader": "^9.4.3",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3",
    "unplugin-swc": "^1.3.2",
    "vitest": "^0.32.0"
  }
}

tsconfig.ts

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ESNext",
    "sourceMap": false,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "@app/lv-post": [
        "libs/lv-post/src"
      ],
      "@app/lv-post/*": [
        "libs/lv-post/src/*"
      ],
      "@app/s-database": [
        "libs/s-database/src"
      ],
      "@app/s-database/*": [
        "libs/s-database/src/*"
      ],
      "@app/s-membership": [
        "libs/s-membership/src"
      ],
      "@app/s-membership/*": [
        "libs/s-membership/src/*"
      ],
      "@app/s-log": [
        "libs/s-log/src"
      ],
      "@app/s-log/*": [
        "libs/s-log/src/*"
      ],
      "@app/s-auth": [
        "libs/s-auth/src"
      ],
      "@app/s-auth/*": [
        "libs/s-auth/src/*"
      ],
      "@app/s-dict": [
        "libs/s-dict/src"
      ],
      "@app/s-dict/*": [
        "libs/s-dict/src/*"
      ],
      "@app/s-booter": [
        "libs/s-booter/src"
      ],
      "@app/s-booter/*": [
        "libs/s-booter/src/*"
      ],
      "@app/s-common": [
        "libs/s-common/src"
      ],
      "@app/s-common/*": [
        "libs/s-common/src/*"
      ],
      "@app/crud-mikro": [
        "libs/crud-mikro/src"
      ],
      "@app/crud-mikro/*": [
        "libs/crud-mikro/src/*"
      ]
    }
  }
}

@superiums
Copy link

error:

Processing 'Migration20230616091813_init0516'
MigrationError: Migration Migration20230616091813_init0516 (up) failed: Original error: Class constructor Migration cannot be invoked without 'new'
    at /opt/code/nodeSpace/nest/lv-main/node_modules/.pnpm/[email protected]/node_modules/umzug/src/umzug.ts:259:12
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Umzug.runCommand (/opt/code/nodeSpace/nest/lv-main/node_modules/.pnpm/[email protected]/node_modules/umzug/src/umzug.ts:208:11)
    at Migrator.runInTransaction (/opt/code/nodeSpace/nest/lv-main/node_modules/.pnpm/registry.npmmirror.com+@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/@mikro-orm/migrations/Migrator.js:301:21)
    at PostgreSqlConnection.transactional (/opt/code/nodeSpace/nest/lv-main/node_modules/.pnpm/registry.npmmirror.com+@[email protected]_@[email protected]_@mikro-orm+entity-genera_jwvymlb5k4coghsmkifoga533m/node_modules/@mikro-orm/knex/AbstractSqlConnection.js:36:25)
    at /opt/code/nodeSpace/nest/lv-main/dist/apps/migrate/main.js:33:13
 ELIFECYCLE  Command failed with exit code 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants