feature: Füge dotEnv configuration hinzu
This commit is contained in:
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
.venv
|
.venv
|
||||||
/.idea/
|
/.idea/
|
||||||
/database.db
|
/api/database.db
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
SQLITE_FILE_NAME=database2.db
|
||||||
|
DATABASE_URL=sqlite:///database2.db
|
||||||
+25
-8
@@ -1,28 +1,35 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlmodel import Session, SQLModel, create_engine, select
|
from sqlmodel import Session, SQLModel, create_engine, select
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from api.models import Car, FuelItem
|
from api.models import Car, FuelItem
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
sqlite_file_name = "database.db"
|
|
||||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
class Settings(BaseSettings):
|
||||||
|
database_url: str = ""
|
||||||
|
sqlite_file_name: str = ""
|
||||||
|
|
||||||
|
model_config = SettingsConfigDict(env_file=".env")
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
|
|
||||||
|
database_url = settings.database_url
|
||||||
|
|
||||||
connect_args = {
|
connect_args = {
|
||||||
"check_same_thread": False
|
"check_same_thread": False
|
||||||
}
|
}
|
||||||
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
|
engine = create_engine(database_url, echo=True, connect_args=connect_args)
|
||||||
|
|
||||||
|
|
||||||
def create_db_and_tables():
|
def create_db_and_tables():
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
def fill_database():
|
||||||
async def lifespan(app: FastAPI):
|
|
||||||
# startup
|
|
||||||
create_db_and_tables()
|
|
||||||
car = Car(id_string="Auto 1")
|
car = Car(id_string="Auto 1")
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(car)
|
session.add(car)
|
||||||
@@ -37,6 +44,16 @@ async def lifespan(app: FastAPI):
|
|||||||
session.add(fill_item)
|
session.add(fill_item)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(fill_item)
|
session.refresh(fill_item)
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
# startup
|
||||||
|
create_db_and_tables()
|
||||||
|
try:
|
||||||
|
fill_database()
|
||||||
|
except IntegrityError as e:
|
||||||
|
print(e)
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -5,12 +5,12 @@ from sqlmodel import Field, SQLModel
|
|||||||
|
|
||||||
class Car(SQLModel, table=True):
|
class Car(SQLModel, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
id_string: str = Field(nullable=False, index=True)
|
id_string: str = Field(nullable=False, index=True, unique=True)
|
||||||
|
|
||||||
|
|
||||||
class FuelItem(SQLModel, table=True):
|
class FuelItem(SQLModel, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
car_id: int | None = Field(default=None, foreign_key="car.id")
|
car_id: int | None = Field(default=None, foreign_key="car.id", unique_items=True)
|
||||||
date: datetime.date = Field(nullable=False, index=True)
|
date: datetime.date = Field(nullable=False, index=True, unique_items=True)
|
||||||
odometer_reading: float = Field(nullable=False)
|
odometer_reading: float = Field(nullable=False)
|
||||||
fuel_fill: float = Field(nullable=False)
|
fuel_fill: float = Field(nullable=False)
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ uvicorn==0.38.0
|
|||||||
idna==3.11
|
idna==3.11
|
||||||
pydantic==2.12.*
|
pydantic==2.12.*
|
||||||
pydantic_core==2.41.*
|
pydantic_core==2.41.*
|
||||||
|
pydantic_settings==2.11.*
|
||||||
sniffio==1.3.*
|
sniffio==1.3.*
|
||||||
starlette==0.48.*
|
starlette==0.49.*
|
||||||
typing-inspection==0.4.*
|
typing-inspection==0.4.*
|
||||||
typing_extensions==4.15.*
|
typing_extensions==4.15.*
|
||||||
Reference in New Issue
Block a user