diff --git a/packages/pg-meta/src/sql/studio/database/table-definition.ts b/packages/pg-meta/src/sql/studio/database/table-definition.ts index 9ba3515bc57..4fbda5ea0c2 100644 --- a/packages/pg-meta/src/sql/studio/database/table-definition.ts +++ b/packages/pg-meta/src/sql/studio/database/table-definition.ts @@ -941,16 +941,16 @@ const SCOPED_PG_GET_TABLEDEF_SQL: SafeSqlFragment = safeSql` bInheritance := False; IF v_pgversion < 100000 THEN -- Issue#11: handle parent schema - SELECT c2.relname parent, c2.relnamespace::regnamespace INTO v_parent, v_parent_schema from pg_class c1, pg_namespace n, pg_inherits i, pg_class c2 - WHERE n.nspname = in_schema and n.oid = c1.relnamespace and c1.relname = in_table and c1.oid = i.inhrelid and i.inhparent = c2.oid and c1.relkind = 'r'; + SELECT c2.relname parent, n2.nspname INTO v_parent, v_parent_schema from pg_class c1, pg_namespace n, pg_inherits i, pg_class c2, pg_namespace n2 + WHERE n.nspname = in_schema and n.oid = c1.relnamespace and c1.relname = in_table and c1.oid = i.inhrelid and i.inhparent = c2.oid and c1.relkind = 'r' and n2.oid = c2.relnamespace; IF (v_parent IS NOT NULL) THEN bPartition := True; bInheritance := True; END IF; ELSE -- Issue#11: handle parent schema - SELECT c2.relname parent, c1.relispartition, pg_get_expr(c1.relpartbound, c1.oid, true), c2.relnamespace::regnamespace INTO v_parent, bRelispartition, v_partbound, v_parent_schema from pg_class c1, pg_namespace n, pg_inherits i, pg_class c2 - WHERE n.nspname = in_schema and n.oid = c1.relnamespace and c1.relname = in_table and c1.oid = i.inhrelid and i.inhparent = c2.oid and c1.relkind = 'r'; + SELECT c2.relname parent, c1.relispartition, pg_get_expr(c1.relpartbound, c1.oid, true), n2.nspname INTO v_parent, bRelispartition, v_partbound, v_parent_schema from pg_class c1, pg_namespace n, pg_inherits i, pg_class c2, pg_namespace n2 + WHERE n.nspname = in_schema and n.oid = c1.relnamespace and c1.relname = in_table and c1.oid = i.inhrelid and i.inhparent = c2.oid and c1.relkind = 'r' and n2.oid = c2.relnamespace; IF (v_parent IS NOT NULL) THEN bPartition := True; IF bRelispartition THEN diff --git a/packages/pg-meta/test/sql/studio/table-definition.test.ts b/packages/pg-meta/test/sql/studio/table-definition.test.ts index 37abe861857..7a82944d5ec 100644 --- a/packages/pg-meta/test/sql/studio/table-definition.test.ts +++ b/packages/pg-meta/test/sql/studio/table-definition.test.ts @@ -38,3 +38,30 @@ test('getTableDefinitionSql (scoped): escapes double quotes in schema, table, co await db.cleanup() } }) + +test('getTableDefinitionSql (scoped): quotes the parent schema for an inherited table', async () => { + const db = await createTestDatabase() + try { + await db.executeQuery(` + create schema "par""ent1"; + create table "par""ent1"."par""ent_tbl" (id bigint primary key); + create schema "chi""ld1"; + create table "chi""ld1"."chi""ld_tbl" () inherits ("par""ent1"."par""ent_tbl"); + `) + + const [{ id }] = await db.executeQuery<{ id: number }[]>( + `select '"chi""ld1"."chi""ld_tbl"'::regclass::oid::int8 as id;` + ) + + const sql = getTableDefinitionSql({ id, scoped: true }) + const [{ definition }] = await db.executeQuery<{ definition: string }[]>(sql) + + expect(definition).toContain('INHERITS ("par""ent1"."par""ent_tbl")') + + // the generated DDL must itself be valid, executable SQL + await db.executeQuery(`drop table "chi""ld1"."chi""ld_tbl";`) + await db.executeQuery(definition) + } finally { + await db.cleanup() + } +})