-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
68 lines (58 loc) · 2.19 KB
/
database.py
File metadata and controls
68 lines (58 loc) · 2.19 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
import sqlite3
import os
def init_db():
"""Initialize the SQLite database for movie matching functionality"""
db_path = os.path.join(os.path.dirname(__file__), 'movie_match.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Create match rooms table
cursor.execute('''
CREATE TABLE IF NOT EXISTS match_rooms (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
code TEXT UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP,
library_filter TEXT DEFAULT 'Movies',
min_participants INTEGER DEFAULT 2
)
''')
# Create room users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS room_users (
room_id TEXT,
user_id TEXT,
username TEXT,
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (room_id, user_id),
FOREIGN KEY (room_id) REFERENCES match_rooms(id) ON DELETE CASCADE
)
''')
# Create movie swipes table
cursor.execute('''
CREATE TABLE IF NOT EXISTS movie_swipes (
room_id TEXT,
user_id TEXT,
movie_id TEXT,
movie_title TEXT,
movie_year INTEGER,
swipe_direction TEXT CHECK(swipe_direction IN ('left', 'right', 'super')),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (room_id, user_id, movie_id),
FOREIGN KEY (room_id) REFERENCES match_rooms(id) ON DELETE CASCADE
)
''')
# Create index for faster queries
cursor.execute('CREATE INDEX IF NOT EXISTS idx_room_swipes ON movie_swipes(room_id, movie_id)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_user_swipes ON movie_swipes(room_id, user_id)')
conn.commit()
conn.close()
print(f"Database initialized at: {db_path}")
def get_db_connection():
"""Get a database connection"""
db_path = os.path.join(os.path.dirname(__file__), 'movie_match.db')
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row # Enable dict-like access to rows
return conn
if __name__ == "__main__":
init_db()