CannotExecuteNotConnectedError when running migration on Postgres
See original GitHub issueIssue 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
- Create a migration
- 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:
- Created a year ago
- Reactions:8
- Comments:28 (2 by maintainers)
Top Related StackOverflow Question
I found today that this error may be because of another completely unrelated error. I added a
console.loginnode_modules/typeorm/commands/MigrationRunCommand.jssuggested by lucasgama335, but instead I logged the error in the catch statement and found that I had accidentally used arequirein 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 😆