# Check if we have the data structure (RARBG dumps usually have an 'items' table) # We try to create a clean search index from the source table. try: print("Initializing Search Index (this may take a minute for large DBs)...") cursor.execute(""" CREATE VIRTUAL TABLE IF NOT EXISTS search_index USING FTS5(title, info_hash, content='items', content_rowid='id'); """) # Populate index if it's empty (optimization check omitted for brevity) cursor.execute("INSERT INTO search_index(search_index) VALUES('rebuild');") db.commit() print("Search Index Ready.") except sqlite3.OperationalError as e: print(f"Index creation skipped or failed: e")

if __name__ == '__main__': # Initialize DB and Index on startup with app.app_context(): try: init_search_index() except Exception as e: print(f"Error initializing database: e") print("Ensure 'rarbg_db.sqlite' exists in the same folder and contains an 'items' table.")

# --- Database Helpers ---