-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
77 lines (57 loc) · 1.88 KB
/
setup.py
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
import os
import databases
import sqlalchemy
from dotenv import find_dotenv, load_dotenv
from ormar import ModelMeta
load_dotenv(find_dotenv())
def get_database_uri() -> str:
ENV = os.getenv("ENV", None)
match ENV:
case "DEV":
return os.getenv("DATABASE_DEV_URL", "sqlite:///dbDev.sqlite")
case "TEST":
return os.getenv("DATABASE_TEST_URL", "sqlite:///dbTest.sqlite")
case "PRO":
try:
user = os.environ["DATABASE_USERNAME"]
password = os.environ["DATABASE_PASSWORD"]
host = os.environ["DATABASE_HOST"]
port: int = int(os.environ["DATABASE_PORT"])
db_name = os.environ["DATABASE_NAME"]
except (ValueError, IndexError) as args:
raise ValueError(args)
else:
return "postgresql://{}:{}@{}:{}/{}".format(
user, password, host, port, db_name
)
def get_cloudinary_uri() -> dict:
try:
api_key = os.environ["API_KEY"]
api_secret = os.environ["API_SECRET"]
name = os.environ["CLOUD_NAME"]
except (ValueError, IndexError) as args:
raise ValueError(args)
else:
return {
"cloud_name": name,
"api_key": api_key,
"api_secret": api_secret,
"secure": True,
}
DB_URI = get_database_uri()
database = databases.Database(DB_URI)
metadata = sqlalchemy.MetaData()
class BaseMeta(ModelMeta):
metadata = metadata
database = database
def build_database():
engine = sqlalchemy.create_engine(DB_URI)
metadata.create_all(engine)
def build_database_test():
engine = sqlalchemy.create_engine(DB_URI)
metadata.drop_all(engine)
metadata.create_all(engine)
async def startup():
await database.connect()
async def shutdown():
await database.disconnect()