From 52f67d3403d5996aa74c9fc833f748030e533315 Mon Sep 17 00:00:00 2001 From: Shayon Mukherjee Date: Sat, 29 Aug 2020 18:10:24 -0700 Subject: [PATCH 1/3] Support idle_in_transaction_session_timeout and statement_timeout for native driver Right now when using `pg` with `pg-native` the config options `idle_in_transaction_session_timeout` and `statement_timeout` do not get applied. This is due to, when building the connection string for native driver these options aren't passed in. I took advantage of `options` conn parameter and passed both the config options into it using `-c` which postgres supports. This builds on top of the work happened in . Also added some tests. Very much open to feedback and comments :). --- packages/pg/lib/connection-parameters.js | 27 +++++++++++++++++++ .../connection-parameters/creation-tests.js | 20 ++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/pg/lib/connection-parameters.js b/packages/pg/lib/connection-parameters.js index 62bee8c85..8258fd3d4 100644 --- a/packages/pg/lib/connection-parameters.js +++ b/packages/pg/lib/connection-parameters.js @@ -45,6 +45,21 @@ var add = function (params, config, paramName) { } } +var escapeOptionValue = function (value) { + return ('' + value).replace(/\\/g, '\\\\').replace(/ /g, '\\ ') +} + +var addOption = function (options, config, optionName) { + if (!config[optionName]) { + return + } + + var value = config[optionName] + if (value !== undefined && value !== null) { + options.push('-c ' + optionName + '=' + escapeOptionValue(value)) + } +} + class ConnectionParameters { constructor(config) { // if a string is passed, it is a raw connection string so we parse it into a config @@ -120,6 +135,8 @@ class ConnectionParameters { getLibpqConnectionString(cb) { var params = [] + var pgOptions = [] + add(params, this, 'user') add(params, this, 'password') add(params, this, 'port') @@ -128,6 +145,16 @@ class ConnectionParameters { add(params, this, 'connect_timeout') add(params, this, 'options') + addOption(pgOptions, this, 'statement_timeout') + addOption(pgOptions, this, 'idle_in_transaction_session_timeout') + + if (this.options) { + pgOptions.push(this.options) + } + if (pgOptions.length > 0) { + params.push('options=' + quoteParamValue(pgOptions.join(' '))) + } + var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {} add(params, ssl, 'sslmode') add(params, ssl, 'sslca') diff --git a/packages/pg/test/unit/connection-parameters/creation-tests.js b/packages/pg/test/unit/connection-parameters/creation-tests.js index e4dd1af72..1432b2883 100644 --- a/packages/pg/test/unit/connection-parameters/creation-tests.js +++ b/packages/pg/test/unit/connection-parameters/creation-tests.js @@ -172,6 +172,26 @@ test('libpq connection string building', function () { ) }) + test('builds conn string with options', function () { + var config = { + user: 'brian', + password: 'xyz', + port: 888, + host: 'localhost', + database: 'bam', + statement_timeout: 5000, + idle_in_transaction_session_timeout: 5000, + } + var subject = new ConnectionParameters(config) + subject.getLibpqConnectionString( + assert.calls(function (err, constring) { + assert(!err) + var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/) + checkForPart(parts, "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000'") + }) + ) + }) + test('builds dns string', function () { var config = { user: 'brian', From e3f89ce5d9ce8df38c87f9df848b0e5bbe314b71 Mon Sep 17 00:00:00 2001 From: Shayon Mukherjee Date: Sat, 24 Oct 2020 13:39:44 -0700 Subject: [PATCH 2/3] Skip adding options twice and extend specs --- packages/pg/lib/connection-parameters.js | 1 - .../connection-parameters/creation-tests.js | 24 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/pg/lib/connection-parameters.js b/packages/pg/lib/connection-parameters.js index 8258fd3d4..c5914a1a7 100644 --- a/packages/pg/lib/connection-parameters.js +++ b/packages/pg/lib/connection-parameters.js @@ -143,7 +143,6 @@ class ConnectionParameters { add(params, this, 'application_name') add(params, this, 'fallback_application_name') add(params, this, 'connect_timeout') - add(params, this, 'options') addOption(pgOptions, this, 'statement_timeout') addOption(pgOptions, this, 'idle_in_transaction_session_timeout') diff --git a/packages/pg/test/unit/connection-parameters/creation-tests.js b/packages/pg/test/unit/connection-parameters/creation-tests.js index 1432b2883..f4229aecc 100644 --- a/packages/pg/test/unit/connection-parameters/creation-tests.js +++ b/packages/pg/test/unit/connection-parameters/creation-tests.js @@ -172,7 +172,7 @@ test('libpq connection string building', function () { ) }) - test('builds conn string with options', function () { + test('builds conn string with options and statement_timeout', function () { var config = { user: 'brian', password: 'xyz', @@ -181,13 +181,33 @@ test('libpq connection string building', function () { database: 'bam', statement_timeout: 5000, idle_in_transaction_session_timeout: 5000, + options: '-c geqo=off -c foobar=off', } var subject = new ConnectionParameters(config) subject.getLibpqConnectionString( assert.calls(function (err, constring) { assert(!err) var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/) - checkForPart(parts, "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000'") + checkForPart(parts, "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000 -c geqo=off -c foobar=off'") + }) + ) + }) + + test('builds conn string with options and without statement_timeout', function () { + var config = { + user: 'brian', + password: 'xyz', + port: 888, + host: 'localhost', + database: 'bam', + options: '-c geqo=off -c foobar=off', + } + var subject = new ConnectionParameters(config) + subject.getLibpqConnectionString( + assert.calls(function (err, constring) { + assert(!err) + var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/) + checkForPart(parts, "options='-c geqo=off -c foobar=off'") }) ) }) From 2f5e812429baf93feb66d045a998a31adf6fec1c Mon Sep 17 00:00:00 2001 From: Shayon Mukherjee Date: Sun, 25 Oct 2020 16:38:54 -0700 Subject: [PATCH 3/3] Lint fix --- .../pg/test/unit/connection-parameters/creation-tests.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/pg/test/unit/connection-parameters/creation-tests.js b/packages/pg/test/unit/connection-parameters/creation-tests.js index f4229aecc..e30194e8b 100644 --- a/packages/pg/test/unit/connection-parameters/creation-tests.js +++ b/packages/pg/test/unit/connection-parameters/creation-tests.js @@ -188,7 +188,10 @@ test('libpq connection string building', function () { assert.calls(function (err, constring) { assert(!err) var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/) - checkForPart(parts, "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000 -c geqo=off -c foobar=off'") + checkForPart( + parts, + "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000 -c geqo=off -c foobar=off'" + ) }) ) })