fix: Update Pydantic v2 configuration for .env loading
- Fix ConfigDict model_config for Pydantic v2.12+ compatibility - Add env_file and env_file_encoding to all config classes - Each config class now properly loads from .env with correct prefix Fixes: ValidationError when loading settings from .env file CLI now works correctly with 'uv run python main.py'
This commit is contained in:
70
config.py
70
config.py
@@ -1,11 +1,19 @@
|
|||||||
"""Configuration management using Pydantic settings."""
|
"""Configuration management using Pydantic settings."""
|
||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
|
from pydantic import ConfigDict
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class DatabaseConfig(BaseSettings):
|
class MySQLConfig(BaseSettings):
|
||||||
"""Database configuration."""
|
"""MySQL source database configuration."""
|
||||||
|
model_config = ConfigDict(
|
||||||
|
env_prefix="MYSQL_",
|
||||||
|
case_sensitive=False,
|
||||||
|
extra="ignore",
|
||||||
|
env_file=".env",
|
||||||
|
env_file_encoding="utf-8"
|
||||||
|
)
|
||||||
|
|
||||||
host: str
|
host: str
|
||||||
port: int
|
port: int
|
||||||
@@ -13,60 +21,66 @@ class DatabaseConfig(BaseSettings):
|
|||||||
password: str
|
password: str
|
||||||
database: str
|
database: str
|
||||||
|
|
||||||
class Config:
|
|
||||||
env_prefix: str = ""
|
|
||||||
|
|
||||||
|
class PostgreSQLConfig(BaseSettings):
|
||||||
class MySQLConfig(DatabaseConfig):
|
|
||||||
"""MySQL source database configuration."""
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
env_prefix: str = "MYSQL_"
|
|
||||||
|
|
||||||
|
|
||||||
class PostgreSQLConfig(DatabaseConfig):
|
|
||||||
"""PostgreSQL target database configuration."""
|
"""PostgreSQL target database configuration."""
|
||||||
|
model_config = ConfigDict(
|
||||||
|
env_prefix="POSTGRES_",
|
||||||
|
case_sensitive=False,
|
||||||
|
extra="ignore",
|
||||||
|
env_file=".env",
|
||||||
|
env_file_encoding="utf-8"
|
||||||
|
)
|
||||||
|
|
||||||
class Config:
|
host: str
|
||||||
env_prefix: str = "POSTGRES_"
|
port: int
|
||||||
|
user: str
|
||||||
|
password: str
|
||||||
|
database: str
|
||||||
|
|
||||||
|
|
||||||
class MigrationSettings(BaseSettings):
|
class MigrationSettings(BaseSettings):
|
||||||
"""Migration settings."""
|
"""Migration settings."""
|
||||||
|
model_config = ConfigDict(
|
||||||
|
case_sensitive=False,
|
||||||
|
extra="ignore",
|
||||||
|
env_file=".env",
|
||||||
|
env_file_encoding="utf-8"
|
||||||
|
)
|
||||||
|
|
||||||
batch_size: int = 10000
|
batch_size: int = 10000
|
||||||
log_level: str = "INFO"
|
log_level: str = "INFO"
|
||||||
dry_run: bool = False
|
dry_run: bool = False
|
||||||
|
|
||||||
class Config:
|
|
||||||
env_file = ".env"
|
|
||||||
case_sensitive = False
|
|
||||||
|
|
||||||
|
|
||||||
class BenchmarkSettings(BaseSettings):
|
class BenchmarkSettings(BaseSettings):
|
||||||
"""Benchmark settings."""
|
"""Benchmark settings."""
|
||||||
|
model_config = ConfigDict(
|
||||||
|
env_prefix="BENCHMARK_",
|
||||||
|
case_sensitive=False,
|
||||||
|
extra="ignore",
|
||||||
|
env_file=".env",
|
||||||
|
env_file_encoding="utf-8"
|
||||||
|
)
|
||||||
|
|
||||||
output_dir: str = "benchmark_results"
|
output_dir: str = "benchmark_results"
|
||||||
iterations: int = 5
|
iterations: int = 5
|
||||||
|
|
||||||
class Config:
|
|
||||||
env_prefix: str = "BENCHMARK_"
|
|
||||||
env_file = ".env"
|
|
||||||
case_sensitive = False
|
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
"""All application settings."""
|
"""All application settings."""
|
||||||
|
model_config = ConfigDict(
|
||||||
|
env_file=".env",
|
||||||
|
env_file_encoding="utf-8",
|
||||||
|
case_sensitive=False,
|
||||||
|
extra="ignore"
|
||||||
|
)
|
||||||
|
|
||||||
mysql: MySQLConfig
|
mysql: MySQLConfig
|
||||||
postgres: PostgreSQLConfig
|
postgres: PostgreSQLConfig
|
||||||
migration: MigrationSettings
|
migration: MigrationSettings
|
||||||
benchmark: BenchmarkSettings
|
benchmark: BenchmarkSettings
|
||||||
|
|
||||||
class Config:
|
|
||||||
env_file = ".env"
|
|
||||||
case_sensitive = False
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_env(cls):
|
def from_env(cls):
|
||||||
"""Load settings from environment variables."""
|
"""Load settings from environment variables."""
|
||||||
|
|||||||
Reference in New Issue
Block a user