typeorm: CannotExecuteNotConnectedError when running migration on Postgres

Issue Description

When trying to run a migration on Postgres DB, the migrations fail to run with CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established. at DataSource.destroy (/home/assaf/Code/private/f3tw/src/data-source/DataSource.ts:284:19) at Object.handler (/home/assaf/Code/private/f3tw/src/commands/MigrationRunCommand.ts:73:46)

Expected Behavior

All migrations should run as expected.

Actual Behavior

No migrations are running, and an error is thrown:

> typeorm-ts-node-esm -d src/data-source.ts "migration:run"

query: SELECT * FROM current_schema()
typeorm-ts-node-esm migration:run

Runs all pending migrations.

Options:
  -h, --help         Show help                                         [boolean]
  -d, --dataSource   Path to the file where your DataSource instance is defined.
                                                                      [required]
  -t, --transaction  Indicates if transaction should be used or not for
                     migration run. Enabled by default.     [default: "default"]
  -v, --version      Show version number                               [boolean]

CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
    at DataSource.destroy (/home/assaf/Code/private/f3tw/src/data-source/DataSource.ts:284:19)
    at Object.handler (/home/assaf/Code/private/f3tw/src/commands/MigrationRunCommand.ts:73:46)

Steps to Reproduce

  1. Create a migration
  2. try to run it with npm run typeorm migration:run

my datasource:

import "reflect-metadata";
import { DataSource } from "typeorm";

export const TestAppDataSource = new DataSource({
  type: "postgres",
  host: "localhost",
  port: 5432,
  username: "postgres",
  password: "test",
  database: "postgres",
  logging: process.env.NODE_ENV !== "production",
  entities: ["src/entity/*.{js,ts}"],
  migrations: ["src/migration/*.{ts, ts}"],
});

From looking at the code in src/commands/MigrationRunCommand.ts it seems like it does call await dataSource.initialize() so I have no idea where that problem is coming from.

My Environment

~Might be related to the fact that I am using npm workspace and running inside one of the projects?~ It does work when using typeorm-ts-node-commonjs but not typeorm-ts-node-esm

Dependency Version
Operating System Arch linux
Node.js version 17.8.0
Typescript version 4.6.3
TypeORM version 0.3.5

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 29 (2 by maintainers)

Most upvoted comments

I found today that this error may be because of another completely unrelated error. I added a console.log in node_modules/typeorm/commands/MigrationRunCommand.js suggested by lucasgama335, but instead I logged the error in the catch statement and found that I had accidentally used a require in an ecmascript module.

I didn’t found a solution @assapir but i could finally run the migrations.

Isn’t the best solution ever but at least we could go to sleep without this exception on mind lol 😆

import dbCreateConnection from "./dbCreateConnection"
import AppDataSource from "./AppDataSource"

export default ( async () => {
  await dbCreateConnection()
  await AppDataSource.runMigrations()
})()

After a couple of hours of investigation I also came to this conclusion: If you see the error below after writing some migrations:

CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established

and it even shows when running typeorm migration:show

then you probably have problem with connection or your migration code! (EDIT: or, as it turns out, entity code, including incorrect column type).

Unfortunately, typeorm (or typeorm cli, or anything related) hides all compilation errors from migrations. The best way to learn what is wrong with your migration is to run:

npx ts-node
> import { MIGRATION_CLASS_NAME } from './src/migration/MIGRATION_FILENAME'
> MIGRATION_CLASS_NAME

This will show you actual errors in the code syntax and will help you solve them. In my case, it was just a typo in a variable name, but cli always reported CannotExecuteNotConnectedError, so I spent way too long time digging into my connectivity params.

I recommend everyone first attempt what lucasgama335 and Johann150 did and add a console.log(err) in the catch block of the node_modules/typeorm/commands/MigrationRunCommand.js on line 65. For me, my issue was I was technically still connected in datagrip to an external DB through an ssh tunnel that was bound to the same local port as my typeorm connection and I was getting an auth error that wasn’t surfacing.

error: password authentication failed for user "my-local-db"

Issue Description

When trying to run a migration on Postgres DB, the migrations fail to run with CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established. at DataSource.destroy (/home/assaf/Code/private/f3tw/src/data-source/DataSource.ts:284:19) at Object.handler (/home/assaf/Code/private/f3tw/src/commands/MigrationRunCommand.ts:73:46)

Expected Behavior

All migrations should run as expected.

Actual Behavior

No migrations are running, and an error is thrown:

> typeorm-ts-node-esm -d src/data-source.ts "migration:run"

query: SELECT * FROM current_schema()
typeorm-ts-node-esm migration:run

Runs all pending migrations.

Options:
  -h, --help         Show help                                         [boolean]
  -d, --dataSource   Path to the file where your DataSource instance is defined.
                                                                      [required]
  -t, --transaction  Indicates if transaction should be used or not for
                     migration run. Enabled by default.     [default: "default"]
  -v, --version      Show version number                               [boolean]

CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
    at DataSource.destroy (/home/assaf/Code/private/f3tw/src/data-source/DataSource.ts:284:19)
    at Object.handler (/home/assaf/Code/private/f3tw/src/commands/MigrationRunCommand.ts:73:46)

