From 5be14bb03cca03a15be31edc885785e49711f5ef Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 14 Apr 2024 15:55:45 +0500 Subject: [PATCH] feat: query to fetch unique columns Signed-off-by: Muhammad Aaqil --- lib/discovery.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/discovery.js b/lib/discovery.js index a9cf6c2c..f5dd53e8 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -298,6 +298,29 @@ function mixinDiscovery(MySQL, mysql) { return sql; }; + /** + * Discover unique keys for a given table + * @param {String} table The table name + * @param {Object} options The options for discovery + */ + + /*! + * Retrieves a list of column names that have unique key index + * @param schema + * @param table + * @returns {string} + */ + MySQL.prototype.buildQueryUniqueKeys = function(schema, table) { + const sql = 'SELECT Column_name AS "columnName",' + + ' table_schema AS "owner",' + + ' table_name AS "tableName"' + + ' FROM Information_schema.statistics' + + ' WHERE Table_schema = ' + mysql.escape(schema) + + ' AND Table_name = ' + mysql.escape(table) + + ' AND Non_unique = 0 AND Index_name <> \'PRIMARY\';'; + return sql; + }; + /** * Discover foreign keys that reference to the primary key of this table * @param {String} table The table name