-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
85 lines (73 loc) · 3.31 KB
/
Copy pathdatabase.py
File metadata and controls
85 lines (73 loc) · 3.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import aiosqlite
import os
# Use env var for DB path (critical for Railway Volumes), default to local file
DB_FILE = os.getenv("DB_FILE", "prices.db")
async def initialize():
# Ensure directory exists if DB_FILE is in a subdirectory (like /data/prices.db)
db_dir = os.path.dirname(DB_FILE)
if db_dir:
os.makedirs(db_dir, exist_ok=True)
async with aiosqlite.connect(DB_FILE) as db:
await db.execute("""
CREATE TABLE IF NOT EXISTS watches (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
url TEXT,
product_name TEXT,
current_price REAL,
previous_price REAL
)
""")
await db.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_user_url ON watches(user_id, url)")
cursor = await db.execute("PRAGMA table_info(watches)")
columns = [row[1] for row in await cursor.fetchall()]
if "target_price" in columns or "previous_price" not in columns:
await db.execute("ALTER TABLE watches RENAME TO watches_old")
await db.execute("""
CREATE TABLE watches (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
url TEXT,
product_name TEXT,
current_price REAL,
previous_price REAL
)
""")
await db.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_user_url ON watches(user_id, url)")
await db.execute("""
INSERT INTO watches (id, user_id, url, product_name, current_price, previous_price)
SELECT id, user_id, url, product_name, current_price, current_price FROM watches_old
""")
await db.execute("DROP TABLE watches_old")
await db.commit()
async def add_watch(user_id, url, name, price):
async with aiosqlite.connect(DB_FILE) as db:
try:
await db.execute(
"INSERT INTO watches (user_id, url, product_name, current_price, previous_price) VALUES (?, ?, ?, ?, ?)",
(user_id, url, name, price, price)
)
await db.commit()
return True
except aiosqlite.IntegrityError:
return False
async def get_user_watches(user_id):
async with aiosqlite.connect(DB_FILE) as db:
db.row_factory = aiosqlite.Row
async with db.execute("SELECT id, product_name, current_price, previous_price FROM watches WHERE user_id = ?", (user_id,)) as cursor:
return await cursor.fetchall()
async def delete_watch(watch_id, user_id):
async with aiosqlite.connect(DB_FILE) as db:
await db.execute("DELETE FROM watches WHERE id = ? AND user_id = ?", (watch_id, user_id))
await db.commit()
async def stream_watches():
db = await aiosqlite.connect(DB_FILE)
cursor = await db.execute("SELECT id, url, current_price, user_id, product_name FROM watches")
return db, cursor
async def update_price(watch_id, new_price):
async with aiosqlite.connect(DB_FILE) as db:
await db.execute(
"UPDATE watches SET previous_price = current_price, current_price = ? WHERE id = ?",
(new_price, watch_id)
)
await db.commit()