import os
import psycopg2
from neo4j import GraphDatabase
from dotenv import load_dotenv
load_dotenv()
# Postgres Connection
def get_postgres_connection():
return psycopg2.connect(
host="localhost",
database="trip_db",
user="admin",
password="password123"
)
# Neo4j Connection
def get_neo4j_driver():
uri = "bolt://localhost:7687"
username = "neo4j"
password = "password123"
return GraphDatabase.driver(uri, auth=(username, password))
# Seed Neo4j Data
def seed_neo4j():
driver = get_neo4j_driver()
# Path to the Cypher init file
current_dir = os.path.dirname(os.path.abspath(__file__))
cypher_file_path = os.path.join(current_dir, '..', 'data', 'neo4j_init.cypher')
try:
with open(cypher_file_path, 'r') as f:
cypher_script = f.read()
# Split by semicolon to get individual statements
# This is a simple split and might need robustness for complex queries with semicolons in strings
statements = [s.strip() for s in cypher_script.split(';') if s.strip()]
with driver.session() as session:
for statement in statements:
session.run(statement)
print(f"Neo4j data seeded successfully from {cypher_file_path}")
except FileNotFoundError:
print(f"Error: Cypher init file not found at {cypher_file_path}")
except Exception as e:
print(f"Error seeding Neo4j: {e}")
finally:
driver.close()
if __name__ == "__main__":
# Run this to seed data manually if needed
try:
seed_neo4j()
except Exception as e:
print(f"Error seeding Neo4j: {e}")