Repository navigation
Expand file tree
/
Copy pathmysql.txt
More file actions
78 lines (58 loc) · 1.41 KB
/
Copy pathmysql.txt
File metadata and controls
78 lines (58 loc) · 1.41 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#database.py
engine = create_engine(
"mysql+pymysql://{username}:{password}@{host}:{port}/{name}".format(
username=settings.DB_USERNAME,
password=settings.DB_PASSWORD,
host=settings.DB_HOST,
port=settings.DB_PORT,
name=settings.DB_TEST_NAME if settings.TESTING else settings.DB_NAME,
)
)
SessionLocal = sessionmaker(
bind=engine,
autocommit=False,
autoflush=False,
)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
#config.py
from pydantic_settings import BaseSettings
from typing import ClassVar
from functools import lru_cache
class BaseConfig(BaseSettings):
# DB
DB_USERNAME: str
DB_HOST: str
DB_PASSWORD: str
DB_PORT: int
# Test Config
TESTING: ClassVar[bool] = False
class Config:
extra = "ignore"
env_file = ".env"
env_file_encoding = "utf-8"
class testSettings(BaseConfig):
TESTING: ClassVar[bool] = True
DB_TEST_NAME: str
class Settings(BaseConfig):
TESTING: ClassVar[bool] = False
DB_NAME: str
class isMain(BaseSettings):
IS_MAIN: bool
class Config:
extra = "ignore"
env_file = ".env"
env_file_encoding = "utf-8"
@lru_cache
def get_settings():
if is_main.IS_MAIN:
return Settings()
else:
return testSettings()
is_main = isMain()
settings = get_settings()