CannotExecuteNotConnectedError when running migration on Postgres

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:8
  • Comments:28 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
Johann150commented, Apr 30, 2022

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.

5reactions
igortrinidadcommented, Apr 13, 2022

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()
})()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migration from dataSource get the error: Cannot execute ...
MIGRATION ERROR:CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
Read more >
Documentation: 9.0: Migration Between Releases - PostgreSQL
Migration Between Releases. This section discusses how to migrate your database data from one PostgreSQL release to a newer one.
Read more >
TypeORM Migrations with Postgres. TypeScript Express #9
When using Postgres with TypeORM, the schema is generated based on your entities. When you instantiate a database, you create a default schema ......
Read more >
Database Migration Service for PostgreSQL to AlloyDB ...
Database Migration Service makes it easier for you to migrate your data to Google Cloud. This service helps you lift and shift your...
Read more >
Migrate from PostgreSQL to Amazon RDS with AWS DMS
In this lesson, you migrate a self-managed PostgreSQL database instance ... Amazon RDS is a fully managed service for running relational databases with...
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