Skip to content

Commit 458c0c9

Browse files
berndartmuellerpaveltiunov
authored andcommitted
fix: Fix postgres driver timestamp parsing by using pg per-query type parser (#269) Thanks to @berndartmueller!
* fix: replace global postgres timestamp type parser with per-query type parser * style: fix eslint errors * use pg-types default parser as fallback * fix lint error Fixes #265
1 parent 6dc3fef commit 458c0c9

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

packages/cubejs-postgres-driver/driver/PostgresDriver.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const pg = require('pg');
2+
const { types } = require('pg');
23
const moment = require('moment');
34
const BaseDriver = require('@cubejs-backend/query-orchestrator/driver/BaseDriver');
45

@@ -8,8 +9,9 @@ const GenericTypeToPostgres = {
89
string: 'text'
910
};
1011

11-
pg.types.setTypeParser(1114, str => moment.utc(str).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
12-
pg.types.setTypeParser(1184, str => moment.utc(str).format(moment.HTML5_FMT.DATETIME_LOCAL_MS));
12+
const timestampDataTypes = [1114, 1184];
13+
14+
const timestampTypeParser = val => moment.utc(val).format(moment.HTML5_FMT.DATETIME_LOCAL_MS);
1315

1416
class PostgresDriver extends BaseDriver {
1517
constructor(config) {
@@ -49,7 +51,19 @@ class PostgresDriver extends BaseDriver {
4951
await client.query("set statement_timeout to 600000");
5052
const res = await client.query({
5153
text: query,
52-
values: values || []
54+
values: values || [],
55+
types: {
56+
getTypeParser: (dataType, format) => {
57+
const isTimestamp = timestampDataTypes.indexOf(dataType) > -1;
58+
let parser = types.getTypeParser(dataType, format);
59+
60+
if (isTimestamp) {
61+
parser = timestampTypeParser;
62+
}
63+
64+
return val => parser(val);
65+
},
66+
},
5367
});
5468
return res && res.rows;
5569
} finally {

packages/cubejs-postgres-driver/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"@cubejs-backend/query-orchestrator": "^0.11.18",
2020
"moment": "^2.24.0",
21-
"pg": "^7.8.0"
21+
"pg": "^7.10.0"
2222
},
2323
"license": "Apache-2.0",
2424
"devDependencies": {

packages/cubejs-postgres-driver/yarn.lock

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
esutils "^2.0.2"
1919
js-tokens "^4.0.0"
2020

21-
"@cubejs-backend/query-orchestrator@^0.11.0":
22-
version "0.11.0"
23-
resolved "https://registry.yarnpkg.com/@cubejs-backend/query-orchestrator/-/query-orchestrator-0.11.0.tgz#8cc898038be6a4da59e2dcbcb0f1a8d5631a66ef"
24-
integrity sha512-uOByU8i2oUNVy1CgWZx8F/JoxXbERmskewkpJD3Kwwx3Pe8kKzAOGj7TtL/9Nrqcq/MRgJwHSLCTlxmDv2x/9g==
21+
"@cubejs-backend/query-orchestrator@^0.11.18":
22+
version "0.11.18"
23+
resolved "https://registry.yarnpkg.com/@cubejs-backend/query-orchestrator/-/query-orchestrator-0.11.18.tgz#1d0f75819159a5eb9fd4e75cbb74a21f8f4fe3f4"
24+
integrity sha512-V7m2T13AceIW2zVSWBbdXZH80/SA3lSkrc0EloZd+1djhdjfUV6jLn5HLN25BK9oucS1tfQ9igI82pO/yX5K9Q==
2525
dependencies:
2626
ramda "^0.24.1"
2727
redis "^2.8.0"
@@ -852,10 +852,10 @@ p-try@^1.0.0:
852852
resolved "https://registry.yarnpkg.com/p-try/-/p-try-1.0.0.tgz#cbc79cdbaf8fd4228e13f621f2b1a237c1b207b3"
853853
integrity sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=
854854

855-
packet-reader@0.3.1:
856-
version "0.3.1"
857-
resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-0.3.1.tgz#cd62e60af8d7fea8a705ec4ff990871c46871f27"
858-
integrity sha1-zWLmCvjX/qinBexP+ZCHHEaHHyc=
855+
packet-reader@1.0.0:
856+
version "1.0.0"
857+
resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-1.0.0.tgz#9238e5480dedabacfe1fe3f2771063f164157d74"
858+
integrity sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==
859859

860860
parent-module@^1.0.0:
861861
version "1.0.1"
@@ -918,27 +918,27 @@ pg-pool@^2.0.4:
918918
resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-2.0.6.tgz#7b561a482feb0a0e599b58b5137fd2db3ad8111c"
919919
integrity sha512-hod2zYQxM8Gt482q+qONGTYcg/qVcV32VHVPtktbBJs0us3Dj7xibISw0BAAXVMCzt8A/jhfJvpZaxUlqtqs0g==
920920

921-
pg-types@~2.0.0:
922-
version "2.0.0"
923-
resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-2.0.0.tgz#038ddc302a0340efcdb46d0581cc7caa2303cbba"
924-
integrity sha512-THUD7gQll5tys+5eQ8Rvs7DjHiIC3bLqixk3gMN9Hu8UrCBAOjf35FoI39rTGGc3lM2HU/R+Knpxvd11mCwOMA==
921+
pg-types@^2.1.0:
922+
version "2.2.0"
923+
resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-2.2.0.tgz#2d0250d636454f7cfa3b6ae0382fdfa8063254a3"
924+
integrity sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==
925925
dependencies:
926926
pg-int8 "1.0.1"
927927
postgres-array "~2.0.0"
928928
postgres-bytea "~1.0.0"
929-
postgres-date "~1.0.0"
929+
postgres-date "~1.0.4"
930930
postgres-interval "^1.1.0"
931931

932-
pg@^7.8.0:
933-
version "7.8.0"
934-
resolved "https://registry.yarnpkg.com/pg/-/pg-7.8.0.tgz#541c25b3323d85f67ce7d4501a77470976868ce9"
935-
integrity sha512-yS3C9YD+ft0H7G47uU0eKajgTieggCXdA+Fxhm5G+wionY6kPBa8BEVDwPLMxQvkRkv3/LXiFEqjZm9gfxdW+g==
932+
pg@^7.10.0:
933+
version "7.12.1"
934+
resolved "https://registry.yarnpkg.com/pg/-/pg-7.12.1.tgz#880636d46d2efbe0968e64e9fe0eeece8ef72a7e"
935+
integrity sha512-l1UuyfEvoswYfcUe6k+JaxiN+5vkOgYcVSbSuw3FvdLqDbaoa2RJo1zfJKfPsSYPFVERd4GHvX3s2PjG1asSDA==
936936
dependencies:
937937
buffer-writer "2.0.0"
938-
packet-reader "0.3.1"
938+
packet-reader "1.0.0"
939939
pg-connection-string "0.1.3"
940940
pg-pool "^2.0.4"
941-
pg-types "~2.0.0"
941+
pg-types "^2.1.0"
942942
pgpass "1.x"
943943
semver "4.3.2"
944944

@@ -971,10 +971,10 @@ postgres-bytea@~1.0.0:
971971
resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35"
972972
integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=
973973

974-
postgres-date@~1.0.0:
975-
version "1.0.3"
976-
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.3.tgz#e2d89702efdb258ff9d9cee0fe91bd06975257a8"
977-
integrity sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=
974+
postgres-date@~1.0.4:
975+
version "1.0.4"
976+
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.4.tgz#1c2728d62ef1bff49abdd35c1f86d4bdf118a728"
977+
integrity sha512-bESRvKVuTrjoBluEcpv2346+6kgB7UlnqWZsnbnCccTNq/pqfj1j6oBaN5+b/NrDXepYUT/HKadqv3iS9lJuVA==
978978

979979
postgres-interval@^1.1.0:
980980
version "1.1.2"

0 commit comments

Comments
 (0)