Skip to content

Commit 3535fea

Browse files
committed
rename settings from POSTGRES_* to PG*
1 parent 1514154 commit 3535fea

File tree

11 files changed

+206
-69
lines changed

11 files changed

+206
-69
lines changed

.github/workflows/cicd.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,10 @@ jobs:
9090
- name: Load data and validate
9191
run: python -m stac_fastapi.pgstac.app & ./scripts/wait-for-it.sh localhost:8080 && python ./scripts/ingest_joplin.py http://localhost:8080 && ./scripts/validate http://localhost:8080
9292
env:
93-
POSTGRES_USER: username
94-
POSTGRES_PASS: password
95-
POSTGRES_DBNAME: postgis
96-
POSTGRES_HOST: localhost
97-
POSTGRES_PORT: 5432
9893
PGUSER: username
9994
PGPASSWORD: password
95+
PGHOST: localhost
96+
PGPORT: 5432
10097
PGDATABASE: postgis
10198
APP_HOST: 0.0.0.0
10299
APP_PORT: 8080

CHANGES.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,34 @@
44

55
### Changed
66

7-
- rename `POSTGRES_HOST_READER` to `POSTGRES_HOST` in config **breaking change**
7+
- rename `POSTGRES_HOST_READER` to `PGHOST` in config **breaking change**
8+
- rename `POSTGRES_USER` to `PGUSER` in config **breaking change**
9+
- rename `POSTGRES_PASS` to `PGPASSWORD` in config **breaking change**
10+
- rename `POSTGRES_PORT` to `PGPORT` in config **breaking change**
11+
- rename `POSTGRES_DBNAME` to `PGDBNAME` in config **breaking change**
12+
```python
13+
from stac_fastapi.pgstac.config import PostgresSettings
14+
15+
# before
16+
settings = PostgresSettings(
17+
postgres_user="user",
18+
postgres_pass="password",
19+
postgres_host_reader="0.0.0.0",
20+
postgres_host_writer="0.0.0.0",
21+
postgres_port=1111,
22+
postgres_dbname="pgstac",
23+
)
24+
25+
# now
26+
settings = PostgresSettings(
27+
pguser="user",
28+
pgpassword="password",
29+
pghost="0.0.0.0",
30+
pgport=1111,
31+
pgdatabase="pgstac",
32+
)
33+
```
34+
835
- rename `reader_connection_string` to `connection_string` in `PostgresSettings` class **breaking change**
936
- add `ENABLE_TRANSACTIONS_EXTENSIONS` env variable to enable `transaction` extensions
1037
- disable transaction and bulk_transactions extensions by default **breaking change**

docker-compose.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ services:
88
- APP_PORT=8082
99
- RELOAD=true
1010
- ENVIRONMENT=local
11-
- POSTGRES_USER=username
12-
- POSTGRES_PASS=password
13-
- POSTGRES_DBNAME=postgis
14-
- POSTGRES_HOST=database
15-
- POSTGRES_PORT=5432
11+
- PGUSER=username
12+
- PGPASS=password
13+
- PGDATABASE=postgis
14+
- PGHOST=database
15+
- PGPORT=5432
1616
- WEB_CONCURRENCY=10
1717
- VSI_CACHE=TRUE
1818
- GDAL_HTTP_MERGE_CONSECUTIVE_RANGES=YES

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"brotli_asgi",
1818
"pygeofilter>=0.2",
1919
"pypgstac>=0.8,<0.10",
20+
"typing_extensions>=4.9.0",
2021
]
2122

