简体   繁体   中英

NestJs TypeORM migrations fail with cross environments

I have two environment files env.development and env.production. When I am running migrations it is not working because of environment variables. If I put them static then it's working.

On the other hand if I use only.env then environments variables are working properly.

is there any way to run typeorm migration with cross environments?

export const typeOrmConfig: TypeOrmModuleOptions = {
    type: 'postgres',
    dropSchema: false,
    migrationsRun: false,
    host: process.env.DATABASE_HOST,
    database: process.env.DATABASE_NAME,
    username: process.env.DATABASE_USERNAME,
    password: process.env.DATABASE_PASSWORD,
    entities: [__dirname + '/../**/*.entity{.ts,.js}'],
    port: parseInt(process.env.DATABASE_PORT as string, 10),
    synchronize: process.env.DATABASE_SYNCHRONIZE === 'true',
    migrations: [__dirname + '/../database/migrations/*{.ts,.js}'],
    cli: {
        migrationsDir: __dirname + '/../database/migrations',
    },
};

Scripts:

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -f src/config/typeorm-migrations.config.ts",
"migration:generate": "npm run typeorm -- migration:generate -d src/database/migrations -n",
"migration:create": "npm run typeorm -- migration:create -d src/database/migrations -n",
"migration:revert": "npm run typeorm -- migration:revert",
"migration:run": "npm run typeorm -- migration:run"

You can use datasource file for each environment, and add "-d yourdatasource.ts" to your typeorm script migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM