Skip to content

Commit 812d3ab

Browse files
committed
make postgres settings values optional (but validate later)
1 parent e063785 commit 812d3ab

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

stac_fastapi/pgstac/config.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Postgres API configuration."""
22

3-
from typing import List, Type
3+
from typing import List, Optional, Type
44
from urllib.parse import quote_plus as quote
55

66
from pydantic import BaseModel, field_validator
@@ -57,12 +57,12 @@ class Settings(ApiSettings):
5757
invalid_id_chars: list of characters that are not allowed in item or collection ids.
5858
"""
5959

60-
postgres_user: str
61-
postgres_pass: str
62-
postgres_host_reader: str
63-
postgres_host_writer: str
64-
postgres_port: int
65-
postgres_dbname: str
60+
postgres_user: Optional[str] = None
61+
postgres_pass: Optional[str] = None
62+
postgres_host_reader: Optional[str] = None
63+
postgres_host_writer: Optional[str] = None
64+
postgres_port: Optional[int] = None
65+
postgres_dbname: Optional[str] = None
6666

6767
db_min_conn_size: int = 10
6868
db_max_conn_size: int = 10
@@ -93,18 +93,41 @@ def parse_cors_methods(cls, v):
9393
@property
9494
def reader_connection_string(self):
9595
"""Create reader psql connection string."""
96+
self._validate_postgres_settings()
9697
return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host_reader}:{self.postgres_port}/{self.postgres_dbname}"
9798

9899
@property
99100
def writer_connection_string(self):
100101
"""Create writer psql connection string."""
102+
self._validate_postgres_settings()
101103
return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host_writer}:{self.postgres_port}/{self.postgres_dbname}"
102104

103105
@property
104106
def testing_connection_string(self):
105107
"""Create testing psql connection string."""
108+
self._validate_postgres_settings()
106109
return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host_writer}:{self.postgres_port}/pgstactestdb"
107110

111+
def _validate_postgres_settings(self) -> None:
112+
"""Validate that required PostgreSQL settings are configured."""
113+
required_settings = [
114+
"postgres_host_writer",
115+
"postgres_host_reader",
116+
"postgres_user",
117+
"postgres_pass",
118+
"postgres_port",
119+
"postgres_dbname",
120+
]
121+
122+
missing = [
123+
setting for setting in required_settings if getattr(self, setting) is None
124+
]
125+
126+
if missing:
127+
raise ValueError(
128+
f"Missing required PostgreSQL settings: {', '.join(missing)}",
129+
)
130+
108131
model_config = SettingsConfigDict(
109132
**{**ApiSettings.model_config, **{"env_nested_delimiter": "__"}}
110133
)

0 commit comments

Comments
 (0)