-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_interface.py
52 lines (49 loc) · 1.87 KB
/
db_interface.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
import os
import json
import psycopg2
from ast import literal_eval
def backup():
json_files = ['guild_language.json', 'responses.json', 'subsettings.json', 'warns.json', 'wikipedia_language.json']
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
for backup_file in json_files:
with open(backup_file) as json_file:
data = json.load(json_file)
cursor = conn.cursor()
delete_query="""DELETE FROM json WHERE name = '{name}'""".format(name=backup_file)
insert_query="""INSERT INTO json (NAME, VALUE) VALUES (%s, %s)"""
record_to_insert = (backup_file, json.dumps(data))
cursor.execute(delete_query)
cursor.execute(insert_query, record_to_insert)
conn.commit()
cursor.close()
conn.close()
def restore(file=None):
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
if file!=None:
cursor = conn.cursor()
select_query="""SELECT VALUE FROM json WHERE name = '{}'""".format(file)
cursor.execute(select_query)
data = cursor.fetchall()
conn.commit()
cursor.close()
data=str(data)[2:-3]
data=literal_eval(data)
return data
json_files = ['guild_language.json', 'responses.json', 'subsettings.json', 'warns.json', 'wikipedia_language.json']
for restore_file in json_files:
cursor = conn.cursor()
select_query="""SELECT VALUE FROM json WHERE name = '{}'""".format(restore_file)
cursor.execute(select_query)
data = cursor.fetchall()
conn.commit()
cursor.close()
try:
data=str(data)[2:-3]
data=literal_eval(data)
with open(restore_file, 'w') as json_file:
json.dump(data, json_file)
except:
pass
conn.close()