-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
53 lines (44 loc) · 1.3 KB
/
Copy pathdatabase.py
File metadata and controls
53 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
import os
load_dotenv()
required_db_variables = (
"DB_USERNAME",
"DB_PASSWORD",
"DB_HOST",
"DB_PORT",
"DB_NAME",
)
missing_variables = [key for key in required_db_variables if not os.getenv(key)]
if missing_variables:
raise RuntimeError(
".env에 다음 DB 설정이 필요합니다: " + ", ".join(missing_variables)
)
DB_URL = URL.create(
drivername="mysql+pymysql",
username=os.environ["DB_USERNAME"],
password=os.environ["DB_PASSWORD"],
host=os.environ["DB_HOST"],
port=int(os.environ["DB_PORT"]),
database=os.environ["DB_NAME"],
)
class engineconn:
def __init__(self):
self.engine = create_engine(DB_URL, pool_recycle=500)
pass
def sessionmaker(self):
Session = sessionmaker(bind=self.engine)
session = Session()
return session
def connection(self):
conn = self.engine.connect()
return conn
engine = engineconn()
def get_db():
session = engine.sessionmaker() # DB 연결 생성
try:
yield session # 라우터 함수에 전달
finally:
session.close() # 요청 종료 후 연결 종료