generated from Randers-Kommune-Digitalisering/python-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
44 lines (33 loc) · 1.22 KB
/
database.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
import sqlalchemy
import logging
import utils.config
logger = logging.getLogger(__name__)
def get_database_connection(database):
database = database.upper()
if database == 'CLIMATE':
port = getattr(utils.config, database + '_DB_PORT')
databasetype = 'mariadb'
elif database == 'FRONTDESK':
port = None
databasetype = 'mssql'
else:
logger.error(f"Invalid database {database}")
return None
if databasetype == 'mssql':
driver = 'mssql+pymssql'
elif databasetype == 'mariadb':
driver = 'mariadb+mariadbconnector'
elif databasetype == 'postgresql':
driver = 'postgresql+psycopg2'
else:
logger.error(f"Invalid database type {databasetype}")
return None
database = database.upper()
host = getattr(utils.config, database + '_DB_HOST')
username = getattr(utils.config, database + '_DB_USER')
password = getattr(utils.config, database + '_DB_PASS')
db = getattr(utils.config, database + '_DB_DATABASE')
if port:
host = host + ':' + port
engine = sqlalchemy.create_engine(f'{driver}://{username}:{password}@{host}/{db}')
return engine.connect()