Skip to content

Commit d743d3f

Browse files
authored
Fix multiple SQL schema paths (geopython#2089)
* Test for multiple schemas in search path * Search multiple schemas * Fix flake8
1 parent 21dc507 commit d743d3f

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

pygeoapi/provider/sql.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,23 @@ def get_table_model(
669669
f'Could not connect to {repr(engine.url)} (password hidden).'
670670
)
671671
except InvalidRequestError:
672-
raise ProviderQueryError(
672+
msg = (
673673
f"Table '{table_name}' not found in schema '{schema}' "
674674
f'on {repr(engine.url)}.'
675675
)
676+
LOGGER.error(msg)
677+
678+
if len(db_search_path) > 1:
679+
# If the table is not found in the first schema, try the next one
680+
return get_table_model(
681+
table_name,
682+
id_field,
683+
db_search_path[1:],
684+
engine
685+
)
686+
else:
687+
# If the table is not found in any schema, raise an error
688+
raise ProviderQueryError(msg)
676689

677690
# Create SQLAlchemy model from reflected table
678691
# It is necessary to add the primary key constraint because SQLAlchemy

tests/test_postgresql_provider.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ def test_valid_connection_options(config):
151151
'keepalives_interval']
152152

153153

154+
def test_schema_path_search(config):
155+
config['data']['search_path'] = ['public', 'osm']
156+
PostgreSQLProvider(config)
157+
158+
config['data']['search_path'] = ['public', 'notosm']
159+
with pytest.raises(ProviderQueryError):
160+
PostgreSQLProvider(config)
161+
162+
154163
def test_query(config):
155164
"""Testing query for a valid JSON object with geometry"""
156165
p = PostgreSQLProvider(config)

0 commit comments

Comments
 (0)