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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/typeorm-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

await dataSource.destroy();
}
} catch (e) {
this.logger.error(e?.message);
}
Expand Down