LalitChaudhari3 commited on
Commit
659b077
·
verified ·
1 Parent(s): e6490d0

Update src/db_connector.py

Browse files
Files changed (1) hide show
  1. src/db_connector.py +8 -9
src/db_connector.py CHANGED
@@ -9,16 +9,17 @@ class Database:
9
  load_dotenv()
10
  self.db_uri = os.getenv("DB_URI")
11
 
12
- # ✅ FIX: Use /tmp/chatbot.db which is ALWAYS writable
13
  self.local_db_path = "/tmp/chatbot.db"
14
 
 
15
  if not self.db_uri:
16
- print(f"⚠️ DB_URI not found. Using internal safe storage: {self.local_db_path}")
17
  self.db_uri = f"sqlite:///{self.local_db_path}"
18
  self.type = "sqlite"
19
  self.db_path = self.local_db_path
20
 
21
- # Auto-build the user's data
22
  self._init_sqlite_data()
23
  else:
24
  parsed = urlparse(self.db_uri)
@@ -48,15 +49,15 @@ class Database:
48
 
49
  def _init_sqlite_data(self):
50
  """Creates the User's specific schema inside the Safe Temp folder."""
51
- # We don't check os.path.exists here because /tmp gets cleared on restart.
52
- # We ALWAYS rebuild it to ensure your data is there.
53
-
54
  print("⚡ Building Database in /tmp/chatbot.db ...")
 
 
55
  try:
56
  conn = sqlite3.connect(self.local_db_path)
57
  cursor = conn.cursor()
58
 
59
- # 1. Clean old data to prevent conflicts
60
  cursor.execute("DROP TABLE IF EXISTS sales")
61
  cursor.execute("DROP TABLE IF EXISTS employees")
62
 
@@ -123,7 +124,6 @@ class Database:
123
  cursor.execute("SHOW TABLES")
124
  tables = [list(row.values())[0] for row in cursor.fetchall()]
125
 
126
- # Print to logs so we can verify
127
  print(f"🔍 TABLES IN DB: {tables}")
128
  return tables
129
  except Exception as e:
@@ -141,7 +141,6 @@ class Database:
141
  cursor.execute(f"PRAGMA table_info({table_name})")
142
  rows = cursor.fetchall()
143
  for row in rows:
144
- # SQLite PRAGMA returns: (cid, name, type, notnull, dflt_value, pk)
145
  col_name = row['name'] if isinstance(row, sqlite3.Row) else row[1]
146
  col_type = row['type'] if isinstance(row, sqlite3.Row) else row[2]
147
  columns.append(f"{col_name} ({col_type})")
 
9
  load_dotenv()
10
  self.db_uri = os.getenv("DB_URI")
11
 
12
+ # ✅ CRITICAL FIX: Use /tmp which is always writable
13
  self.local_db_path = "/tmp/chatbot.db"
14
 
15
+ # Always default to internal DB if URI is missing or fails
16
  if not self.db_uri:
17
+ print(f"⚠️ DB_URI not found. Using safe internal storage: {self.local_db_path}")
18
  self.db_uri = f"sqlite:///{self.local_db_path}"
19
  self.type = "sqlite"
20
  self.db_path = self.local_db_path
21
 
22
+ # Auto-build the user's data every time
23
  self._init_sqlite_data()
24
  else:
25
  parsed = urlparse(self.db_uri)
 
49
 
50
  def _init_sqlite_data(self):
51
  """Creates the User's specific schema inside the Safe Temp folder."""
52
+ # We REBUILD it on every startup to ensure the schema matches your code.
 
 
53
  print("⚡ Building Database in /tmp/chatbot.db ...")
54
+
55
+ conn = None
56
  try:
57
  conn = sqlite3.connect(self.local_db_path)
58
  cursor = conn.cursor()
59
 
60
+ # 1. Clean old data to avoid conflicts
61
  cursor.execute("DROP TABLE IF EXISTS sales")
62
  cursor.execute("DROP TABLE IF EXISTS employees")
63
 
 
124
  cursor.execute("SHOW TABLES")
125
  tables = [list(row.values())[0] for row in cursor.fetchall()]
126
 
 
127
  print(f"🔍 TABLES IN DB: {tables}")
128
  return tables
129
  except Exception as e:
 
141
  cursor.execute(f"PRAGMA table_info({table_name})")
142
  rows = cursor.fetchall()
143
  for row in rows:
 
144
  col_name = row['name'] if isinstance(row, sqlite3.Row) else row[1]
145
  col_type = row['type'] if isinstance(row, sqlite3.Row) else row[2]
146
  columns.append(f"{col_name} ({col_type})")