diff --git a/Makefile b/Makefile index 59d4bd3..257c99a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ params := $(connectionString) node-command := xargs -n 1 -I file node file $(params) .PHONY : test test-connection test-integration bench test-native \ - build/default/binding.node jshint upgrade-pg publish + build/default/binding.node jshint upgrade-pg publish test-missing-native all: npm install @@ -21,7 +21,7 @@ help: test: test-unit -test-all: jshint test-unit test-integration test-native test-binary +test-all: jshint test-missing-native test-unit test-integration test-native test-binary test-travis: test-all upgrade-pg #@make test-all connectionString=postgres://postgres@localhost:5433/postgres @@ -44,7 +44,18 @@ test-connection-binary: @echo "***Testing binary connection***" @node script/test-connection.js $(params) binary -test-native: +test-missing-native: + @echo "***Testing optional native install***" + @rm -rf node_modules/pg-native + @node test/native/missing-native.js + @npm install pg-native@1.4.0 + @node test/native/missing-native.js + @rm -rf node_modules/pg-native + +node_modules/pg-native/index.js: + @npm i pg-native + +test-native: node_modules/pg-native/index.js @echo "***Testing native bindings***" @find test/native -name "*-tests.js" | $(node-command) @find test/integration -name "*-tests.js" | $(node-command) native diff --git a/lib/index.js b/lib/index.js index b5337e3..e11334d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -67,16 +67,15 @@ PG.prototype.cancel = function(config, client, query) { cancellingClient.cancel(client, query); }; -var forceNative = Object.prototype.hasOwnProperty.call(process.env, 'NODE_PG_FORCE_NATIVE'); -if (forceNative) { - module.exports = new PG(require(__dirname + '/native')); +if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') { + module.exports = new PG(require('./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')); + module.exports.native = new PG(require('./native')); return module.exports.native; }); } diff --git a/lib/native/index.js b/lib/native/index.js index dd708be..cc510ef 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -1,8 +1,13 @@ var Native = require('pg-native'); +var semver = require('semver'); +var pkg = require('../../package.json'); +var assert = require('assert'); var EventEmitter = require('events').EventEmitter; var util = require('util'); var ConnectionParameters = require(__dirname + '/../connection-parameters'); +assert(semver.gte(Native.version, pkg.minNativeVersion)); + var NativeQuery = require('./query'); var Client = module.exports = function(config) { diff --git a/lib/native/require.js b/lib/native/require.js new file mode 100644 index 0000000..e69c4e1 --- /dev/null +++ b/lib/native/require.js @@ -0,0 +1,4 @@ + +var pgNative = require('pg-native'); +assert(semver.gte(pgNative.version, pkg.minNativeVersion)); +module.exports = require('./index'); diff --git a/package.json b/package.json index ab83ffb..8fa9fe2 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "nan": "1.3.0", "packet-reader": "0.2.0", "pg-connection-string": "0.1.3", - "pg-native": "1.4.3", "pg-types": "1.6.0", "pgpass": "0.0.3" }, @@ -32,6 +31,7 @@ "jshint": "2.5.2", "semver": "~3.0.1" }, + "minNativeVersion": "1.5.0", "scripts": { "changelog": "npm i github-changes && ./node_modules/.bin/github-changes -o brianc -r node-postgres -d pulls -a -v", "test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres" diff --git a/test/native/missing-native.js b/test/native/missing-native.js new file mode 100644 index 0000000..b13a631 --- /dev/null +++ b/test/native/missing-native.js @@ -0,0 +1,8 @@ +//this test assumes it has been run from the Makefile +//and that node_modules/pg-native has been deleted + +var assert = require('assert'); + +assert.throws(function() { + require('../../lib').native; +});