Spaces:
Sleeping
Sleeping
Update src/db_connector.py
Browse files8 unmodified lines910111213141516171819202122232423 unmodified lines48495051525354555657585960616260 unmodified lines12312412512612712812911 unmodified lines1411421431441451461478 unmodified linesload_dotenv()self.db_uri = os.getenv("DB_URI")# ✅ FIX: Use /tmp/chatbot.db which is ALWAYS writableself.local_db_path = "/tmp/chatbot.db"if not self.db_uri:print(f"⚠️ DB_URI not found. Using internal safe storage: {self.local_db_path}")self.db_uri = f"sqlite:///{self.local_db_path}"self.type = "sqlite"self.db_path = self.local_db_path# Auto-build the user's dataself._init_sqlite_data()else:parsed = urlparse(self.db_uri)23 unmodified linesdef _init_sqlite_data(self):"""Creates the User's specific schema inside the Safe Temp folder."""# We don't check os.path.exists here because /tmp gets cleared on restart.# We ALWAYS rebuild it to ensure your data is there.print("⚡ Building Database in /tmp/chatbot.db ...")try:conn = sqlite3.connect(self.local_db_path)cursor = conn.cursor()# 1. Clean old data to prevent conflictscursor.execute("DROP TABLE IF EXISTS sales")cursor.execute("DROP TABLE IF EXISTS employees")60 unmodified linescursor.execute("SHOW TABLES")tables = [list(row.values())[0] for row in cursor.fetchall()]# Print to logs so we can verifyprint(f"🔍 TABLES IN DB: {tables}")return tablesexcept Exception as e:11 unmodified linescursor.execute(f"PRAGMA table_info({table_name})")rows = cursor.fetchall()for row in rows:# SQLite PRAGMA returns: (cid, name, type, notnull, dflt_value, pk)col_name = row['name'] if isinstance(row, sqlite3.Row) else row[1]col_type = row['type'] if isinstance(row, sqlite3.Row) else row[2]columns.append(f"{col_name} ({col_type})")8 unmodified lines91011121314151617181920212223242523 unmodified lines49505152535455565758596061626360 unmodified lines12412512612712812911 unmodified lines1411421431441451468 unmodified linesload_dotenv()self.db_uri = os.getenv("DB_URI")# ✅ CRITICAL FIX: Use /tmp which is always writableself.local_db_path = "/tmp/chatbot.db"# Always default to internal DB if URI is missing or failsif not self.db_uri:print(f"⚠️ DB_URI not found. Using safe internal storage: {self.local_db_path}")self.db_uri = f"sqlite:///{self.local_db_path}"self.type = "sqlite"self.db_path = self.local_db_path# Auto-build the user's data every timeself._init_sqlite_data()else:parsed = urlparse(self.db_uri)23 unmodified linesdef _init_sqlite_data(self):"""Creates the User's specific schema inside the Safe Temp folder."""# We REBUILD it on every startup to ensure the schema matches your code.print("⚡ Building Database in /tmp/chatbot.db ...")conn = Nonetry:conn = sqlite3.connect(self.local_db_path)cursor = conn.cursor()# 1. Clean old data to avoid conflictscursor.execute("DROP TABLE IF EXISTS sales")cursor.execute("DROP TABLE IF EXISTS employees")60 unmodified linescursor.execute("SHOW TABLES")tables = [list(row.values())[0] for row in cursor.fetchall()]print(f"🔍 TABLES IN DB: {tables}")return tablesexcept Exception as e:11 unmodified linescursor.execute(f"PRAGMA table_info({table_name})")rows = cursor.fetchall()for row in rows:col_name = row['name'] if isinstance(row, sqlite3.Row) else row[1]col_type = row['type'] if isinstance(row, sqlite3.Row) else row[2]columns.append(f"{col_name} ({col_type})")
src/db_connector.py+8-9