Skip to content

Commit ff64b6b

Browse files
fix: BI-6847 fix postgres schema filters by adding escaping (#1468)
* fix: BI-6847 fix postgres schema filters by adding escaping * fix test
1 parent 512354d commit ff64b6b

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

lib/dl_connector_greenplum/dl_connector_greenplum/core/adapters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def get_list_all_tables_query(
2727
JOIN pg_namespace
2828
ON pg_namespace.oid = pg_class.relnamespace
2929
WHERE
30-
pg_namespace.nspname not like 'pg_%'
31-
AND pg_namespace.nspname not like 'gp_%'
30+
pg_namespace.nspname not like 'pg\_%'
31+
AND pg_namespace.nspname not like 'gp\_%'
3232
AND pg_namespace.nspname != 'session_state'
3333
AND pg_namespace.nspname != 'information_schema'
3434
AND pg_class.relkind in ('m', 'p', 'r', 'v')
@@ -58,8 +58,8 @@ def get_list_schema_names_query(
5858
sql_parts = [
5959
"""
6060
SELECT nspname FROM pg_namespace
61-
WHERE nspname NOT LIKE 'pg_%'
62-
AND nspname NOT LIKE 'gp_%'
61+
WHERE nspname NOT LIKE 'pg\_%'
62+
AND nspname NOT LIKE 'gp\_%'
6363
AND nspname != 'session_state'
6464
AND nspname != 'information_schema'
6565
"""

lib/dl_connector_postgresql/dl_connector_postgresql/core/postgresql_base/adapters_base_postgres.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def get_list_all_tables_query(
387387
JOIN pg_namespace
388388
ON pg_namespace.oid = pg_class.relnamespace
389389
WHERE
390-
pg_namespace.nspname not like 'pg_%'
390+
pg_namespace.nspname not like 'pg\_%'
391391
AND pg_namespace.nspname != 'information_schema'
392392
AND pg_class.relkind in ('m', 'p', 'r', 'v')
393393
AND NOT COALESCE((row_to_json(pg_class)->>'relispartition')::boolean, false)
@@ -412,7 +412,7 @@ def get_list_schema_names_query(
412412
sql_parts = [
413413
"""
414414
SELECT nspname FROM pg_namespace
415-
WHERE nspname NOT LIKE 'pg_%'
415+
WHERE nspname NOT LIKE 'pg\_%'
416416
"""
417417
]
418418

lib/dl_connector_postgresql/dl_connector_postgresql_tests/db/core/test_adapter.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async def test_tables_list(self, conn_bi_context: RequestContextInfo, target_con
3434
if t.schema_name != "public" # skip public schema because it contains garbage tables from other tests
3535
# TODO: clean up tables created in other tests
3636
] == [
37+
"pgmyschema.test_table",
3738
"test_data.sample",
3839
"test_data_partitions.sample_partition",
3940
]
@@ -54,3 +55,26 @@ async def test_tables_list_schema(
5455
)
5556

5657
assert [f"{t.schema_name}.{t.table_name}" for t in tables] == [f"{schema}.{t}" for t in expected_tables]
58+
59+
@pytest.mark.asyncio
60+
async def test_list_system_schemas(
61+
self, conn_bi_context: RequestContextInfo, target_conn_dto: PostgresConnTargetDTO
62+
):
63+
"""Test that tables from schemas starting with 'pg' but not 'pg_' are included in the table list."""
64+
dba = self._make_dba(target_conn_dto, conn_bi_context)
65+
tables = await dba.get_tables(SchemaIdent(db_name="test_data", schema_name=None))
66+
table_schemas = {t.schema_name for t in tables}
67+
68+
assert "pgmyschema" in table_schemas, (
69+
f"Schema 'pgmyschema' should be included in table list. " f"Found schemas: {sorted(table_schemas)}"
70+
)
71+
72+
pgmyschema_tables = [f"{t.schema_name}.{t.table_name}" for t in tables if t.schema_name == "pgmyschema"]
73+
assert "pgmyschema.test_table" in pgmyschema_tables, (
74+
f"Table 'pgmyschema.test_table' should be in the results. " f"Found: {pgmyschema_tables}"
75+
)
76+
77+
pg_underscore_schemas = [s for s in table_schemas if s.startswith("pg_")]
78+
assert len(pg_underscore_schemas) == 0, (
79+
f"System schemas starting with 'pg_' should be filtered out. " f"Found: {pg_underscore_schemas}"
80+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Create schemas that test edge cases for schema filtering
2+
-- This schema name starts with 'pg' but doesn't have an underscore after it,
3+
-- so it should NOT be filtered out by the pattern 'pg\_%'
4+
CREATE SCHEMA IF NOT EXISTS pgmyschema;
5+
CREATE TABLE pgmyschema.test_table (
6+
id INTEGER PRIMARY KEY,
7+
name VARCHAR(255)
8+
);
9+
INSERT INTO pgmyschema.test_table (id, name) VALUES (1, 'test');

0 commit comments

Comments
 (0)