Delete database.py
Browse files- database.py +0 -48
database.py
DELETED
@@ -1,48 +0,0 @@
|
|
1 |
-
# database.py
|
2 |
-
|
3 |
-
from sqlalchemy import create_engine
|
4 |
-
from sqlalchemy.ext.declarative import declarative_base
|
5 |
-
from sqlalchemy.orm import sessionmaker
|
6 |
-
import os
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
|
9 |
-
# Load environment variables from .env file
|
10 |
-
load_dotenv()
|
11 |
-
|
12 |
-
# Retrieve database connection details from environment variables
|
13 |
-
MYSQL_HOST = os.getenv("MYSQL_HOST", "db") # Default to 'db' if not set
|
14 |
-
MYSQL_PORT = os.getenv("MYSQL_PORT", "3306")
|
15 |
-
MYSQL_DATABASE = os.getenv("MYSQL_DATABASE", "bbapi")
|
16 |
-
MYSQL_USER = os.getenv("MYSQL_USER", "bbapi")
|
17 |
-
MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "QQQR5dKS8NYGnYcC")
|
18 |
-
|
19 |
-
# Construct the database URL
|
20 |
-
DATABASE_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}"
|
21 |
-
|
22 |
-
# Create SQLAlchemy engine
|
23 |
-
engine = create_engine(
|
24 |
-
DATABASE_URL,
|
25 |
-
pool_pre_ping=True,
|
26 |
-
pool_recycle=3600
|
27 |
-
)
|
28 |
-
|
29 |
-
# Create a configured "Session" class
|
30 |
-
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
31 |
-
|
32 |
-
# Base class for your ORM models
|
33 |
-
Base = declarative_base()
|
34 |
-
|
35 |
-
# Dependency to get a database session
|
36 |
-
from fastapi import Depends
|
37 |
-
from sqlalchemy.orm import Session
|
38 |
-
|
39 |
-
def get_db():
|
40 |
-
"""
|
41 |
-
Dependency that provides a SQLAlchemy session.
|
42 |
-
Ensures the session is closed after the request is completed.
|
43 |
-
"""
|
44 |
-
db = SessionLocal()
|
45 |
-
try:
|
46 |
-
yield db
|
47 |
-
finally:
|
48 |
-
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|