From a761e7e99bd3d9079ac0cdc3299c05810aa9d147 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sun, 28 Jan 2024 21:38:14 +0500 Subject: [PATCH] fix: return auto-increment as generated Signed-off-by: Muhammad Aaqil --- lib/discovery.js | 8 +++++--- test/postgresql.discover.test.js | 10 ++++++++++ test/schema.sql | 9 +++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/discovery.js b/lib/discovery.js index e30aeb8b..0393c524 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -137,7 +137,7 @@ function mixinDiscovery(PostgreSQL) { results.map(function(r) { // PostgreSQL returns ALWAYS in case the property is generated, // otherwise it returns NEVER - if (r.generated === 'ALWAYS') { + if (r.generated === 'ALWAYS' || r.identityGenerated === 'ALWAYS') { r.generated = true; } else { r.generated = false; @@ -173,8 +173,9 @@ function mixinDiscovery(PostgreSQL) { if (owner) { sql = this.paginateSQL('SELECT table_schema AS "owner", table_name AS "tableName", column_name AS "columnName",' + 'data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",' + + ' numeric_scale AS "dataScale", is_nullable AS "nullable",' + ' is_generated AS "generated",' - + ' numeric_scale AS "dataScale", is_nullable AS "nullable"' + + ' identity_generation AS "identityGenerated"' + ' FROM information_schema.columns' + ' WHERE table_schema=\'' + owner + '\'' + (table ? ' AND table_name=\'' + table + '\'' : ''), @@ -183,8 +184,9 @@ function mixinDiscovery(PostgreSQL) { sql = this.paginateSQL('SELECT current_schema() AS "owner", table_name AS "tableName",' + ' column_name AS "columnName",' + ' data_type AS "dataType", character_maximum_length AS "dataLength", numeric_precision AS "dataPrecision",' + + ' numeric_scale AS "dataScale", is_nullable AS "nullable",' + ' is_generated AS "generated",' - + ' numeric_scale AS "dataScale", is_nullable AS "nullable"' + + ' identity_generation AS "identityGenerated"' + ' FROM information_schema.columns' + (table ? ' WHERE table_name=\'' + table + '\'' : ''), 'table_name, ordinal_position', {}); diff --git a/test/postgresql.discover.test.js b/test/postgresql.discover.test.js index 729d434b..74aadce0 100644 --- a/test/postgresql.discover.test.js +++ b/test/postgresql.discover.test.js @@ -382,6 +382,16 @@ describe('Discover LDL schema from a table', function() { done(null, schema); }); }); + + it('should return an LDL schema for user which has id as generated', 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.id.generated); + assert(typeof schema.properties.id.generated === 'boolean'); + done(null, schema); + }); + }); }); describe('Discover and map correctly database types', function() { diff --git a/test/schema.sql b/test/schema.sql index fdbdacd4..d6212895 100644 --- a/test/schema.sql +++ b/test/schema.sql @@ -267,6 +267,15 @@ CREATE TABLE "GeoPoint" ( ); +-- +-- Name: user; Type: TABLE; Schema: strongloop; Owner: strongloop +-- + +CREATE TABLE "user" ( + id integer NOT NULL GENERATED ALWAYS AS IDENTITY, + email character varying(100) +); + -- -- Name: GeoPoint_id_seq; Type: SEQUENCE; Schema: strongloop; Owner: strongloop --