Skip to content
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

Conversation

thomasconner
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

If a NestJS app destroys a dataSource instance before app shutdown, they will see the following error:

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

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:

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

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@@ -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) {
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

@thomasconner
Copy link
Contributor Author

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants