-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathdump-db.py
More file actions
executable file
·115 lines (92 loc) · 3.12 KB
/
dump-db.py
File metadata and controls
executable file
·115 lines (92 loc) · 3.12 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
from prettytable import PrettyTable
import time
import os
if 'SOGS_PGSQL' in os.environ and os.environ['SOGS_PGSQL']:
try:
import psycopg as pg
conn = pg.connect(os.environ['SOGS_PGSQL'], autocommit=True)
except ModuleNotFoundError:
import psycopg2 as pg
conn = pg.connect(os.environ['SOGS_PGSQL'])
conn.autocommit = True
else:
pg = None
import sqlite3
conn = sqlite3.connect('file:sogs.db?mode=ro', uri=True)
cur = conn.cursor()
# Sorting priorities for column names (because different import paths could end up with different
# column orders in the table when we `SELECT *`). Lower = earlier. Anything not in here gets a
# sort value of 100, and equal orders are sorted alphabetically.
column_priority = {
'id': 0,
'room': 1,
'token': 2,
'user': 3,
'session_id': 4,
'name': 4,
'description': 5,
'image': 6,
'admin': 101,
'moderator': 102,
'global_moderator': 103,
'room_moderator': 103,
'visible_mod': 103,
'read': 104,
'accessible': 105,
'write': 106,
'upload': 107,
'banned': 108,
}
def dump_rows(table, extra=None, where=None, order="id", skip=set()):
print(f"{table}:")
ob = order if isinstance(order, str) else ', '.join(order)
extra = f', {extra}' if extra else ''
cur.execute(f"SELECT * {extra} FROM {table} {'WHERE ' + where if where else ''} ORDER BY {ob}")
cols = [x[0] for x in cur.description]
indices = [i for i in range(len(cols)) if cols[i] not in skip]
indices.sort(key=lambda i: (column_priority.get(cols[i], 100), cols[i]))
table = PrettyTable()
table.field_names = [cols[i] for i in indices]
for r in cur:
table.add_row(
[
(
'NULL'
if r[i] is None
else (
int(r[i])
if isinstance(r[i], bool)
else f"{r[i]:.3f}" if isinstance(r[i], float) else r[i]
)
)
for i in indices
]
)
for c in cols:
table.align[c] = 'l' if c in ('token', 'name', 'session_id', 'description', 'path') else 'r'
return table.get_string() + "\n"
print(dump_rows("rooms", skip={'created', 'info_updates'}))
TableNotFoundError = pg.errors.UndefinedTable if pg else sqlite3.OperationalError
try:
print(dump_rows("room_import_hacks", order='room'))
except TableNotFoundError:
pass
print(dump_rows("message_metadata"))
print(dump_rows("pinned_messages", order=("room", "pinned_at")))
print(
dump_rows(
"files",
extra=f"CASE WHEN expiry IS NULL THEN NULL ELSE uploaded > {time.time()-86400} END "
"AS recent_upload",
skip={'uploaded', 'expiry'},
)
)
try:
print(dump_rows("file_id_hacks", order='file'))
except TableNotFoundError:
pass
print(dump_rows("users", where="id != 0", skip={'created', 'last_active'}))
print(dump_rows("room_users", order=('room', '"user"')))
print(dump_rows("user_permissions", where='"user" != 0', order=('room', '"user"')))
print(dump_rows("inbox"))