Repository navigation
fix: release query runner when there is no migration to revert - #11232
Conversation
avoid error when there is nothing to revert because datasource is destroyed and query runner is not released fix: Error trying to revert last migration when there is none on Oracle Closes: typeorm#11231
gioboa
left a comment
There was a problem hiding this comment.
I'm wondering why pipeline tests didn't run 🤔
WalkthroughAdds an early-release of an internally created queryRunner in MigrationExecutor.undoLastMigration when no migrations exist, and introduces a test validating Oracle behavior for reverting with an empty migrations list. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller
participant MigrationExecutor
participant QueryRunner
Caller->>MigrationExecutor: undoLastMigration()
alt No external queryRunner provided
MigrationExecutor->>QueryRunner: create()
end
MigrationExecutor->>QueryRunner: getExecutedMigrations()
alt No migrations found
MigrationExecutor->>MigrationExecutor: log "Nothing to revert"
opt Executor created QueryRunner
MigrationExecutor->>QueryRunner: release()
end
MigrationExecutor-->>Caller: return
else Migrations exist
MigrationExecutor->>QueryRunner: revert last migration
opt Executor created QueryRunner
MigrationExecutor->>QueryRunner: release()
end
MigrationExecutor-->>Caller: return
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/migration/MigrationExecutor.ts (1)
433-437: Leak on early-throw path: release QueryRunner before throwing.If the last executed migration isn’t found in source, we throw before the try/finally and leak the QueryRunner (same symptom on Oracle). Release it here too.
Apply this minimal patch:
- // if no migrations found in the database then nothing to revert - if (!migrationToRevert) - throw new TypeORMError( - `No migration ${lastTimeExecutedMigration.name} was found in the source code. Make sure you have this migration in your codebase and its included in the connection options.`, - ) + // if migration is missing in source, release and throw + if (!migrationToRevert) { + if (!this.queryRunner) await queryRunner.release() + throw new TypeORMError( + `No migration ${lastTimeExecutedMigration.name} was found in the source code. Make sure you have this migration in your codebase and its included in the connection options.`, + ) + }
🧹 Nitpick comments (2)
src/migration/MigrationExecutor.ts (1)
731-744: Optional: centralize lifecycle with withQueryRunner.Consider wrapping undoLastMigration in withQueryRunner instead of manual releases to eliminate future early-exit leaks.
test/github-issues/11231/issue-11231.ts (1)
23-29: Chai misuse: to.not.throw on an awaited promise is a no-op.Either assert the promise is fulfilled (chai-as-promised) or just await destroy.
Apply one of the following:
Option A (no chai-as-promised):
- await dataSource.undoLastMigration() - expect(await dataSource.destroy()).to.not.throw + await dataSource.undoLastMigration() + await dataSource.destroy()If you take Option A, also remove the unused import:
- import { expect } from "chai" + // import removed – no assertions usedOption B (with chai-as-promised):
await dataSource.undoLastMigration() - expect(await dataSource.destroy()).to.not.throw + await expect(dataSource.destroy()).to.be.fulfilled
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/migration/MigrationExecutor.ts(1 hunks)test/github-issues/11231/issue-11231.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/github-issues/11231/issue-11231.ts (1)
test/utils/test-utils.ts (2)
createTestingConnections(388-482)closeTestingConnections(487-499)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
- GitHub Check: tests-linux (20) / sap
- GitHub Check: tests-linux (18) / postgres (14)
- GitHub Check: tests-linux (18) / postgres (17)
- GitHub Check: tests-linux (20) / cockroachdb
- GitHub Check: tests-linux (20) / mssql
- GitHub Check: tests-linux (18) / mysql_mariadb_latest
- GitHub Check: tests-linux (18) / mssql
- GitHub Check: tests-linux (18) / mysql_mariadb
- GitHub Check: tests-linux (18) / mongodb
- GitHub Check: tests-linux (18) / better-sqlite3
- GitHub Check: tests-windows / sqljs
- GitHub Check: tests-windows / sqlite
- GitHub Check: tests-windows / better-sqlite3
- GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (2)
src/migration/MigrationExecutor.ts (1)
416-422: Good fix: release QueryRunner on early return.This closes the resource leak when there’s nothing to revert and prevents Oracle’s NJS-104. Looks correct and consistent with other early-exit paths.
test/github-issues/11231/issue-11231.ts (1)
9-9: Move test to functional suite; avoid GitHub-issue-specific path/name.Per project guidance, place this under the migrations test suite and rename the describe accordingly.
Suggested describe string:
- migrations > undoLastMigration > does not leave busy connections when no migrations (oracle)
PR Code Suggestions ✨Latest suggestions up to aec9f47
Previous suggestionsSuggestions up to commit 685afba
|
||||||||||||||||||
…rm#11232) Co-authored-by: Adrien PEREZ <[email protected]>
…rm#11232) Co-authored-by: Adrien PEREZ <[email protected]>
avoid error when there is nothing to revert because datasource is destroyed and query runner is not released
fix: Error trying to revert last migration when there is none on Oracle
Closes: #11231
Description of change
Update
MigrationExectuor.undoLastMigration()to release the query runner when there is no migration to run. The query runner is released the same way it is done when there is a migration to revert.This fixes #11231 and avoid an error message on oracle database when reverting without migration to revert.
Pull-Request Checklist
masterbranchnpm run formatto apply prettier formattingnpm run testpasses with this changeFixes #11231Summary by CodeRabbit