Skip to content

Commit 060bd00

Browse files
committed
update from review
1 parent d85d10a commit 060bd00

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

stac_fastapi/pgstac/config.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ class PostgresSettings(BaseSettings):
7878
postgres_host_reader: Annotated[
7979
Optional[str],
8080
Field(
81-
deprecated="`postgres_host_reader` is deprecated, please use `pghost`", default=None
81+
deprecated="`postgres_host_reader` is deprecated, please use `pghost`",
82+
default=None,
8283
),
8384
]
8485
postgres_host_writer: Annotated[
8586
Optional[str],
8687
Field(
87-
deprecated="`postgres_host_writer` is deprecated, please use `pghost`", default=None
88+
deprecated="`postgres_host_writer` is deprecated, please use `pghost`",
89+
default=None,
8890
),
8991
]
9092
postgres_port: Annotated[
@@ -123,7 +125,8 @@ def _pg_settings_compat(cls, data: Any) -> Any:
123125
compat = {
124126
"postgres_user": "pguser",
125127
"postgres_pass": "pgpassword",
126-
"postgres_host": "pghost",
128+
"postgres_host_reader": "pghost",
129+
"postgres_host_writer": "pghost",
127130
"postgres_port": "pgport",
128131
"postgres_dbname": "pgdatabase",
129132
}
@@ -136,6 +139,15 @@ def _pg_settings_compat(cls, data: Any) -> Any:
136139
)
137140
data[new_key] = val
138141

142+
if (pgh_reader := data.get("postgres_host_reader")) and (
143+
pgh_writer := data.get("postgres_host_writer")
144+
):
145+
if pgh_reader != pgh_writer:
146+
raise ValueError(
147+
"In order to use different host values for reading and writing "
148+
"you must explicitly provide write_postgres_settings to the connect_to_db function"
149+
)
150+
139151
return data
140152

141153
@property

tests/test_config.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44

55
import pytest
6+
from pydantic import ValidationError
67

78
from stac_fastapi.pgstac.config import PostgresSettings
89

@@ -21,12 +22,13 @@ async def test_pg_settings_with_env_postgres(monkeypatch):
2122
"""Test PostgresSettings with POSTGRES_* environment variables"""
2223
monkeypatch.setenv("POSTGRES_USER", "username")
2324
monkeypatch.setenv("POSTGRES_PASS", "password")
24-
monkeypatch.setenv("POSTGRES_HOST", "0.0.0.0")
25+
monkeypatch.setenv("POSTGRES_HOST_READER", "0.0.0.0")
26+
monkeypatch.setenv("POSTGRES_HOST_WRITER", "0.0.0.0")
2527
monkeypatch.setenv("POSTGRES_PORT", "1111")
2628
monkeypatch.setenv("POSTGRES_DBNAME", "pgstac")
2729
with pytest.warns(DeprecationWarning) as record:
2830
assert PostgresSettings(_env_file=None)
29-
assert len(record) == 5
31+
assert len(record) == 6
3032

3133

3234
async def test_pg_settings_attributes(monkeypatch):
@@ -49,7 +51,7 @@ async def test_pg_settings_attributes(monkeypatch):
4951
settings = PostgresSettings(
5052
postgres_user="user",
5153
postgres_pass="password",
52-
postgres_host="0.0.0.0",
54+
postgres_host_reader="0.0.0.0",
5355
postgres_port=1111,
5456
postgres_dbname="pgstac",
5557
_env_file=None,
@@ -59,4 +61,16 @@ async def test_pg_settings_attributes(monkeypatch):
5961

6062
# Should raise warning when accessing deprecated attributes
6163
with pytest.warns(DeprecationWarning):
62-
assert settings.postgres_host == "0.0.0.0"
64+
assert settings.postgres_host_reader == "0.0.0.0"
65+
66+
with pytest.raises(ValidationError):
67+
with pytest.warns(DeprecationWarning) as record:
68+
PostgresSettings(
69+
postgres_user="user",
70+
postgres_pass="password",
71+
postgres_host_reader="0.0.0.0",
72+
postgres_host_writer="1.1.1.1",
73+
postgres_port=1111,
74+
postgres_dbname="pgstac",
75+
_env_file=None,
76+
)

0 commit comments

Comments
 (0)