Skip to content

Commit 77b28d1

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

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

lib/discovery.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -348,25 +348,17 @@ function mixinDiscovery(PostgreSQL) {
348348
* @returns {string}
349349
*/
350350
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-
};
351+
const sql = 'SELECT a.attname AS "columnName", n.nspname AS "owner", c.relname AS "tableName"'
352+
+ 'FROM pg_index i'
353+
+ 'JOIN pg_class c ON c.oid = i.indrelid'
354+
+ 'JOIN pg_namespace n ON n.oid = c.relnamespace'
355+
+ 'JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(i.indkey)'
356+
+ 'WHERE i.indisunique = true'
357+
+ 'AND i.indisprimary = false'
358+
+ 'AND n.nspname = $1'
359+
+ 'AND c.relname = $2'
360+
+ 'ORDER BY a.attnum';
361+
return {text: sql, values: [schema, table]};
370362
};
371363

372364
/**

test/postgresql.discover.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ describe('Discover LDL schema from a table', function() {
394394
});
395395
});
396396

397+
describe('Discover unique properties', function() {
398+
it('should validate unique key for user', function(done) {
399+
db.discoverSchema('user', {owner: 'strongloop'}, function(err, schema) {
400+
console.log('This is our err: ', err);
401+
console.log('This is our schema: ', schema);
402+
assert(schema.properties.email.index.unique, true);
403+
done(null, schema);
404+
});
405+
});
406+
});
407+
397408
describe('Discover and map correctly database types', function() {
398409
it('should handle character varying, date, timestamp with time zone, timestamp without time zone', function(done) {
399410
db.discoverSchema('customer', {owner: 'strongloop'}, function(err, schema) {

test/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ CREATE TABLE "user" (
276276
email character varying(100)
277277
);
278278

279+
ALTER TABLE "user" ADD CONSTRAINT user_email_unique UNIQUE (email);
279280
--
280281
-- Name: GeoPoint_id_seq; Type: SEQUENCE; Schema: strongloop; Owner: strongloop
281282
--

0 commit comments

Comments
 (0)