From b0f636a4c44d9d14ed057ed33d095b90b4c517af Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 8 Apr 2014 08:35:39 -0700 Subject: [PATCH] Fix the query for discovery with current user See https://github.com/strongloop/loopback-connector-mysql/issues/22 --- lib/discovery.js | 2 +- test/mysql.discover.test.js | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/discovery.js b/lib/discovery.js index 60d56b2..a3c2bf0 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -35,7 +35,7 @@ function mixinDiscovery(MySQL) { + ' FROM information_schema.tables WHERE table_schema=\'' + owner + '\'', 'table_schema, table_name', options); } else { sqlTables = paginateSQL('SELECT \'table\' AS "type", table_name AS "name",' - + ' table_schema AS "owner" FROM information_schema.tables', + + ' table_schema AS "owner" FROM information_schema.tables WHERE table_schema=SUBSTRING_INDEX(USER(),\'@\',1)', 'table_name', options); } return sqlTables; diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index 7597e74..ec9c7ad 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -3,10 +3,10 @@ require('should'); var assert = require('assert'); var DataSource = require('loopback-datasource-juggler').DataSource; -var db; +var db, config; before(function () { - var config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql; + config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql; config.database = 'STRONGLOOP'; db = new DataSource(require('../'), config); }); @@ -37,6 +37,26 @@ describe('discoverModels', function () { }); }); + describe('Discover current user\'s tables', function () { + it('should return an array of tables for the current user', function (done) { + + db.discoverModelDefinitions({ + limit: 3 + }, function (err, models) { + if (err) { + console.error(err); + done(err); + } else { + var views = false; + models.forEach(function (m) { + assert.equal(m.owner, config.username); + }); + done(null, models); + } + }); + }); + }); + describe('Discover models excluding views', function () { it('should return an array of only tables', function (done) {