From cdf35c579f40a08f7f1388fc7cc5471bcb9eb594 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Fri, 13 Oct 2023 19:32:11 +0500 Subject: [PATCH] fix: add order by ordinal position for query columns Signed-off-by: Muhammad Aaqil --- lib/discovery.js | 1 + test/mysql.discover.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/discovery.js b/lib/discovery.js index d765ea945..e21a74244 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -186,6 +186,7 @@ function mixinDiscovery(MySQL, mysql) { (table ? ' WHERE table_name=' + mysql.escape(table) : ''), 'table_name, ordinal_position', {}); } + sql += ' ORDER BY ordinal_position'; return sql; }; diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index a92d67c55..feb90ee17 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -125,6 +125,32 @@ describe('Discover models including other users', function() { }); describe('Discover model properties', function() { + describe('Discover model properties in Ordinal Order', function() { + it('should return an array of columns for product in ordinal order', function(done) { + const productProperties = []; + const productPropertiesInOrdinalOrder = [ + 'ID', + 'NAME', + 'AUDIBLE_RANGE', + 'EFFECTIVE_RANGE', + 'ROUNDS', + 'EXTRAS', + 'FIRE_MODES', + ]; + db.discoverModelProperties('PRODUCT', function(err, models) { + if (err) { + console.error(err); + done(err); + } else { + models.forEach(function(m) { productProperties.push(m.columnName); }); + productProperties.forEach((prop, index) => { + assert(productPropertiesInOrdinalOrder[index] === prop); + }); + done(null, models); + } + }); + }); + }); describe('Discover a named model', function() { it('should return an array of columns for product', function(done) { db.discoverModelProperties('product', function(err, models) {