2223
extra_reqs = {

stac_fastapi/pgstac/config.py

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Postgres API configuration."""
22

3-
from typing import List, Type
3+
import warnings
4+
from typing import Annotated, Any, List, Optional, Type
45
from urllib.parse import quote_plus as quote
56

6-
from pydantic import BaseModel, field_validator
7+
from pydantic import BaseModel, Field, field_validator, model_validator
78
from pydantic_settings import BaseSettings, SettingsConfigDict
89
from stac_fastapi.types.config import ApiSettings
910

@@ -61,11 +62,44 @@ class PostgresSettings(BaseSettings):
6162
invalid_id_chars: list of characters that are not allowed in item or collection ids.
6263
"""
6364

64-
postgres_user: str
65-
postgres_pass: str
66-
postgres_host: str
67-
postgres_port: int
68-
postgres_dbname: str
65+
postgres_user: Annotated[
66+
Optional[str],
67+
Field(
68+
deprecated="`postgres_user` is deprecated, please use `pguser`", default=None
69+
),
70+
]
71+
postgres_pass: Annotated[
72+
Optional[str],
73+
Field(
74+
deprecated="`postgres_pass` is deprecated, please use `pgpassword`",
75+
default=None,
76+
),
77+
]
78+
postgres_host: Annotated[
79+
Optional[str],
80+
Field(
81+
deprecated="`postgres_host` is deprecated, please use `pghost`", default=None
82+
),
83+
]
84+
postgres_port: Annotated[
85+
Optional[int],
86+
Field(
87+
deprecated="`postgres_port` is deprecated, please use `pgport`", default=None
88+
),
89+
]
90+
postgres_dbname: Annotated[
91+
Optional[str],
92+
Field(
93+
deprecated="`postgres_dbname` is deprecated, please use `pgdatabase`",
94+
default=None,
95+
),
96+
]
97+
98+
pguser: str
99+
pgpassword: str
100+
pghost: str
101+
pgport: int
102+
pgdatabase: str
69103

70104
db_min_conn_size: int = 1
71105
db_max_conn_size: int = 10
@@ -76,10 +110,32 @@ class PostgresSettings(BaseSettings):
76110

77111
model_config = {"env_file": ".env", "extra": "ignore"}
78112

113+
@model_validator(mode="before")
114+
@classmethod
115+
def _pg_settings_compat(cls, data: Any) -> Any:
116+
if isinstance(data, dict):
117+
compat = {
118+
"postgres_user": "pguser",
119+
"postgres_pass": "pgpassword",
120+
"postgres_host": "pghost",
121+
"postgres_port": "pgport",
122+
"postgres_dbname": "pgdatabase",
123+
}
124+
for old_key, new_key in compat.items():
125+
if val := data.get(old_key, None):
126+
warnings.warn(
127+
f"`{old_key}` is deprecated, please use `{new_key}`",
128+
DeprecationWarning,
129+
stacklevel=1,
130+
)
131+
data[new_key] = val
132+
133+
return data
134+
79135
@property
80136
def connection_string(self):
81137
"""Create reader psql connection string."""
82-
return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host}:{self.postgres_port}/{self.postgres_dbname}"
138+
return f"postgresql://{self.pguser}:{quote(self.pgpassword)}@{self.pghost}:{self.pgport}/{self.pgdatabase}"
83139

84140

85141
class Settings(ApiSettings):

