File size: 785 Bytes
997f52c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sqlite3
from pathlib import Path

BASE_DIR = Path(__file__).parent
DB_PATH = BASE_DIR / "faculty.db"
SCHEMA_PATH = BASE_DIR / "schema.sql"

print(f"[INIT_DB] DB Path     : {DB_PATH.resolve()}")
print(f"[INIT_DB] Schema Path : {SCHEMA_PATH.resolve()}")

def init_db():
    if not SCHEMA_PATH.exists():
        raise FileNotFoundError(f"schema.sql not found at {SCHEMA_PATH}")

    with sqlite3.connect(DB_PATH) as conn:
        with open(SCHEMA_PATH, "r", encoding="utf-8") as f:
            schema = f.read()

        if not schema.strip():
            raise ValueError("schema.sql is EMPTY")

        conn.executescript(schema)
        conn.commit()

    print("[INIT_DB] Database initialized successfully.")

if __name__ == "__main__":
    init_db()