-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateDb.py
46 lines (38 loc) · 1.61 KB
/
updateDb.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
'''
This utility will update the current r8dium database (in csv) to the most recent schema
- Currently this involves adding a new field (column) to store the last login date of each user
'''
import shutil
input_name = 'r8diumDb'
output_name = input_name + '-update'
ifp = open(input_name + '.csv', 'r')
ofp = open(output_name + '.csv', 'w')
shutil.copy(input_name + '.csv', input_name + '.BAK') # create a backup of the original db
for line in ifp.readlines():
if line.find('discord_name,discord_id') > 0:
if line.find(',last_login') < 0: # Header hasn't been modified
ins_pt = line.find(',ip')
new_header = line[:ins_pt] + ',last_login,active' + line[ins_pt:]
ofp.write(new_header)
else:
print('found first line has already been updated')
ofp.write(line)
else:
if line.count(',') == 12:
# This is an old format, so update by adding two new fields (last_login, active?)
cpos = 0
cfnd = 0
for char in line:
if char == ',':
cfnd += 1
cpos += 1
if cfnd == 8:
cloc = cpos
# insert two new fields - blank for last_login, and True for active
new_line = line[:cloc] + ',True,' + line[cloc:]
ofp.write(new_line)
ofp.close()
ifp.close()
print(f'Database updated and stored as {output_name}.csv')
print(f'It is suggested to compare the existing database to the newly created one before copying over.')
print(f'A backup of the original database has been saved as {input_name}.BAK')