#!/usr/bin/env python3 """ Clear all databases - PostgreSQL (vector) and Neo4j (graph) """ import asyncio import asyncpg from neo4j import AsyncGraphDatabase # Configuration SUPABASE_DB_URL = "postgresql://postgres:CzkaYmRvc26Y@localhost:5435/postgres" NEO4J_URL = "bolt://localhost:7687" NEO4J_USER = "neo4j" NEO4J_PASSWORD = "langmem_neo4j_password" async def clear_postgresql(): """Clear PostgreSQL database completely""" print("๐Ÿงน Clearing PostgreSQL database...") try: conn = await asyncpg.connect(SUPABASE_DB_URL) # Drop all tables and extensions await conn.execute("DROP SCHEMA public CASCADE;") await conn.execute("CREATE SCHEMA public;") await conn.execute("GRANT ALL ON SCHEMA public TO postgres;") await conn.execute("GRANT ALL ON SCHEMA public TO public;") print(" โœ… PostgreSQL database cleared completely") await conn.close() return True except Exception as e: print(f" โŒ Error clearing PostgreSQL: {e}") return False async def clear_neo4j(): """Clear Neo4j database completely""" print("๐Ÿงน Clearing Neo4j database...") try: driver = AsyncGraphDatabase.driver(NEO4J_URL, auth=(NEO4J_USER, NEO4J_PASSWORD)) async with driver.session() as session: # Delete all nodes and relationships await session.run("MATCH (n) DETACH DELETE n") # Verify it's empty result = await session.run("MATCH (n) RETURN count(n) as count") record = await result.single() node_count = record['count'] print(f" โœ… Neo4j database cleared completely (nodes: {node_count})") await driver.close() return True except Exception as e: print(f" โŒ Error clearing Neo4j: {e}") return False async def restart_langmem_api(): """Restart LangMem API to recreate tables""" print("๐Ÿ”„ Restarting LangMem API to recreate tables...") import subprocess try: # Restart the API container result = subprocess.run( ["docker", "compose", "restart", "langmem-api"], cwd="/home/klas/langmem-project", capture_output=True, text=True ) if result.returncode == 0: print(" โœ… LangMem API restarted successfully") # Wait for API to be ready await asyncio.sleep(3) # Check API health import httpx async with httpx.AsyncClient() as client: try: response = await client.get("http://localhost:8765/health", timeout=10.0) if response.status_code == 200: data = response.json() print(f" โœ… API health status: {data['status']}") return True else: print(f" โš ๏ธ API health check returned: {response.status_code}") return False except Exception as e: print(f" โš ๏ธ API health check failed: {e}") return False else: print(f" โŒ Failed to restart API: {result.stderr}") return False except Exception as e: print(f" โŒ Error restarting API: {e}") return False async def main(): """Main function to clear all databases""" print("๐Ÿš€ Clearing All LangMem Databases") print("=" * 50) # Clear PostgreSQL postgres_cleared = await clear_postgresql() # Clear Neo4j neo4j_cleared = await clear_neo4j() # Restart API to recreate tables api_restarted = await restart_langmem_api() # Summary print("\n" + "=" * 50) print("๐Ÿ“Š Database Clear Summary:") print(f" PostgreSQL: {'โœ… CLEARED' if postgres_cleared else 'โŒ FAILED'}") print(f" Neo4j: {'โœ… CLEARED' if neo4j_cleared else 'โŒ FAILED'}") print(f" API Restart: {'โœ… SUCCESS' if api_restarted else 'โŒ FAILED'}") if all([postgres_cleared, neo4j_cleared, api_restarted]): print("\n๐ŸŽ‰ All databases cleared successfully!") print(" Ready for fresh data storage") return True else: print("\nโš ๏ธ Some operations failed - check logs above") return False if __name__ == "__main__": asyncio.run(main())