diff --git a/src/pg-structure/column.ts b/src/pg-structure/column.ts index a0d8524..68cc570 100644 --- a/src/pg-structure/column.ts +++ b/src/pg-structure/column.ts @@ -41,6 +41,8 @@ export default class Column extends DbObject { const { schema: typeSchema, typeName, length, precision, scale } = parseSQLType(this.db, args.sqlType); this.notNull = args.notNull; this.type = + this.db.allTypes.getMaybe(args.typeOid, { key: "oid" }) || + this.db.allTypes.getMaybe(args.typeOid, { key: "arrayOid" }) || typeSchema.typesIncludingEntities.getMaybe(typeName, { key: "internalName" }) || typeSchema.typesIncludingEntities.get(typeName); this.length = length; this.precision = precision; diff --git a/test/column.test.ts b/test/column.test.ts index 24c29d7..f2131c1 100644 --- a/test/column.test.ts +++ b/test/column.test.ts @@ -1,5 +1,6 @@ /* eslint-disable prefer-destructuring, @typescript-eslint/no-non-null-assertion */ -import { Db, Table, View, Column, CompositeType } from "../src/index"; +import { Client } from "pg"; +import pgStructure, { Db, Table, View, Column, CompositeType, Domain } from "../src/index"; import getDb from "./test-helper/get-db"; let db: Db; @@ -65,6 +66,22 @@ describe("Column", () => { expect(accountTable.get("c").type.name).toBe("circle"); }); + it("should resolve an unqualified domain visible through search_path.", async () => { + const client = new Client({ database: "pg-structure-test-main", user: "user", password: "password" }); + await client.connect(); + + try { + await client.query("SET search_path TO other_schema, public"); + const searchPathDb = await pgStructure(client); + const domainColumn = searchPathDb.schemas.get("other_schema").tables.get("domain_table").columns.get("name"); + + expect(domainColumn.type).toBeInstanceOf(Domain); + expect(domainColumn.type.schema.name).toBe("other_schema"); + } finally { + await client.end(); + } + }); + it("should have isSerial.", () => { expect(idColumn.isSerial).toBe(true); expect(accountTable.get("c").isSerial).toBe(false); diff --git a/test/test-helper/ddl/main.sql b/test/test-helper/ddl/main.sql index 3dab131..fff5bbd 100755 --- a/test/test-helper/ddl/main.sql +++ b/test/test-helper/ddl/main.sql @@ -69,6 +69,15 @@ CREATE DOMAIN "cross_schema_domain" AS "udt_composite"[] NOT NULL CREATE DOMAIN "price" AS Numeric(8,2) DEFAULT 1.2 NOT NULL CHECK (VALUE > 0) ; +CREATE DOMAIN "other_schema"."localised_string" AS jsonb +; + +CREATE TABLE "other_schema"."domain_table" +( + "name" "other_schema"."localised_string" NOT NULL +) +; + -- Create functions section ------------------------------------------------- CREATE FUNCTION "trigger_returning_function"()