Aniruddha7 commited on
Commit
4b6d3b6
·
verified ·
1 Parent(s): 4e43601

Restore files the worker imports (parity with original Space)

Browse files
Files changed (1) hide show
  1. Agent/migrate_to_neon.py +54 -0
Agent/migrate_to_neon.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ from sqlalchemy import create_engine, text
4
+ from sqlalchemy.engine import URL
5
+ import pandas as pd
6
+ from dotenv import load_dotenv
7
+
8
+ # Try to find .env in current dir and Agent dir
9
+ load_dotenv()
10
+ load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), '.env'))
11
+ load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), '..', '.env'))
12
+
13
+ # LOCAL_DB = "postgresql://postgres:postgres@localhost:5432/employee"
14
+ LOCAL_DB = os.environ.get("DB_CONNECTION_URL")
15
+ # NEON_DB = Your new Neon connection string
16
+ NEON_DB = os.environ.get("NEON_DB_URL")
17
+
18
+ async def migrate():
19
+ if not NEON_DB:
20
+ print("❌ Error: NEON_DB_URL not found in environment variables.")
21
+ return
22
+
23
+ print("🚀 Starting Cloud Migration to Neon.tech...")
24
+
25
+ # 1. Connect to Local
26
+ local_engine = create_engine(LOCAL_DB)
27
+
28
+ # 2. Connect to Neon (Ensure SSL)
29
+ neon_uri = NEON_DB
30
+ if 'sslmode=' not in neon_uri:
31
+ sep = '&' if '?' in neon_uri else '?'
32
+ neon_uri += f"{sep}sslmode=require"
33
+
34
+ neon_engine = create_engine(neon_uri)
35
+
36
+ tables = ["department", "employee", "dept_emp", "dept_manager", "salary", "title"]
37
+
38
+ for table in tables:
39
+ print(f"📦 Migrating table: {table}...")
40
+ try:
41
+ # Read data from local
42
+ df = pd.read_sql_table(table, local_engine)
43
+ print(f" - Found {len(df)} rows.")
44
+
45
+ # Write to Neon (replace if exists)
46
+ df.to_sql(table, neon_engine, if_exists='replace', index=False)
47
+ print(f" ✅ {table} successfully uploaded to Neon.")
48
+ except Exception as e:
49
+ print(f" ❌ Failed to migrate {table}: {e}")
50
+
51
+ print("\n🎉 Migration Complete! Your cloud database is ready.")
52
+
53
+ if __name__ == "__main__":
54
+ asyncio.run(migrate())