Skip to content

Commit 9423f46

Browse files
authored
fix(postgres-driver): release method should not throw (#5395)
1 parent 4afd604 commit 9423f46

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/cubejs-postgres-driver/src/PostgresDriver.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre
6666
return 2;
6767
}
6868

69+
private enabled: boolean = false;
70+
6971
protected readonly pool: Pool;
7072

7173
protected readonly config: Partial<Config>;
@@ -107,13 +109,15 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre
107109
executionTimeout: getEnv('dbQueryTimeout', { dataSource }),
108110
...config,
109111
};
112+
this.enabled = true;
110113
}
111114

112115
/**
113116
* The easiest way how to add additional configuration from env variables, because
114117
* you cannot call method in RedshiftDriver.constructor before super.
115118
*/
116119
protected getInitialConfiguration(
120+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
117121
dataSource: string,
118122
): Partial<PostgresDriverConfiguration> {
119123
return {
@@ -334,8 +338,11 @@ export class PostgresDriver<Config extends PostgresDriverConfiguration = Postgre
334338
}
335339
}
336340

337-
public release() {
338-
return this.pool.end();
341+
public async release() {
342+
if (this.enabled) {
343+
this.pool.end();
344+
this.enabled = false;
345+
}
339346
}
340347

341348
public param(paramIndex: number) {

packages/cubejs-postgres-driver/test/PostgresDriver.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { PostgresDBRunner } from '@cubejs-backend/testing-shared';
2-
32
import { StartedTestContainer } from 'testcontainers';
4-
53
import { PostgresDriver } from '../src';
64

75
const streamToArray = require('stream-to-array');
@@ -25,11 +23,7 @@ describe('PostgresDriver', () => {
2523
});
2624

2725
afterAll(async () => {
28-
await driver.release();
29-
30-
if (container) {
31-
await container.stop();
32-
}
26+
await container.stop();
3327
});
3428

3529
test('type coercion', async () => {
@@ -121,4 +115,19 @@ describe('PostgresDriver', () => {
121115
);
122116
}
123117
});
118+
119+
// Note: This test MUST be the last in the list.
120+
test('release', async () => {
121+
expect(async () => {
122+
await driver.release();
123+
}).not.toThrowError(
124+
/Called end on pool more than once/
125+
);
126+
127+
expect(async () => {
128+
await driver.release();
129+
}).not.toThrowError(
130+
/Called end on pool more than once/
131+
);
132+
});
124133
});

0 commit comments

Comments
 (0)