TypeORM Cli:migrationsDir 似乎被脚本忽略

MDS*_*lds 5 database-migration typeorm

我正在尝试完成一个简单的迁移 - 重命名用户表中的列。\n我无法获取 cli 来使用 migrationsDir 来创建或运行迁移。

\n\n

迁移创造

\n\n

当我运行 \n 时npm run typeorm:cli -- migration:create -n UserFullName -d \'server/migration,在迁移文件夹中创建文件没有问题。

\n\n

创建不带 -d 参数的迁移只会在文件夹根目录中创建文件,它会忽略连接选项中的migrationsDir(请参阅下面的 ormconfig.ts)。

\n\n

运行迁移

\n\n

运行npm run typeorm:cli -- migration:run会产生退出状态 1,我的猜测是它找不到迁移,但我真的不知道。

\n\n
Error during migration run:\nError: No connection options were found in any of configurations file.\n    at ConnectionOptionsReader.<anonymous> (/Users/matthewshields/Documents/Code/Projects/Sumo/dohyo-dreams/src/connection/ConnectionOptionsReader.ts:41:19)\n    at step (/Users/matthewshields/Documents/Code/Projects/Sumo/dohyo-dreams/node_modules/tslib/tslib.js:133:27)\n    at Object.next (/Users/matthewshields/Documents/Code/Projects/Sumo/dohyo-dreams/node_modules/tslib/tslib.js:114:57)\n    at fulfilled (/Users/matthewshields/Documents/Code/Projects/Sumo/dohyo-dreams/node_modules/tslib/tslib.js:104:62)\n    at process._tickCallback (internal/process/next_tick.js:68:7)\n    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)\n    at Object.<anonymous> (/Users/matthewshields/Documents/Code/Projects/Sumo/dohyo-dreams/node_modules/ts-node/src/bin.ts:157:12)\n    at Module._compile (internal/modules/cjs/loader.js:689:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)\n    at Module.load (internal/modules/cjs/loader.js:599:32)\n
Run Code Online (Sandbox Code Playgroud)\n\n

包.json

