Add db migrations, update watch.sh for uv + idempotent runs
- Schema versioned with migrations table - watch.sh runs db migrations on every pull - watch.sh uses uv run/sync instead of pip - Fixed parser.py reference in watch.sh
This commit is contained in:
45
db.py
45
db.py
@@ -15,10 +15,10 @@ def get_db() -> sqlite3.Connection:
|
||||
return conn
|
||||
|
||||
|
||||
def init_db():
|
||||
"""Create tables if they don't exist."""
|
||||
conn = get_db()
|
||||
conn.executescript("""
|
||||
SCHEMA_VERSION = 2
|
||||
|
||||
MIGRATIONS = {
|
||||
1: """
|
||||
CREATE TABLE IF NOT EXISTS repos (
|
||||
name TEXT PRIMARY KEY,
|
||||
url TEXT,
|
||||
@@ -34,7 +34,7 @@ def init_db():
|
||||
language TEXT,
|
||||
documentation TEXT,
|
||||
prev_documentation TEXT,
|
||||
functions TEXT, -- JSON array
|
||||
functions TEXT,
|
||||
last_commit TEXT,
|
||||
staleness TEXT DEFAULT 'fresh',
|
||||
updated_at TEXT
|
||||
@@ -54,9 +54,42 @@ def init_db():
|
||||
CREATE INDEX IF NOT EXISTS idx_files_staleness ON files(staleness);
|
||||
CREATE INDEX IF NOT EXISTS idx_rels_staleness ON relationships(staleness);
|
||||
CREATE INDEX IF NOT EXISTS idx_rels_to ON relationships(to_file);
|
||||
""")
|
||||
""",
|
||||
2: """
|
||||
CREATE TABLE IF NOT EXISTS schema_version (
|
||||
version INTEGER PRIMARY KEY
|
||||
);
|
||||
""",
|
||||
}
|
||||
|
||||
|
||||
def init_db():
|
||||
"""Create or migrate the database schema."""
|
||||
conn = get_db()
|
||||
|
||||
# Check current version
|
||||
try:
|
||||
row = conn.execute("SELECT MAX(version) AS v FROM schema_version").fetchone()
|
||||
current = row["v"] or 0
|
||||
except sqlite3.OperationalError:
|
||||
current = 0
|
||||
|
||||
if current >= SCHEMA_VERSION:
|
||||
conn.close()
|
||||
return
|
||||
|
||||
# Run pending migrations
|
||||
for v in range(current + 1, SCHEMA_VERSION + 1):
|
||||
if v in MIGRATIONS:
|
||||
print(f" [db] Running migration v{v}...")
|
||||
conn.executescript(MIGRATIONS[v])
|
||||
|
||||
# Ensure schema_version table exists and record version
|
||||
conn.execute("CREATE TABLE IF NOT EXISTS schema_version (version INTEGER PRIMARY KEY)")
|
||||
conn.execute("INSERT OR REPLACE INTO schema_version (version) VALUES (?)", (SCHEMA_VERSION,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print(f" [db] Schema at v{SCHEMA_VERSION}")
|
||||
|
||||
|
||||
class GraphDB:
|
||||
|
||||
Reference in New Issue
Block a user