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."""
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import ConfigDict
|
||||
from typing import Optional
|
||||
import os
|
||||
|
||||
|
||||
class DatabaseConfig(BaseSettings):
|
||||
"""Database configuration."""
|
||||
class MySQLConfig(BaseSettings):
|
||||
"""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
|
||||
port: int
|
||||
@@ -13,60 +21,66 @@ class DatabaseConfig(BaseSettings):
|
||||
password: str
|
||||
database: str
|
||||
|
||||
class Config:
|
||||
env_prefix: str = ""
|
||||
|
||||
|
||||
class MySQLConfig(DatabaseConfig):
|
||||
"""MySQL source database configuration."""
|
||||
|
||||
class Config:
|
||||
env_prefix: str = "MYSQL_"
|
||||
|
||||
|
||||
class PostgreSQLConfig(DatabaseConfig):
|
||||
class PostgreSQLConfig(BaseSettings):
|
||||
"""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:
|
||||
env_prefix: str = "POSTGRES_"
|
||||
host: str
|
||||
port: int
|
||||
user: str
|
||||
password: str
|
||||
database: str
|
||||
|
||||
|
||||
class MigrationSettings(BaseSettings):
|
||||
"""Migration settings."""
|
||||
model_config = ConfigDict(
|
||||
case_sensitive=False,
|
||||
extra="ignore",
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8"
|
||||
)
|
||||
|
||||
batch_size: int = 10000
|
||||
log_level: str = "INFO"
|
||||
dry_run: bool = False
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
case_sensitive = False
|
||||
|
||||
|
||||
class BenchmarkSettings(BaseSettings):
|
||||
"""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"
|
||||
iterations: int = 5
|
||||
|
||||
class Config:
|
||||
env_prefix: str = "BENCHMARK_"
|
||||
env_file = ".env"
|
||||
case_sensitive = False
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""All application settings."""
|
||||
model_config = ConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
case_sensitive=False,
|
||||
extra="ignore"
|
||||
)
|
||||
|
||||
mysql: MySQLConfig
|
||||
postgres: PostgreSQLConfig
|
||||
migration: MigrationSettings
|
||||
benchmark: BenchmarkSettings
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
case_sensitive = False
|
||||
|
||||
@classmethod
|
||||
def from_env(cls):
|
||||
"""Load settings from environment variables."""
|
||||
|
||||
Reference in New Issue
Block a user