-
Notifications
You must be signed in to change notification settings - Fork 59
/
utils.py
63 lines (58 loc) · 1.45 KB
/
utils.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
53
54
55
56
57
58
59
60
61
62
63
import sqlite3
def migrate_db(conn, cursor):
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS messages (
message TEXT,
user TEXT,
channel TEXT,
timestamp TEXT,
UNIQUE(channel, timestamp) ON CONFLICT REPLACE
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS users (
name TEXT,
id TEXT,
avatar TEXT,
UNIQUE(id) ON CONFLICT REPLACE
)"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS channels (
name TEXT,
id TEXT,
is_private BOOLEAN NOT NULL CHECK (is_private IN (0,1)),
UNIQUE(id) ON CONFLICT REPLACE
)"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS members (
channel TEXT,
user TEXT,
FOREIGN KEY (channel) REFERENCES channels(id),
FOREIGN KEY (user) REFERENCES users(id)
)
"""
)
conn.commit()
# Add `is_private` to channels for dbs that existed in v0.1
try:
cursor.execute(
"""
ALTER TABLE channels
ADD COLUMN is_private BOOLEAN default 1
NOT NULL CHECK (is_private IN (0,1))
"""
)
conn.commit()
except:
pass
def db_connect(database_path):
conn = sqlite3.connect(database_path)
cursor = conn.cursor()
return conn, cursor