From c126ba1c7c95a3e8b466710fae1807f9fba6e470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Wed, 26 Jun 2013 22:32:07 +0200 Subject: [PATCH 1/7] Added NODE_PG_FORCE_NATIVE to force usage of libpg bindings (native client) --- lib/index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index bb2041bf..6bd6838d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -52,12 +52,15 @@ PG.prototype.cancel = function(config, client, query) { cancellingClient.cancel(client, query); }; -module.exports = new PG(Client); - -//lazy require native module...the native module may not have installed -module.exports.__defineGetter__("native", function() { - delete module.exports.native; - module.exports.native = new PG(require(__dirname + '/native')); - return module.exports.native; -}); +if (process.env.hasOwnProperty('NODE_PG_FORCE_NATIVE')) { + module.exports = new PG(require(__dirname + '/native')); +} else { + module.exports = new PG(Client); + //lazy require native module...the native module may not have installed + module.exports.__defineGetter__("native", function() { + delete module.exports.native; + module.exports.native = new PG(require(__dirname + '/native')); + return module.exports.native; + }); +} From e9cb2965e9ba1a8c0d95475091f1aa21fd54c263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Wed, 26 Jun 2013 23:46:15 +0200 Subject: [PATCH 2/7] Bugfix: safe call of .hasOwnProperty(...) --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 6bd6838d..5f1cea07 100644 --- a/lib/index.js +++ b/lib/index.js @@ -52,7 +52,8 @@ PG.prototype.cancel = function(config, client, query) { cancellingClient.cancel(client, query); }; -if (process.env.hasOwnProperty('NODE_PG_FORCE_NATIVE')) { +var forceNative = Object.prototype.hasOwnProperty.call(process.env, 'NODE_PG_FORCE_NATIVE'); +if (forceNative) { module.exports = new PG(require(__dirname + '/native')); } else { module.exports = new PG(Client); From 37f4d504d20ffc10062f88d68165a135b0ebf9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Thu, 27 Jun 2013 01:37:54 +0200 Subject: [PATCH 3/7] added test case for NODE_PG_FORCE_NATIVE --- test/integration/client/force-native-with-envvar.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/integration/client/force-native-with-envvar.js diff --git a/test/integration/client/force-native-with-envvar.js b/test/integration/client/force-native-with-envvar.js new file mode 100644 index 00000000..9c8ec87d --- /dev/null +++ b/test/integration/client/force-native-with-envvar.js @@ -0,0 +1,10 @@ +// if (!assert) var assert = require('assert'); + +process.env.NODE_PG_FORCE_NATIVE = true; + +var pg = require('../../../lib/'); +var query_native = require('../../../lib/native/query.js'); +var query_js = require('../../../lib/query.js'); + +assert.deepEqual(pg.Client.Query, query_native); +assert.notDeepEqual(pg.Client.Query, query_js); From 0b149e66192145590f8c0dbe9a52f67072a3bc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Thu, 27 Jun 2013 01:46:37 +0200 Subject: [PATCH 4/7] fixed wrong name for test file --- ...rce-native-with-envvar.js => force-native-with-envvar-test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/integration/client/{force-native-with-envvar.js => force-native-with-envvar-test.js} (100%) diff --git a/test/integration/client/force-native-with-envvar.js b/test/integration/client/force-native-with-envvar-test.js similarity index 100% rename from test/integration/client/force-native-with-envvar.js rename to test/integration/client/force-native-with-envvar-test.js From 7103c044f15d7389bd38d3dbd7e974a65a88a510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Thu, 27 Jun 2013 01:51:02 +0200 Subject: [PATCH 5/7] fixed wrong name for test file ... again --- ...tive-with-envvar-test.js => force-native-with-envvar-tests.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/integration/client/{force-native-with-envvar-test.js => force-native-with-envvar-tests.js} (100%) diff --git a/test/integration/client/force-native-with-envvar-test.js b/test/integration/client/force-native-with-envvar-tests.js similarity index 100% rename from test/integration/client/force-native-with-envvar-test.js rename to test/integration/client/force-native-with-envvar-tests.js From 0d1054a87446fe73d00fc19621aeb8bc9ebf3354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Thu, 27 Jun 2013 02:40:42 +0200 Subject: [PATCH 6/7] remove modules from the cache & load test-helper --- .../client/force-native-with-envvar-tests.js | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/test/integration/client/force-native-with-envvar-tests.js b/test/integration/client/force-native-with-envvar-tests.js index 9c8ec87d..93b9dddc 100644 --- a/test/integration/client/force-native-with-envvar-tests.js +++ b/test/integration/client/force-native-with-envvar-tests.js @@ -1,10 +1,34 @@ -// if (!assert) var assert = require('assert'); +var helper = require(__dirname+"/test-helper") + , path = require('path') +; -process.env.NODE_PG_FORCE_NATIVE = true; +var paths = { + 'pg' : path.join(__dirname, '..', '..', '..', 'lib', 'index.js') , + 'query_js' : path.join(__dirname, '..', '..', '..', 'lib', 'query.js') , + 'query_native' : path.join(__dirname, '..', '..', '..', 'lib', 'native', 'query.js') , +}; -var pg = require('../../../lib/'); -var query_native = require('../../../lib/native/query.js'); -var query_js = require('../../../lib/query.js'); +/** + * delete the modules we are concerned about from the + * module cache + */ +function deleteFromCache(){ + Object.keys(paths).forEach(function(module){ + var cache_key = paths[ module ]; + delete require.cache[ cache_key ]; + }); +}; + + +deleteFromCache(); +process.env.NODE_PG_FORCE_NATIVE = "1"; + +var pg = require( paths.pg ); +var query_native = require( paths.query_native ); +var query_js = require( paths.query_js ); assert.deepEqual(pg.Client.Query, query_native); assert.notDeepEqual(pg.Client.Query, query_js); + +deleteFromCache(); +delete process.env.NODE_PG_FORCE_NATIVE From b313a392a7d2a35abf6ad11f06e4fb81d58141d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20H=C3=B6rl?= Date: Sat, 29 Jun 2013 10:15:39 +0200 Subject: [PATCH 7/7] delete the entire module cache --- .../client/force-native-with-envvar-tests.js | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/integration/client/force-native-with-envvar-tests.js b/test/integration/client/force-native-with-envvar-tests.js index 93b9dddc..e41587d7 100644 --- a/test/integration/client/force-native-with-envvar-tests.js +++ b/test/integration/client/force-native-with-envvar-tests.js @@ -1,3 +1,8 @@ +/** + * helper needs to be loaded for the asserts but it alos proloads + * client which we don't want here + * + */ var helper = require(__dirname+"/test-helper") , path = require('path') ; @@ -10,18 +15,17 @@ var paths = { /** * delete the modules we are concerned about from the - * module cache + * module cache, so they get loaded cleanly and the env + * var can kick in ... */ -function deleteFromCache(){ - Object.keys(paths).forEach(function(module){ - var cache_key = paths[ module ]; - delete require.cache[ cache_key ]; +function emptyCache(){ + Object.keys(require.cache).forEach(function(key){ + delete require.cache[key]; }); }; - -deleteFromCache(); -process.env.NODE_PG_FORCE_NATIVE = "1"; +emptyCache(); +process.env.NODE_PG_FORCE_NATIVE = '1'; var pg = require( paths.pg ); var query_native = require( paths.query_native ); @@ -30,5 +34,5 @@ var query_js = require( paths.query_js ); assert.deepEqual(pg.Client.Query, query_native); assert.notDeepEqual(pg.Client.Query, query_js); -deleteFromCache(); +emptyCache(); delete process.env.NODE_PG_FORCE_NATIVE