|
1 | 1 | """Postgres API configuration.""" |
2 | 2 |
|
3 | | -from typing import List, Type |
| 3 | +from typing import List, Optional, Type |
4 | 4 | from urllib.parse import quote_plus as quote |
5 | 5 |
|
6 | 6 | from pydantic import BaseModel, field_validator |
@@ -57,12 +57,12 @@ class Settings(ApiSettings): |
57 | 57 | invalid_id_chars: list of characters that are not allowed in item or collection ids. |
58 | 58 | """ |
59 | 59 |
|
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 |
66 | 66 |
|
67 | 67 | db_min_conn_size: int = 10 |
68 | 68 | db_max_conn_size: int = 10 |
@@ -93,18 +93,41 @@ def parse_cors_methods(cls, v): |
93 | 93 | @property |
94 | 94 | def reader_connection_string(self): |
95 | 95 | """Create reader psql connection string.""" |
| 96 | + self._validate_postgres_settings() |
96 | 97 | return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host_reader}:{self.postgres_port}/{self.postgres_dbname}" |
97 | 98 |
|
98 | 99 | @property |
99 | 100 | def writer_connection_string(self): |
100 | 101 | """Create writer psql connection string.""" |
| 102 | + self._validate_postgres_settings() |
101 | 103 | return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host_writer}:{self.postgres_port}/{self.postgres_dbname}" |
102 | 104 |
|
103 | 105 | @property |
104 | 106 | def testing_connection_string(self): |
105 | 107 | """Create testing psql connection string.""" |
| 108 | + self._validate_postgres_settings() |
106 | 109 | return f"postgresql://{self.postgres_user}:{quote(self.postgres_pass)}@{self.postgres_host_writer}:{self.postgres_port}/pgstactestdb" |
107 | 110 |
|
| 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 | + |
108 | 131 | model_config = SettingsConfigDict( |
109 | 132 | **{**ApiSettings.model_config, **{"env_nested_delimiter": "__"}} |
110 | 133 | ) |
0 commit comments