From 3fd4c5cccae946dd623cedd1278a989fc833b97c Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Mon, 14 Oct 2024 22:02:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=20=D0=B8=20=D0=B2?= =?UTF-8?q?=D0=BD=D0=B5=D1=88=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/config.py | 19 +++++++++++++++++++ db/db_connection.py | 5 ++--- db/models/experiment_data_model.py | 2 +- db/models/experiment_parameters_model.py | 10 +++++++--- db/models/load_parameters_model.py | 2 +- db/models/recycling_parameters_model.py | 8 ++++++-- 6 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 db/config.py 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]