\n\n
{\n  "name": "xxxxxxxxx",\n  "version": "0.1.0",\n  "private": true,\n  "main": "./server/server.ts",\n  "dependencies": {\n    "axios": "^0.19.0",\n    "bcrypt": "^3.0.6",\n    "body-parser": "^1.18.3",\n    "breakpoint-sass": "^2.7.1",\n    "chroma-js": "^2.0.3",\n    "class-transformer": "^0.2.0",\n    "class-validator": "^0.9.1",\n    "dotenv": "^6.2.0",\n    "envalid": "^4.1.4",\n    "express": "^4.16.4",\n    "express-session": "^1.16.1",\n    "http-server": "^0.11.1",\n    "lodash": "^4.17.15",\n    "lodash.isequal": "^4.5.0",\n    "massive": "^5.7.7",\n    "node-sass": "^4.11.0",\n    "pg": "^7.11.0",\n    "react": "^16.8.6",\n    "react-dom": "^16.8.6",\n    "react-router-dom": "^5.0.0",\n    "react-scripts": "2.1.8",\n    "reflect-metadata": "^0.1.13",\n    "sumo-rank": "^1.0.2",\n    "tsconfig-paths": "^3.9.0",\n    "typeorm": "^0.2.18"\n  },\n  "devDependencies": {\n    "@types/express": "^4.16.1",\n    "@types/node": "^10.12.11",\n    "husky": "^1.2.0",\n    "nodemon": "^1.18.7",\n    "ts-node": "^7.0.1",\n    "tslint": "^5.11.0",\n    "tslint-config-airbnb": "^5.11.1",\n    "typescript": "^3.2.1"\n  },\n  "scripts": {\n    "dev": "ts-node ./server/server.ts",\n    "start": "react-scripts start",\n    "build": "react-scripts build",\n    "test": "react-scripts test",\n    "eject": "react-scripts eject",\n    "start-sw": "express ./build",\n    "lint": "tslint -p tsconfig.json -c tslint.json",\n    "typeorm:cli": "ts-node ./node_modules/typeorm/cli.js"\n  },\n  "eslintConfig": {\n    "extends": "react-app"\n  },\n  "husky": {\n    "hooks": {\n      "pre-commit": "npm run lint"\n    }\n  },\n  "browserslist": [\n    ">0.2%",\n    "not dead",\n    "not ie <= 11",\n    "not op_mini all"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

服务器.ts

\n\n
require(\'dotenv\').config();\nimport { } from \'reflect-metadata\';\nimport { createConnection } from \'typeorm\';\nimport App from \'./app\';\nimport * as config from \'./ormconfig\';\n\nimport RankingsController from \'./rankings/rankings.controller\';\nimport RankChartsController from \'./rankCharts/rankCharts.controller\';\nimport TournamentsController from \'./tournaments/tournaments.controller\';\nimport UsersController from \'./users/users.controller\';\nimport validateEnv from \'./utils/validateEnv\';\nimport WrestlersController from \'./wrestlers/wrestlers.controller\';\n\nvalidateEnv();\n\n(async () => {\n  try {\n    await createConnection(config);\n  } catch (error) {\n    console.log(\'Error while connecting to the database\', error);\n    return error;\n  }\n  const app = new App(\n    [\n      new TournamentsController(),\n      new WrestlersController(),\n      new RankingsController(),\n      new RankChartsController(),\n      new UsersController(),\n    ],\n  );\n  app.listen();\n})();\n
Run Code Online (Sandbox Code Playgroud)\n\n

应用程序.ts

\n\n
import * as bodyParser from \'body-parser\';\nimport * as express from \'express\';\nimport Controller from \'./interfaces/interface.controller\';\nimport errorMiddleware from \'./middleware/error.middleware\';\n\nclass App {\n  public app: express.Application;\n\n  constructor(controllers: Controller[]) {\n    this.app = express();\n\n    this.initializeMiddlewares();\n    this.initializeErrorHandling();\n    this.initializeControllers(controllers);\n  }\n\n  public listen() {\n    this.app.listen(process.env.PORT, () => {\n      console.log(`App listening on the port ${process.env.PORT}`);\n    });\n  }\n\n  private initializeMiddlewares() {\n    this.app.use(bodyParser.json());\n  }\n\n  private initializeErrorHandling() {\n    this.app.use(errorMiddleware);\n  }\n\n  private initializeControllers(controllers: Controller[]) {\n    controllers.forEach((controller) => {\n      this.app.use(\'/\', controller.router);\n    });\n  }\n}\n\nexport default App;\n
Run Code Online (Sandbox Code Playgroud)\n\n

ormconfig.ts

\n\n
import { ConnectionOptions } from \'typeorm\';\n\nconst config: ConnectionOptions = {\n  type: \'postgres\',\n  host: process.env.POSTGRES_HOST,\n  port: Number(process.env.POSTGRES_PORT),\n  username: process.env.POSTGRES_USER,\n  password: process.env.POSTGRES_PASSWORD,\n  database: process.env.POSTGRES_DB,\n  entities: [\n    __dirname + \'/../**/*.entity{.ts,.js}\',\n  ],\n  cli: {\n    migrationsDir: \'server\',\n  }\n}\nexport = config;\n
Run Code Online (Sandbox Code Playgroud)\n\n

(时间戳)-UserFullName.ts

\n\n
import { MigrationInterface, QueryRunner } from "typeorm";\n\nexport class UserFullName1574403715918 implements MigrationInterface {\n\n  public async up(queryRunner: QueryRunner): Promise<any> {\n    await queryRunner.query(`ALTER TABLE "user" RENAME "fullName" to "name"`);\n  }\n\n  public async down(queryRunner: QueryRunner): Promise<any> {\n    await queryRunner.query(`ALTER TABLE "user" RENAME "name" to "fullName"`);\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我怀疑我的文件结构可能与该问题有关,因此我简要列出了它。我只是列出了一些基础知识,还有更多用于锦标赛、摔跤手、排名、排名图的控制器和实体。

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 server\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ormconfig.ts\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 server.ts\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.ts\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 users\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 users.controller.ts\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 users.dto.ts\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 users.entity.ts\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 migration\n
Run Code Online (Sandbox Code Playgroud)\n\n

第一次发帖,任何对我的格式或解释有建设性的批评都是值得赞赏的。

\n

The*_*ski 6

对于任何遇到类似问题的人来说,这些都是我很看重的好东西:

  1. typeorm CLI仅在创建迁移(不读取)时cli.migrationsDir读取(从 ormconfig.ts) 。您可以在文档中看到这种微妙的区别- 它如下:

    ... "cli": { "migrationsDir": "migration" }- 表示 CLI 必须在“迁移”目录中创建新的迁移。

  2. 这很令人困惑 - 为什么它需要一个单独的配置来编写?读/写迁移不是相同的配置吗?我不知道,仍然不知道 - 但我也通过阅读源代码证实了这一点(除非我闻到了什么)。

  3. migrations: [...]最终结论:这些配置 ( & )可能cli.migrationsDir应该指向文件系统上的同一位置,除非您有充分的理由不这样做。

干杯。


小智 -1

您是否到达了正确的迁移文件路径?

{
    cli: {
        migrationsDir: "src/migration"
    }
}
Run Code Online (Sandbox Code Playgroud)

https://github.com/typeorm/typeorm/blob/master/docs/using-cli.md