-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmigrate_db.py
More file actions
55 lines (44 loc) · 2.16 KB
/
migrate_db.py
File metadata and controls
55 lines (44 loc) · 2.16 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
import asyncio
import aiosqlite
from pathlib import Path
async def main():
"""
Applies database migrations to an existing SQLite database.
- Adds indexes for performance.
- Ensures all columns exist.
"""
base_dir = Path(__file__).resolve().parent.parent
db_path = base_dir / "data.sqlite3"
if not db_path.exists():
print(f"Database not found at {db_path}, skipping migration.")
return
print(f"Applying migrations to {db_path}...")
async with aiosqlite.connect(db_path) as conn:
# 1. Add indexes
print("Creating indexes...")
await conn.execute("CREATE INDEX IF NOT EXISTS idx_accounts_enabled ON accounts (enabled);")
await conn.execute("CREATE INDEX IF NOT EXISTS idx_accounts_created_at ON accounts (created_at);")
await conn.execute("CREATE INDEX IF NOT EXISTS idx_accounts_success_count ON accounts (success_count);")
print("Indexes created successfully.")
# 2. Add missing columns (idempotent)
print("Checking for missing columns...")
try:
async with conn.execute("PRAGMA table_info(accounts)") as cursor:
rows = await cursor.fetchall()
cols = [row[1] for row in rows]
if "enabled" not in cols:
print("Adding 'enabled' column...")
await conn.execute("ALTER TABLE accounts ADD COLUMN enabled INTEGER DEFAULT 1")
if "error_count" not in cols:
print("Adding 'error_count' column...")
await conn.execute("ALTER TABLE accounts ADD COLUMN error_count INTEGER DEFAULT 0")
if "success_count" not in cols:
print("Adding 'success_count' column...")
await conn.execute("ALTER TABLE accounts ADD COLUMN success_count INTEGER DEFAULT 0")
print("Column check complete.")
except Exception as e:
print(f"Error checking/adding columns: {e}")
await conn.commit()
print("Migrations applied successfully.")
if __name__ == "__main__":
asyncio.run(main())