-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check if datasource is initialized before destroying it #1269
Check if datasource is initialized before destroying it #1269
Conversation
@@ -137,7 +137,9 @@ export class TypeOrmCoreModule implements OnApplicationShutdown { | |||
getDataSourceToken(this.options as DataSourceOptions) as Type<DataSource>, | |||
); | |||
try { | |||
dataSource && (await dataSource.destroy()); | |||
if (dataSource && dataSource.isInitialized) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We call initialize()
within the DataSource provider factory. It doesn't seem to be possible then (that DataSource might not be initialized yet at this point). Can you share a minimum reproduction repository?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes let me try and create something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please give https://github.com/thomasconner/nestjs-typeorm-test a try. I have included a docker-compose.yml
file to setup Postgres. If you execute npm run test:e2e
you will see the error that I mentioned about. We ran into this with e2e tests we had at work. All the tests would pass but CircleCI would fail because of the error that gets logged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The argument can be made that we should just remove the line that destroys the datasource in our tests which would definitely solve the problem. But my reasoning for changing it here is that the expectation should be that I could call datasource.destroy()
without there being an error thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The argument can be made that we should just remove the line that destroys the datasource in our tests which would definitely solve the problem
And I think that's the valid argument TBH. You're trying to destroy a DataSource
in your test suite that has been already terminated in the onApplicationShutdown
hook, and we can't really remove it from there as we don't want folks to manually terminate data sources that were auto-created & are maintained within the module.
But my reasoning for changing it here is that the expectation should be that I could call datasource.destroy() without there being an error thrown.
What's the reason for manually calling destroy
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No specific reason to be honest. We upgraded from TypeORM v2 to v3. Before there was a connection.close()
so this was replaced, without much thought, with datasource.destroy()
. This is the only reason it existed.
Any update on this? |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
If a NestJS app destroys a
dataSource
instance before app shutdown, they will see the following error:Issue Number: N/A
What is the new behavior?
A NestJS app can destroy a
dataSource
instance before app shutdown and they will not see the following error logged:Does this PR introduce a breaking change?
Other information