我正在尝试完成一个简单的迁移--重新命名用户表中的列。我无法让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;(时间戳)-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第一次海报,任何建设性的批评,我的格式或解释,将不胜感激。
发布于 2020-11-23 16:04:39
对于任何遇到类似问题的人来说,这些都是我珍视的好金块:
cli.migrationsDir (从ormconfig.ts) 。
"...__"cli": { "migrationsDir": "migration" } --表示CLI必须在“迁移”目录中创建新的迁移。“migrations: [...] & cli.migrationsDir)很可能指向文件系统上的相同位置,除非您有充分的理由不这样做。干杯。
发布于 2020-07-15 06:58:23
我认为这里的问题在于您使用异步连接。我也遇到了同样的问题,并且在添加了带有同步连接的ormconfig.ts (也应该与.js一起工作)文件之后,我设法解决了这个问题。
在配置文件中,应该添加cli属性cli: {migrationsDir: "server/migration"}。
要能够使用cli运行迁移,需要另一个属性:migrations: [join(__dirname, 'server/migration/*{.ts,.js}')],
此外,在运行cli时,您应该指出配置文件位于何处,--config path/to/ormconfig.ts标志。
具有ts:ts-node ./node_modules/typeorm/cli.js migration:generate --config server/ormconfig.ts的完整命令示例
要获得更多信息,您可以查看这个示例https://github.com/ambroiseRabier/typeorm-nestjs-migration-example,我发现它非常有用。
发布于 2020-07-15 08:02:05
从文件结构看,配置应该如下所示:
ormconfig.ts
export const config: TypeOrmModuleOptions = {
...
migrations: ['server/migration/*.js', 'server/migration/*.ts'],
cli: {
migrationsDir: 'server/migration',
},
};您可能需要将ormconfig.ts文件从server文件夹中取出,以便与package.json处于同一级别。
https://stackoverflow.com/questions/58989696
复制相似问题