我正在尝试完成一个简单的迁移-重命名users表中的一列。 我无法使用clis使用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
初次张贴,对我的格式或解释提出的任何建设性批评表示赞赏。
答案 0 :(得分:0)
我认为这里的问题是因为您使用异步连接。在添加具有同步连接的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,我发现它非常有用。
答案 1 :(得分:0)
从您的文件结构看来,配置应如下所示:
ormconfig.ts
export const config: TypeOrmModuleOptions = {
...
migrations: ['server/migration/*.js', 'server/migration/*.ts'],
cli: {
migrationsDir: 'server/migration',
},
};
您可能需要从ormconfig.ts
文件夹中取出server
文件,使其与package.json
位于同一级别。
答案 2 :(得分:0)
对于遇到类似问题的任何人,这些都是我重视的重要矿块:
仅当创建迁移(不读取)时,typeorm CLI才会从ormconfig.ts中读取cli.migrationsDir
。您可以看到细微的区别here in the docs-它显示为:
“ ...
"cli": { "migrationsDir": "migration" }
-表示 CLI必须在“迁移”目录中创建新的迁移。”
这令人困惑-为什么只需要编写一个单独的配置?读/写迁移不会是相同的配置吗?我不知道,仍然不知道-但是我也通过阅读源代码来确认了这一点(除非我发了些什么)。
最终结论:除非您有充分的理由,否则这些配置(migrations: [...]
和cli.migrationsDir
)可能指向文件系统上的相同位置。
干杯。
答案 3 :(得分:-2)
您是否到达了正确的迁移文件路径?
{
cli: {
migrationsDir: "src/migration"
}
}
https://github.com/typeorm/typeorm/blob/master/docs/using-cli.md