gpt4 book ai didi

database-migration - 类型ORM Cli : migrationsDir seems to be ignored by scripts

转载 作者:行者123 更新时间:2023-12-04 04:22:00 24 4
gpt4 key购买 nike

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

迁移创建

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

创建没有 -d 参数的迁移只会在文件夹根目录中创建文件,它会忽略连接选项中的迁移目录(请参阅下面的 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)

包.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"
]
}

服务器.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();
})();

应用程序.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;

配置文件
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;

(时间戳)-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"`);
}
}

我怀疑我的文件结构可能与该问题有关,因此我已简要列出。我只是列出了一些基础知识,还有更多用于锦标赛、摔跤手、排名、排名图表的 Controller 和实体。
├── docker-compose.yaml
├── package.json
├── src
├── server
│ ├── ormconfig.ts
│ ├── server.ts
│ ├── app.ts
│ ├── users
│ │ ├── users.controller.ts
│ │ ├── users.dto.ts
│ │ ├── users.entity.ts
│ ├── migration

第一次发帖,对我的格式或解释的任何 build 性批评表示赞赏。

最佳答案

对于遇到类似问题的任何人,这些都是我看重的好掘金:

  • typeorm CLI 读取 cli.migrationsDir (来自 ormconfig.ts)仅在创建迁移时(不读取)。 你可以看到细微的区别 here in the docs - 它写道:

    "..."cli": { "migrationsDir": "migration" } - indicates that theCLI must create new migrations in the "migration" directory."


  • 这令人困惑 - 为什么它需要一个单独的配置来编写?读/写迁移不是相同的配置吗?我不知道,仍然不知道 - 但我也通过阅读来源证实了这一点(除非我闻到了一些东西)。
  • 最终结论:除非您有充分的理由不这样做,否则这些配置( migrations: [...]cli.migrationsDir )应该指向文件系统上的相同位置。

  • 干杯。

    关于database-migration - 类型ORM Cli : migrationsDir seems to be ignored by scripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58989696/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com