forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
44 lines (39 loc) · 1.07 KB
/
Copy pathdatabase.py
File metadata and controls
44 lines (39 loc) · 1.07 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
import aiosqlite
import os
DB_PATH = os.path.join(os.path.dirname(__file__), 'sns_api.db')
POSTS_SCHEMA = """
CREATE TABLE IF NOT EXISTS posts (
id TEXT PRIMARY KEY,
username TEXT NOT NULL,
content TEXT NOT NULL,
createdAt TEXT NOT NULL,
updatedAt TEXT NOT NULL,
likes INTEGER NOT NULL DEFAULT 0,
comments INTEGER NOT NULL DEFAULT 0
);
"""
COMMENTS_SCHEMA = """
CREATE TABLE IF NOT EXISTS comments (
id TEXT PRIMARY KEY,
postId TEXT NOT NULL,
username TEXT NOT NULL,
content TEXT NOT NULL,
createdAt TEXT NOT NULL,
updatedAt TEXT NOT NULL,
FOREIGN KEY(postId) REFERENCES posts(id) ON DELETE CASCADE
);
"""
LIKES_SCHEMA = """
CREATE TABLE IF NOT EXISTS likes (
postId TEXT NOT NULL,
username TEXT NOT NULL,
PRIMARY KEY(postId, username),
FOREIGN KEY(postId) REFERENCES posts(id) ON DELETE CASCADE
);
"""
async def init_db():
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(POSTS_SCHEMA)
await db.execute(COMMENTS_SCHEMA)
await db.execute(LIKES_SCHEMA)
await db.commit()