Skip to content

Commit 6d99edb

Browse files
fix: enable sync_to_postgres.py for local DATABASE_URL
Added _create_direct_engine_sync() function that uses pg8000 driver for sync database operations. This allows running the sync script locally with DATABASE_URL instead of only in Cloud SQL environments. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2cd1449 commit 6d99edb

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

core/database/connection.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_conn():
7171

7272

7373
def _create_direct_engine():
74-
"""Create engine using direct DATABASE_URL connection."""
74+
"""Create async engine using direct DATABASE_URL connection."""
7575
url = DATABASE_URL
7676

7777
# Ensure async driver
@@ -100,25 +100,55 @@ def _create_direct_engine():
100100
return engine
101101

102102

103+
def _create_direct_engine_sync():
104+
"""Create sync engine using direct DATABASE_URL connection (for sync scripts)."""
105+
from sqlalchemy import create_engine
106+
107+
url = DATABASE_URL
108+
109+
# Use pg8000 sync driver (already installed for Cloud SQL)
110+
if url.startswith("postgresql+asyncpg://"):
111+
url = url.replace("postgresql+asyncpg://", "postgresql+pg8000://")
112+
elif url.startswith("postgresql://"):
113+
url = url.replace("postgresql://", "postgresql+pg8000://")
114+
elif url.startswith("postgres://"):
115+
url = url.replace("postgres://", "postgresql+pg8000://")
116+
117+
engine_kwargs = {
118+
"echo": ENVIRONMENT == "development",
119+
"pool_size": 5,
120+
"max_overflow": 10,
121+
"pool_pre_ping": True,
122+
}
123+
124+
engine = create_engine(url, **engine_kwargs)
125+
126+
# Log without exposing password
127+
safe_url = url.split("@")[-1] if "@" in url else "local"
128+
logger.info(f"Created direct sync database engine: {safe_url}")
129+
return engine
130+
131+
103132
def init_db_sync() -> None:
104133
"""
105-
Initialize sync database connection (for scripts in GitHub Actions).
134+
Initialize sync database connection (for scripts like sync_to_postgres.py).
106135
107-
Uses pg8000 sync driver with Cloud SQL Connector to avoid event loop issues.
136+
Uses sync drivers for both local (psycopg2) and Cloud SQL (pg8000).
108137
"""
109138
global engine, _sync_session_factory
110139

111-
if engine is not None:
140+
if _sync_session_factory is not None:
112141
return # Already initialized
113142

143+
from sqlalchemy.orm import sessionmaker
144+
114145
if DATABASE_URL:
115-
# For DATABASE_URL, create async engine (used locally)
116-
engine = _create_direct_engine()
146+
# Use sync engine for local development
147+
engine = _create_direct_engine_sync()
148+
_sync_session_factory = sessionmaker(engine, expire_on_commit=False)
117149
elif INSTANCE_CONNECTION_NAME:
118150
# Use sync pg8000 driver for Cloud SQL Connector
119151
engine = _create_cloud_sql_engine_sync()
120-
from sqlalchemy.orm import sessionmaker
121-
122152
_sync_session_factory = sessionmaker(engine, expire_on_commit=False)
123153
else:
124154
logger.warning("No database configuration found - running without database")

0 commit comments

Comments
 (0)