-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
71 lines (66 loc) · 1.88 KB
/
database.py
File metadata and controls
71 lines (66 loc) · 1.88 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
import sqlite3
from sqlite3 import Connection
DATABASE_NAME = "database.db"
DATABASE_URL = f"sqlite:///{DATABASE_NAME}"
def init_and_get_db() -> Connection:
db = sqlite3.connect(DATABASE_NAME, check_same_thread=False)
cursor = db.cursor()
# TODO - find better alternative for this
# cursor.execute(
# "DROP TABLE IF EXISTS users;"
# )
# cursor.execute(
# "DROP TABLE IF EXISTS boards;"
# )
# cursor.execute(
# "DROP TABLE IF EXISTS columns;"
# )
# cursor.execute(
# "DROP TABLE IF EXISTS items;"
# )
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id VARCHAR(8) PRIMARY KEY,
username VARCHAR(30) UNIQUE NOT NULL,
password VARCHAR(30) NOT NULL
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS boards (
id VARCHAR(10) PRIMARY KEY,
title VARCHAR(100) NOT NULL,
created_by VARCHAR(8) NOT NULL,
FOREIGN KEY (created_by) REFERENCES users (id)
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS columns (
id VARCHAR(10) PRIMARY KEY,
board_id VARCHAR(10) NOT NULL,
title VARCHAR(50) NOT NULL,
position INTEGER NOT NULL,
FOREIGN KEY (board_id) REFERENCES boards (id)
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS items (
id VARCHAR(10) PRIMARY KEY,
column_id VARCHAR(10) NOT NULL,
title VARCHAR(100) NOT NULL,
description TEXT,
position INTEGER NOT NULL,
created_by VARCHAR(10) NOT NULL,
FOREIGN KEY (column_id) REFERENCES columns (id),
FOREIGN KEY (created_by) REFERENCES users (id)
);
"""
)
db.commit()
return db