Fix: Make DB path robust for Docker
This commit is contained in:
19
db.py
19
db.py
@@ -1,10 +1,20 @@
|
||||
import sqlite3
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
DB_PATH = 'lead-engine/data/leads.db'
|
||||
# Robust path handling:
|
||||
# 1. Get the directory where this script (db.py) lives
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
# 2. Define data directory and db path relative to it
|
||||
DATA_DIR = os.path.join(BASE_DIR, 'data')
|
||||
DB_PATH = os.path.join(DATA_DIR, 'leads.db')
|
||||
|
||||
def init_db():
|
||||
# Ensure data directory exists
|
||||
if not os.path.exists(DATA_DIR):
|
||||
os.makedirs(DATA_DIR)
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('''
|
||||
@@ -27,6 +37,10 @@ def init_db():
|
||||
conn.close()
|
||||
|
||||
def insert_lead(lead_data):
|
||||
# Ensure DB exists before inserting (if ingest runs before init)
|
||||
if not os.path.exists(DB_PATH):
|
||||
init_db()
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
try:
|
||||
@@ -51,6 +65,9 @@ def insert_lead(lead_data):
|
||||
conn.close()
|
||||
|
||||
def get_leads():
|
||||
if not os.path.exists(DB_PATH):
|
||||
init_db()
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
c = conn.cursor()
|
||||
|
||||
Reference in New Issue
Block a user