stac_fastapi/pgstac/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def connect_to_db(
7474

7575
if add_write_connection_pool:
7676
if not write_postgres_settings:
77-
write_postgres_settings = PostgresSettings()
77+
write_postgres_settings = postgres_settings
7878

7979
app.state.writepool = await _create_pool(write_postgres_settings)
8080

tests/api/test_api.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,11 @@ async def get_collection(
740740
)
741741

742742
postgres_settings = PostgresSettings(
743-
postgres_user=database.user,
744-
postgres_pass=database.password,
745-
postgres_host=database.host,
746-
postgres_port=database.port,
747-
postgres_dbname=database.dbname,
743+
pguser=database.user,
744+
pgpassword=database.password,
745+
pghost=database.host,
746+
pgport=database.port,
747+
pgdatabase=database.dbname,
748748
)
749749

750750
extensions = [
@@ -773,7 +773,6 @@ async def get_collection(
773773
app,
774774
postgres_settings=postgres_settings,
775775
add_write_connection_pool=True,
776-
write_postgres_settings=postgres_settings,
777776
)
778777
try:
779778
async with AsyncClient(transport=ASGITransport(app=app)) as client:
@@ -814,11 +813,11 @@ async def test_no_extension(
814813
enable_response_models=validation,
815814
)
816815
postgres_settings = PostgresSettings(
817-
postgres_user=database.user,
818-
postgres_pass=database.password,
819-
postgres_host=database.host,
820-
postgres_port=database.port,
821-
postgres_dbname=database.dbname,
816+
pguser=database.user,
817+
pgpassword=database.password,
818+
pghost=database.host,
819+
pgport=database.port,
820+
pgdatabase=database.dbname,
822821
)
823822
extensions = []
824823
post_request_model = create_post_request_model(extensions, base_model=PgstacSearch)
@@ -833,7 +832,6 @@ async def test_no_extension(
833832
app,
834833
postgres_settings=postgres_settings,
835834
add_write_connection_pool=True,
836-
write_postgres_settings=postgres_settings,
837835
)
838836
try:
839837
async with AsyncClient(transport=ASGITransport(app=app)) as client:

tests/clients/test_postgres.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ async def test_create_bulk_items_id_mismatch(
527527

528528
async def test_db_setup_works_with_env_vars(api_client, database, monkeypatch):
529529
"""Test that the application starts successfully if the POSTGRES_* environment variables are set"""
530-
monkeypatch.setenv("POSTGRES_USER", database.user)
531-
monkeypatch.setenv("POSTGRES_PASS", database.password)
532-
monkeypatch.setenv("POSTGRES_HOST", database.host)
533-
monkeypatch.setenv("POSTGRES_PORT", str(database.port))
534-
monkeypatch.setenv("POSTGRES_DBNAME", database.dbname)
530+
monkeypatch.setenv("PGUSER", database.user)
531+
monkeypatch.setenv("PGPASSWORD", database.password)
532+
monkeypatch.setenv("PGHOST", database.host)
533+
monkeypatch.setenv("PGPORT", str(database.port))
534+
monkeypatch.setenv("PGDATABASE", database.dbname)
535535

536536
await connect_to_db(api_client.app)
537537
await close_db_connection(api_client.app)
@@ -564,11 +564,11 @@ async def app(self, api_client, database):
564564
app fixture override to setup app with a customized db connection getter
565565
"""
566566
postgres_settings = PostgresSettings(
567-
postgres_user=database.user,
568-
postgres_pass=database.password,
569-
postgres_host=database.host,
570-
postgres_port=database.port,
571-
postgres_dbname=database.dbname,
567+
pguser=database.user,
568+
pgpassword=database.password,
569+
pghost=database.host,
570+
pgport=database.port,
571+
pgdatabase=database.dbname,
572572
)
573573

574574
logger.debug("Customizing app setup")

tests/conftest.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ def api_client(request):
200200
@pytest.fixture(scope="function")
201201
async def app(api_client, database):
202202
postgres_settings = PostgresSettings(
203-
postgres_user=database.user,
204-
postgres_pass=database.password,
205-
postgres_host=database.host,
206-
postgres_port=database.port,
207-
postgres_dbname=database.dbname,
203+
pguser=database.user,
204+
pgpassword=database.password,
205+
pghost=database.host,
206+
pgport=database.port,
207+
pgdatabase=database.dbname,
208208
)
209209
logger.info("Creating app Fixture")
210210
time.time()
@@ -213,7 +213,6 @@ async def app(api_client, database):
213213
app,
214214
postgres_settings=postgres_settings,
215215
add_write_connection_pool=True,
216-
write_postgres_settings=postgres_settings,
217216
)
218217

219218
yield app
@@ -308,19 +307,18 @@ async def app_no_ext(database):
308307
)
309308

310309
postgres_settings = PostgresSettings(
311-
postgres_user=database.user,
312-
postgres_pass=database.password,
313-
postgres_host=database.host,
314-
postgres_port=database.port,
315-
postgres_dbname=database.dbname,
310+
pguser=database.user,
311+
pgpassword=database.password,
312+
pghost=database.host,
313+
pgport=database.port,
314+
pgdatabase=database.dbname,
316315
)
317316
logger.info("Creating app Fixture")
318317
time.time()
319318
await connect_to_db(
320319
api_client_no_ext.app,
321320
postgres_settings=postgres_settings,
322321
add_write_connection_pool=True,
323-
write_postgres_settings=postgres_settings,
324322
)
325323
yield api_client_no_ext.app
326324
await close_db_connection(api_client_no_ext.app)
@@ -349,11 +347,11 @@ async def app_no_transaction(database):
349347
)
350348

351349
postgres_settings = PostgresSettings(
352-
postgres_user=database.user,
353-
postgres_pass=database.password,
354-
postgres_host=database.host,
355-
postgres_port=database.port,
356-
postgres_dbname=database.dbname,
350+
pguser=database.user,
351+
pgpassword=database.password,
352+
pghost=database.host,
353+
pgport=database.port,
354+
pgdatabase=database.dbname,
357355
)
358356
logger.info("Creating app Fixture")
359357
time.time()
@@ -380,11 +378,11 @@ async def app_client_no_transaction(app_no_transaction):
380378
@pytest.fixture(scope="function")
381379
async def default_app(database, monkeypatch):
382380
"""Test default stac-fastapi-pgstac application."""
383-
monkeypatch.setenv("POSTGRES_USER", database.user)
384-
monkeypatch.setenv("POSTGRES_PASS", database.password)
385-
monkeypatch.setenv("POSTGRES_HOST", database.host)
386-
monkeypatch.setenv("POSTGRES_PORT", str(database.port))
387-
monkeypatch.setenv("POSTGRES_DBNAME", database.dbname)
381+
monkeypatch.setenv("PGUSER", database.user)
382+
monkeypatch.setenv("PGPASSWORD", database.password)
383+
monkeypatch.setenv("PGHOST", database.host)
384+
monkeypatch.setenv("PGPORT", str(database.port))
385+
monkeypatch.setenv("PGDATABASE", database.dbname)
388386
monkeypatch.delenv("ENABLED_EXTENSIONS", raising=False)
389387

390388
monkeypatch.setenv("ENABLE_TRANSACTIONS_EXTENSIONS", "TRUE")

tests/resources/test_mgmt.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ async def test_health_503(database):
5858

5959
# No lifespan so no `get_connection` is application state
6060
postgres_settings = PostgresSettings(
61-
postgres_user=database.user,
62-
postgres_pass=database.password,
63-
postgres_host=database.host,
64-
postgres_port=database.port,
65-
postgres_dbname=database.dbname,
61+
pguser=database.user,
62+
pgpassword=database.password,
63+
pghost=database.host,
64+
pgport=database.port,
65+
pgdatabase=database.dbname,
6666
)
6767
# Create connection pool but close it just after
6868
await connect_to_db(api.app, postgres_settings=postgres_settings)

0 commit comments

Comments
 (0)