diff --git a/db/config.py b/db/config.py new file mode 100644 index 0000000..001c696 --- /dev/null +++ b/db/config.py @@ -0,0 +1,19 @@ +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + DB_USER: str + DB_PASSWORD: str + DB_HOST: str + DB_PORT: int + DB_NAME: str + + @property + def db_url_asyncpg(self): + # 'postgresql+asyncpg://username:password@localhost:5432/database_name' + return f'postgresql+asyncpg://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}' + + model_config = SettingsConfigDict(env_file='../.env') + + +settings = Settings() \ No newline at end of file diff --git a/db/db_connection.py b/db/db_connection.py index 528db69..6cd6d8f 100644 --- a/db/db_connection.py +++ b/db/db_connection.py @@ -1,7 +1,6 @@ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker +from config import settings -# Асинхронное подключение к PostgreSQL -DATABASE_URL = 'postgresql+asyncpg://username:password@localhost:5432/database_name' -engine = create_async_engine(DATABASE_URL, echo=True) +engine = create_async_engine(url=settings.db_url_asyncpg, echo=True) async_session = async_sessionmaker(engine) diff --git a/db/models/experiment_data_model.py b/db/models/experiment_data_model.py index 0ac0fcc..1ee7167 100644 --- a/db/models/experiment_data_model.py +++ b/db/models/experiment_data_model.py @@ -2,7 +2,7 @@ from base import Base from sqlalchemy.orm import Mapped, mapped_column class ExperimentData(Base): - __tablename__ = 'ExperimentData' + __tablename__ = 'experiment_data' id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) direction: Mapped[float] diff --git a/db/models/experiment_parameters_model.py b/db/models/experiment_parameters_model.py index 4b39fe1..ad41a9f 100644 --- a/db/models/experiment_parameters_model.py +++ b/db/models/experiment_parameters_model.py @@ -1,16 +1,20 @@ +from typing import Optional + +from sqlalchemy import ForeignKey + from base import Base from sqlalchemy.orm import Mapped, mapped_column class ExperimentParameters(Base): - __tablename__ = 'ExperimentParameters' + __tablename__ = 'experiment_parameters' id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) outer_blades_count: Mapped[int] outer_blades_length: Mapped[float] outer_blades_angle: Mapped[float] middle_blades_count: Mapped[int] - load_id: Mapped[int] - recycling_id: Mapped[int] + load_id: Mapped[Optional[int]] = mapped_column(ForeignKey('load_parameters.id', ondelete='SET NULL')) + recycling_id: Mapped[Optional[int]] = mapped_column(ForeignKey('recycling_parameters.id', ondelete='SET NULL')) experiment_hash: Mapped[str] def __repr__(self): diff --git a/db/models/load_parameters_model.py b/db/models/load_parameters_model.py index 4027656..5895199 100644 --- a/db/models/load_parameters_model.py +++ b/db/models/load_parameters_model.py @@ -2,7 +2,7 @@ from base import Base from sqlalchemy.orm import Mapped, mapped_column class LoadParameters(Base): - __tablename__ = 'LoadParameters' + __tablename__ = 'load_parameters' id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) load: Mapped[int] diff --git a/db/models/recycling_parameters_model.py b/db/models/recycling_parameters_model.py index a21aae6..cea125a 100644 --- a/db/models/recycling_parameters_model.py +++ b/db/models/recycling_parameters_model.py @@ -1,11 +1,15 @@ +from typing import Optional + +from sqlalchemy import ForeignKey + from base import Base from sqlalchemy.orm import Mapped, mapped_column class RecyclingParameters(Base): - __tablename__ = 'RecyclingParameters' + __tablename__ = 'recycling_parameters' id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) - load_id: Mapped[int] + load_id: Mapped[Optional[int]] = mapped_column(ForeignKey('load_parameters.id', ondelete='SET NULL')) recycling_level: Mapped[int] co2: Mapped[float] n2: Mapped[float]