-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
98 lines (80 loc) · 2.88 KB
/
database.py
File metadata and controls
98 lines (80 loc) · 2.88 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
86
87
88
89
90
91
92
93
94
95
96
97
98
import sqlite3
from datetime import datetime
from pathlib import Path
DB_PATH = Path(__file__).resolve().parent / "hiresight.db"
def create_connection():
conn = sqlite3.connect(str(DB_PATH), check_same_thread=False, timeout=10)
conn.row_factory = sqlite3.Row
return conn
def _column_exists(conn, table_name, column_name):
rows = conn.execute(f"PRAGMA table_info({table_name})").fetchall()
return any(row[1] == column_name for row in rows)
def _ensure_history_columns(conn):
required_columns = {
"filename": "TEXT",
"missing_skills": "TEXT",
"strengths": "TEXT",
"recommendations": "TEXT",
"created_at": "TEXT",
}
for column, column_type in required_columns.items():
if not _column_exists(conn, "history", column):
conn.execute(f"ALTER TABLE history ADD COLUMN {column} {column_type}")
if column == "created_at":
conn.execute(
"UPDATE history SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL"
)
def _ensure_user_columns(conn):
if not _column_exists(conn, "users", "created_at"):
conn.execute("ALTER TABLE users ADD COLUMN created_at TEXT")
conn.execute("UPDATE users SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL")
def create_tables():
def _init_schema(conn):
conn.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
role TEXT DEFAULT 'user',
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)
"""
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
filename TEXT,
match_score REAL NOT NULL,
grade TEXT NOT NULL,
missing_skills TEXT,
strengths TEXT,
recommendations TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
)
"""
)
_ensure_user_columns(conn)
_ensure_history_columns(conn)
conn.commit()
conn = None
try:
conn = create_connection()
_init_schema(conn)
except sqlite3.DatabaseError as exc:
if "malformed" not in str(exc).lower():
raise
if conn is not None:
conn.close()
conn = None
stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup = DB_PATH.with_name(f"{DB_PATH.stem}.corrupt.{stamp}{DB_PATH.suffix}")
if DB_PATH.exists():
DB_PATH.replace(backup)
conn = create_connection()
_init_schema(conn)
finally:
if conn is not None:
conn.close()