TypeORM Cli:脚本似乎忽略了migrationsDir

问题描述 投票:1回答:1

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

迁移创建

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

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

正在迁移中

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

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

package.json

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

server.ts

require('dotenv').config();
import { } from 'reflect-metadata';
import { createConnection } from 'typeorm';
import App from './app';
import * as config from './ormconfig';

import RankingsController from './rankings/rankings.controller';
import RankChartsController from './rankCharts/rankCharts.controller';
import TournamentsController from './tournaments/tournaments.controller';
import UsersController from './users/users.controller';
import validateEnv from './utils/validateEnv';
import WrestlersController from './wrestlers/wrestlers.controller';

validateEnv();

(async () => {
  try {
    await createConnection(config);
  } catch (error) {
    console.log('Error while connecting to the database', error);
    return error;
  }
  const app = new App(
    [
      new TournamentsController(),
      new WrestlersController(),
      new RankingsController(),
      new RankChartsController(),
      new UsersController(),
    ],
  );
  app.listen();
})();

apps.ts

import * as bodyParser from 'body-parser';
import * as express from 'express';
import Controller from './interfaces/interface.controller';
import errorMiddleware from './middleware/error.middleware';

class App {
  public app: express.Application;

  constructor(controllers: Controller[]) {
    this.app = express();

    this.initializeMiddlewares();
    this.initializeErrorHandling();
    this.initializeControllers(controllers);
  }

  public listen() {
    this.app.listen(process.env.PORT, () => {
      console.log(`App listening on the port ${process.env.PORT}`);
    });
  }

  private initializeMiddlewares() {
    this.app.use(bodyParser.json());
  }

  private initializeErrorHandling() {
    this.app.use(errorMiddleware);
  }

  private initializeControllers(controllers: Controller[]) {
    controllers.forEach((controller) => {
      this.app.use('/', controller.router);
    });
  }
}

export default App;

ormconfig.ts

import { ConnectionOptions } from 'typeorm';

const config: ConnectionOptions = {
  type: 'postgres',
  host: process.env.POSTGRES_HOST,
  port: Number(process.env.POSTGRES_PORT),
  username: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB,
  entities: [
    __dirname + '/../**/*.entity{.ts,.js}',
  ],
  cli: {
    migrationsDir: 'server',
  }
}
export = config;

(timestamp)-UserFullName.ts

import { MigrationInterface, QueryRunner } from "typeorm";

export class UserFullName1574403715918 implements MigrationInterface {

  public async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query(`ALTER TABLE "user" RENAME "fullName" to "name"`);
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query(`ALTER TABLE "user" RENAME "name" to "fullName"`);
  }
}

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

├── docker-compose.yaml
├── package.json
├── src
├── server
│   ├── ormconfig.ts
│   ├── server.ts
│   ├── app.ts
│   ├── users
│   │   ├── users.controller.ts
│   │   ├── users.dto.ts
│   │   ├── users.entity.ts
│   ├── migration

初次张贴者,对我的格式或解释有建设性的批评,深表感谢。

我正在尝试完成简单的迁移-重命名users表中的列。我无法使用clis使用migrationsDir来创建或运行迁移。运行时迁移创建...

database-migration typeorm
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.