Skip to content

Commit 35a15b4

Browse files
ryan-williamsclaude
andcommitted
Initialize database tables on server startup
Add `init_db()` function that creates `scan` and `scan_progress` tables with `CREATE TABLE IF NOT EXISTS`. This fixes the e2e tests failing in CI with "no such table: scan" when starting with a fresh database. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent d9352b6 commit 35a15b4

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/disk_tree/server.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from os import listdir, remove, stat
2+
from os import listdir, makedirs, remove, stat
33
from os.path import abspath, dirname, exists, isdir, isfile, join
44
import shutil
55
import sqlite3
@@ -20,6 +20,50 @@
2020

2121
DB_PATH = abspath(SQLITE_PATH)
2222

23+
24+
def init_db():
25+
"""Initialize the database with required tables if they don't exist."""
26+
from os.path import dirname
27+
makedirs(dirname(DB_PATH), exist_ok=True)
28+
conn = sqlite3.connect(DB_PATH)
29+
try:
30+
conn.execute('''
31+
CREATE TABLE IF NOT EXISTS scan (
32+
id INTEGER PRIMARY KEY AUTOINCREMENT,
33+
path TEXT NOT NULL,
34+
time DATETIME NOT NULL,
35+
blob TEXT NOT NULL,
36+
error_count INTEGER,
37+
error_paths TEXT,
38+
size INTEGER,
39+
n_children INTEGER,
40+
n_desc INTEGER
41+
)
42+
''')
43+
conn.execute('''
44+
CREATE INDEX IF NOT EXISTS ix_scan_path_time ON scan (path, time)
45+
''')
46+
conn.execute('''
47+
CREATE TABLE IF NOT EXISTS scan_progress (
48+
id INTEGER PRIMARY KEY AUTOINCREMENT,
49+
path TEXT NOT NULL UNIQUE,
50+
pid INTEGER NOT NULL,
51+
started DATETIME NOT NULL,
52+
items_found INTEGER NOT NULL DEFAULT 0,
53+
items_per_sec INTEGER,
54+
error_count INTEGER NOT NULL DEFAULT 0,
55+
status TEXT NOT NULL DEFAULT 'running'
56+
)
57+
''')
58+
conn.commit()
59+
finally:
60+
conn.close()
61+
62+
63+
# Initialize database on module load
64+
init_db()
65+
66+
2367
# Static file serving for bundled UI
2468
# Check multiple locations: packaged static/, dev ui/dist/
2569
_this_dir = dirname(abspath(__file__))

0 commit comments

Comments
 (0)