Steps to Reproduce

  1. Create a migration
  2. try to run it with npm run typeorm migration:run

my datasource:

import "reflect-metadata";
import { DataSource } from "typeorm";

export const TestAppDataSource = new DataSource({
  type: "postgres",
  host: "localhost",
  port: 5432,
  username: "postgres",
  password: "test",
  database: "postgres",
  logging: process.env.NODE_ENV !== "production",
  entities: ["src/entity/*.{js,ts}"],
  migrations: ["src/migration/*.{ts, ts}"],
});

From looking at the code in src/commands/MigrationRunCommand.ts it seems like it does call await dataSource.initialize() so I have no idea where that problem is coming from.

My Environment

~Might be related to the fact that I am using npm workspace and running inside one of the projects?~ It does work when using typeorm-ts-node-commonjs but not typeorm-ts-node-esm

Dependency Version Operating System Arch linux Node.js version 17.8.0 Typescript version 4.6.3 TypeORM version 0.3.5

Additional Context

Relevant Database Driver(s)

DB Type Reproducible aurora-mysql no aurora-postgres no better-sqlite3 no cockroachdb no cordova no expo no mongodb no mysql no nativescript no oracle no postgres yes react-native no sap no sqlite no sqlite-abstract no sqljs no sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

I found the problem. import dotenv into your file that contains the dataSource. i also retrieve some environment variables and for some reason when dotenv does not matter the username comes as null.

My code: Configuration file: (@src/config/database.ts)

export default {
    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */
    driver: process.env.DB_CONNECTION || 'postgres',
    debug: process.env.DB_DEBUG === 'true',

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by App is shown below to make development simple.
    |
    |
    */
    connections: {
        postgres: {
            driver: 'postgres',
            host: process.env.DB_HOST || 'localhost',
            port: Number(process.env.DB_PORT) || 5432,
            database:
                process.env.APP_ENV === 'test'
                    ? process.env.DB_DATABASE_TEST || 'api_base_test'
                    : process.env.DB_DATABASE || 'api_base',
            username: process.env.DB_USERNAME || 'postgres',
            password: process.env.DB_PASSWORD || 'root',
        },
    },

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer body of commands than a typical key-value system
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */
    redis: {
        host: process.env.REDIS_HOST || 'localhost',
        port: process.env.REDIS_PORT || 6379,
        username: process.env.REDIS_USERNAME || '',
        password: process.env.REDIS_PASSWORD || '',
    },
};

Configuration file: (@src/config/index.ts)

import 'dotenv/config';
import app from './app';
import auth from './auth';
import cors from './cors';
import database from './database';
import filesystem from './filesystem';
import mail from './mail';
import upload from './upload';

export const ConfigService = {
    app,
    auth,
    cors,
    database,
    filesystem,
    mail,
    upload,
};

dataSource:

import { DataSource } from 'typeorm';

import { ConfigService } from '@src/config';

export const AppDataSource = new DataSource({
    type: 'postgres',
    host: ConfigService.database.connections.postgres.host,
    port: ConfigService.database.connections.postgres.port,
    username: ConfigService.database.connections.postgres.username,
    password: ConfigService.database.connections.postgres.password,
    database: ConfigService.database.connections.postgres.database,
    synchronize: false,
    logging: ConfigService.database.debug,
    entities: ['./src/modules/**/entities/typeorm/*.ts'],
    migrations: ['./src/database/typeorm/migrations/*.ts'],
});

if it doesn’t work for you, go to your node_modules/typeorm/commands/MigrationRunCommand.js folder and console log the dataSource to verify the connection data is correct. I discovered my problem this way.

same issue with misskey migrations. edit: in the case of misskey, running npm run clean-all and then installing the packages again made it possible to run the migrations as normal.

I was able to fix my issue with migration:show with the advice from this reply.

I had custom path aliases configured in my tsconfig.json such that I could “pretty-import” modules within the project, i.e. import { User } from '@/user/user.entity. Turns out Typeorm doesn’t like this, and couldn’t resolve the module references while using typeorm-ts-node-commonjs. The error I was able to log from node_modules/typeorm/commands/MigrationShowCommand.js was

Error: Cannot find module '@/users/user.entity'
Require stack:
- /Users/{user}/dev/{ns}/_sandbox/{app}/src/auth/entities/token.entity.ts
- /Users/{user}/dev/{ns}/_sandbox/{app}/node_modules/typeorm/util/ImportUtils.js
- /Users/{user}/dev/{ns}/_sandbox/{app}/node_modules/typeorm/commands/CommandUtils.js
...

This explains why I could use the Typeorm CLI fine from the built JS datasource, but using the TS datasource it was unable to resolve those references.

I had the same issue today and after some debugging like @acSpock told us I finally got an error message that helped me figuring out what seems to be the problem.

Including console.log(err) didn’t helped but commenting the dataSource.destroy() method out I got the espected result and an error: image

I then found this article that solved my problems (Seems like I just had to define a password for connecting to an mysql database…) https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server

Hope that helps some of you.

Same issue

++ Same Issue