Skip to content

Commit 2386245

Browse files
aaqilnizsamarpanB
authored andcommitted
feat: query to fetch unique columns
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 3596be1 commit 2386245

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

lib/discovery.js

+23
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,29 @@ function mixinDiscovery(MySQL, mysql) {
301301
return sql;
302302
};
303303

304+
/**
305+
* Discover unique keys for a given table
306+
* @param {String} table The table name
307+
* @param {Object} options The options for discovery
308+
*/
309+
310+
/*!
311+
* Retrieves a list of column names that have unique key index
312+
* @param schema
313+
* @param table
314+
* @returns {string}
315+
*/
316+
MySQL.prototype.buildQueryUniqueKeys = function(schema, table) {
317+
const sql = 'SELECT Column_name AS "columnName",' +
318+
' table_schema AS "owner",' +
319+
' table_name AS "tableName"' +
320+
' FROM Information_schema.statistics' +
321+
' WHERE Table_schema = ' + mysql.escape(schema) +
322+
' AND Table_name = ' + mysql.escape(table) +
323+
' AND Non_unique = 0 AND Index_name <> \'PRIMARY\';';
324+
return sql;
325+
};
326+
304327
/**
305328
* Discover foreign keys that reference to the primary key of this table
306329
* @param {String} table The table name

package-lock.json

+19-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@commitlint/config-conventional": "^19.6.0",
3434
"eslint": "^8.57.1",
3535
"eslint-config-loopback": "^13.1.0",
36-
"loopback-datasource-juggler": "^5.1.3",
36+
"loopback-datasource-juggler": "^5.1.4",
3737
"mocha": "^11.1.0",
3838
"rc": "^1.2.8",
3939
"should": "^13.2.3",

test/mysql.discover.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,21 @@ describe('Discover LDL schema from a table', function() {
283283
});
284284
});
285285

286+
describe('Discover unique properties', function() {
287+
let schema;
288+
before(function(done) {
289+
db.discoverSchema('CUSTOMER', {owner: 'STRONGLOOP'}, function(err, schema_) {
290+
schema = schema_;
291+
done(err);
292+
});
293+
});
294+
it('should validate unique key for customer', function() {
295+
assert.ok(/STRONGLOOP/i.test(schema.options.mysql.schema));
296+
assert.strictEqual(schema.options.mysql.table, 'CUSTOMER');
297+
assert.strictEqual(schema.properties.email.index.unique, true);
298+
});
299+
});
300+
286301
describe('Discover and handle enum', function() {
287302
let schema;
288303
before(function(done) {

test/schema.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ CREATE TABLE `CUSTOMER` (
3939
PRIMARY KEY (`ID`)
4040
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
4141
/*!40101 SET character_set_client = @saved_cs_client */;
42-
42+
ALTER TABLE `CUSTOMER`
43+
ADD COLUMN `email` VARCHAR(100) UNIQUE;
4344
--
4445
-- Dumping data for table `CUSTOMER`
4546
--

0 commit comments

Comments
 (0)