Skip to content

Commit

Permalink
fix: return auto-increment as generated
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Mar 3, 2024
1 parent 49b33f8 commit a761e7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + '\'' : ''),
Expand All @@ -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', {});
Expand Down
10 changes: 10 additions & 0 deletions test/postgresql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 9 additions & 0 deletions test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down

0 comments on commit a761e7e

Please sign in to comment.