-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_init.py
More file actions
64 lines (57 loc) · 2.12 KB
/
db_init.py
File metadata and controls
64 lines (57 loc) · 2.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
"""
Create directory /db, database and tables:
events:
- lat, lon, name, author, location, hrtime, deleteAfter,
- time, website, description, createTime, event_id, verified
event_tags:
- tag, event_id
users:
- user_id, username, display_name, is_admin, hashed_password
likes:
- user_id, event_id
"""
from pathlib import Path
from os.path import split as path_split
from sqlite_handler import SQLiteHandler
from config import config
def db_init():
Path(path_split(config["database_path"])[0]).mkdir(parents=True, exist_ok=True)
with SQLiteHandler() as cur:
cur.executescript(
"""
CREATE TABLE IF NOT EXISTS events (
lat REAL NOT NULL,
lon REAL NOT NULL,
name TEXT NOT NULL,
author TEXT NOT NULL,
location TEXT NOT NULL,
hrtime TEXT NOT NULL,
deleteAfter INT NOT NULL,
time TEXT NOT NULL,
website TEXT NOT NULL,
description TEXT NOT NULL,
createTime TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
event_id TEXT NOT NULL PRIMARY KEY,
verified INT NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS event_tags (
tag TEXT NOT NULL,
event_id TEXT,
FOREIGN KEY (event_id) REFERENCES events(event_id)
);
CREATE TABLE IF NOT EXISTS users (
user_id TEXT NOT NULL PRIMARY KEY,
username TEXT NOT NULL,
display_name TEXT NOT NULL,
is_admin INT NOT NULL DEFAULT 0,
hashed_password TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS likes (
user_id TEXT NOT NULL,
event_id TEXT NOT NULL,
FOREIGN KEY (event_id) REFERENCES events(event_id),
FOREIGN KEY (user_id) REFERENCES events(event_id)
)
""")
if __name__ == "__main__":
db_init()