Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/unique index #743

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,32 @@ function mixinDiscovery(PostgreSQL) {
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}
*/
PostgreSQL.prototype.buildQueryUniqueKeys = function(schema, table) {
const sql = 'SELECT a.attname AS "columnName", n.nspname AS "owner", c.relname AS "tableName"'
+ 'FROM pg_index i'
+ 'JOIN pg_class c ON c.oid = i.indrelid'
+ 'JOIN pg_namespace n ON n.oid = c.relnamespace'
+ 'JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(i.indkey)'
+ 'WHERE i.indisunique = true'
+ 'AND i.indisprimary = false'
+ 'AND n.nspname = $1'
+ 'AND c.relname = $2'
+ 'ORDER BY a.attnum';
return {text: sql, values: [schema, table]};
};

/**
* Discover foreign keys that reference to the primary key of this table
* @param {String} table The table name
Expand Down
98 changes: 45 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"eslint": "^8.0.0",
"eslint-config-loopback": "^13.1.0",
"lodash": "^4.17.4",
"loopback-datasource-juggler": "^5.0.0",
"loopback-datasource-juggler": "^5.1.5",
"mocha": "^11.0.0",
"rc": "^1.0.0",
"should": "^13.2.3",
Expand Down
11 changes: 11 additions & 0 deletions test/postgresql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,17 @@ describe('Discover LDL schema from a table', function() {
});
});

describe('Discover unique properties', function() {
it('should validate unique key for user', function(done) {
db.discoverSchema('user', {owner: 'strongloop'}, function(err, schema) {
console.log('This is our err: ', err);
console.log('This is our schema: ', schema);
assert(schema.properties.email.index.unique, true);
done(null, schema);
});
});
});

describe('Discover and map correctly database types', function() {
it('should handle character varying, date, timestamp with time zone, timestamp without time zone', function(done) {
db.discoverSchema('customer', {owner: 'strongloop'}, function(err, schema) {
Expand Down
1 change: 1 addition & 0 deletions test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ CREATE TABLE "user" (
email character varying(100)
);

ALTER TABLE "user" ADD CONSTRAINT user_email_unique UNIQUE (email);
--
-- Name: GeoPoint_id_seq; Type: SEQUENCE; Schema: strongloop; Owner: strongloop
--
Expand Down
Loading