Skip to content

Commit 7942e74

Browse files
committed
feat: query to fetch unique columns
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 23e3874 commit 7942e74

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/discovery.js

+34
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,40 @@ function mixinDiscovery(PostgreSQL) {
335335
return sql;
336336
};
337337

338+
/**
339+
* Discover unique keys for a given table
340+
* @param {String} table The table name
341+
* @param {Object} options The options for discovery
342+
*/
343+
344+
/*!
345+
* Retrieves a list of column names that have unique key index
346+
* @param schema
347+
* @param table
348+
* @returns {string}
349+
*/
350+
PostgreSQL.prototype.buildQueryUniqueKeys = function(schema, table) {
351+
const sql = `
352+
SELECT
353+
a.attname AS "columnName",
354+
n.nspname AS "owner",
355+
c.relname AS "tableName"
356+
FROM pg_index i
357+
JOIN pg_class c ON c.oid = i.indrelid
358+
JOIN pg_namespace n ON n.oid = c.relnamespace
359+
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(i.indkey)
360+
WHERE i.indisunique = true
361+
AND i.indisprimary = false
362+
AND n.nspname = $1
363+
AND c.relname = $2
364+
ORDER BY a.attnum;
365+
`;
366+
return {
367+
text: sql,
368+
values: [schema, table]
369+
};
370+
};
371+
338372
/**
339373
* Discover foreign keys that reference to the primary key of this table
340374
* @param {String} table The table name

0 commit comments

Comments
 (0)