question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

migrations not run with dotenv file

See original GitHub issue

Issue type:

[X] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [X] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Hello, I’m trying to run migrations. I have a dotenv configuration file for each non-production environment per the dotenv instructions (.env.development, .env.test, etc.). However, when I run migrations I get the error:

Error: No connection options were found in any of configurations file.

I’ve tried explicity setting the file with --config .env.development and I’ve tried leaving things blank. How should I be specifying this file?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
rubiincommented, Jun 6, 2020

I have the same problem here. I verified the code base and I noticed that the TypeORM doing loading .env file, overriding my dotenv rules.

See how my environment files are configured.

// >>> env.ts
import dotenv from 'dotenv';
import path from 'path';

const envFile = process.env.NODE_ENV ? `.env.${process.env.NODE_ENV}` : '.env';
const envPath = path.resolve(__dirname, `../../${envFile}`);

dotenv.config({
  path: envPath,
});

// >>> ormconfig.ts
import '@config/env';

const { DB_TYPE, DB_USER, DB_PASS, DB_NAME, DB_HOST } = process.env;

module.exports = [
  {
    type: DB_TYPE || 'postgres',
    name: 'default',
    host: DB_HOST,
    username: DB_USER,
    password: DB_PASS,
    database: DB_NAME,
    entities: ['src/modules/**/infra/typeorm/entities/*.ts'],
    migrations: ['src/shared/infra/typeorm/migrations/*.ts'],
    cli: {
      migrationsDir: 'src/shared/infra/typeorm/migrations',
    },
    migrationsTableName: '_migrations',
  },
];

I believe that what is causing problems is the excerpt below. We should and could have a way to avoid this behavior.

https://github.com/typeorm/typeorm/blob/4ed79c9cf9ad0f1e51930159dfdab8c1e8549339/src/connection/ConnectionOptionsReader.ts#L98-L101

i am using migrations with dotenv and it works

1reaction
devdemicommented, Dec 6, 2019

I have the same problem. I have a file with typeorm configuration src/config/typeorm.ts

export default {
    type: "postgres",
    host: process.env.DB_HOST,
    port: parseInt(process.env.DB_PORT),
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    schema: process.env.DB_SCHEMA,
    logging: process.env.DB_LOGGING == 'true',
    sync: process.env.DB_SYNC == 'true',
    entities: ["dist/**/*.entity{.ts,.js}"],
    migrations: ["dist/migrations/*{.ts,.js}"],
    cli: {
        migrationsDir: "migrations"
    }
}

I couldn’t execute typeorm migration:run --config dist/src/config/typeorm.js because dotenv doesn’t apply enviroment variables. And it seems --config development.env or --config .env.development will not work because the file contains just text wich describe enviroment variables. Please advise how to apply config which uses environment variables.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node-pg-migration not reading .env file despite having dotenv ...
I am using node-pg-migrate and would like the db name, password and schema to come from the .env file as opposed to a...
Read more >
NestJS DB config that plays well with migrations
When you run your migrations, you want to specify the environment in the same way. ... It's no problem if the .env file...
Read more >
4. Sequelize and Dotenv Files - Overview and Implementation
Access the full course early: https://chriscourses.com/courses/build-web-apps/videos/sequelize-dotenvPart four of the How to Build Web Apps ...
Read more >
How to set environment variables for your web apps (for ...
Make sure that you have at least version 0.8 of the python-dotenv package, as older versions can't handle the export at the start...
Read more >
Environments and deployments - GitLab Docs
yml file. When an environment is stopped, the matching on_stop actions from successful deployment jobs alone are run in parallel in no particular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found