-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_connect.py
24 lines (24 loc) · 922 Bytes
/
db_connect.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
from sqlalchemy import create_engine
import os
import csv
import urllib.parse
currentDBIndex=5
def connect():
global currentDBIndex
dbPostfixes=['a','b']
#dbType='postgresql+pg8000'
dbType='postgresql'
with open('currentDBIndex') as x: currentDBIndex = x.read()
currentDBIndex=1-int(currentDBIndex)
db='db_%s_private.csv' %dbPostfixes[currentDBIndex]
if os.environ.get('LANDO_INFO') is None:
dbConf='db_%s_private.postgres.csv' %dbPostfixes[currentDBIndex]
x.close()
with open(dbConf, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
engine = create_engine('%s://%s:%s@%s:5432/%s' %(dbType,row['user'],urllib.parse.quote_plus(row['password']),row['host'],row['db']), echo=False)
return engine,dbPostfixes[currentDBIndex]
def change_db():
with open('currentDBIndex', 'w') as writer:
writer.write(str(currentDBIndex))