Spaces:
Paused

dushyantsingh0 commited on
Commit
36b0f72
·
1 Parent(s): 22a1a45

backend stabilization

Browse files
Files changed (4) hide show
  1. alembic/env.py +2 -1
  2. core/config.py +0 -6
  3. core/database.py +4 -9
  4. seeds/seed_data.py +11 -1
alembic/env.py CHANGED
@@ -92,11 +92,12 @@ async def run_async_migrations() -> None:
92
  config.get_section(config.config_ini_section, {}),
93
  prefix="sqlalchemy.",
94
  poolclass=pool.NullPool,
 
95
  connect_args={
96
  "ssl": "require",
97
  "statement_cache_size": 0,
98
  "prepared_statement_cache_size": 0,
99
- "max_cached_statement_lifetime": 0
100
  }
101
  )
102
 
 
92
  config.get_section(config.config_ini_section, {}),
93
  prefix="sqlalchemy.",
94
  poolclass=pool.NullPool,
95
+ prepared_statement_cache_size=0,
96
  connect_args={
97
  "ssl": "require",
98
  "statement_cache_size": 0,
99
  "prepared_statement_cache_size": 0,
100
+ "command_timeout": 60,
101
  }
102
  )
103
 
core/config.py CHANGED
@@ -48,12 +48,6 @@ class Settings(BaseSettings):
48
  if url.startswith("postgresql://"):
49
  url = url.replace("postgresql://", "postgresql+asyncpg://", 1)
50
 
51
- # Anti-PgBouncer DuplicatePreparedStatementError Fix:
52
- # Force dialect to disable prepared statement caching.
53
- if "prepared_statement_cache_size" not in url:
54
- separator = "&" if "?" in url else "?"
55
- url = f"{url}{separator}prepared_statement_cache_size=0"
56
-
57
  return url
58
 
59
  # Fallback Diagnostic: If we are in the cloud (Linux) but have no URL, something is misconfigured
 
48
  if url.startswith("postgresql://"):
49
  url = url.replace("postgresql://", "postgresql+asyncpg://", 1)
50
 
 
 
 
 
 
 
51
  return url
52
 
53
  # Fallback Diagnostic: If we are in the cloud (Linux) but have no URL, something is misconfigured
core/database.py CHANGED
@@ -38,22 +38,17 @@ def get_engine():
38
  # We rely 100% on the server-side PgBouncer for pooling.
39
  # This prevents SQLAlchemy from keeping 'warm' local connections that
40
  # lead to prepared statement conflicts in transaction-mode proxies.
41
- pool_config = {"poolclass": NullPool}
42
-
43
- logger = get_logger(__name__)
44
- logger.info(f"🛠️ [DB] Mode: ZERO-FOOTPRINT (Worker Detect: {is_worker})")
45
-
46
  _engine = create_async_engine(
47
  settings.async_database_url,
48
  echo=settings.DEBUG,
49
  future=True,
50
- **pool_config,
51
- pool_pre_ping=False, # Disabled for NullPool compatibility and statement safety
 
52
  connect_args={
53
  "ssl": "require",
54
- "statement_cache_size": 0,
55
  "prepared_statement_cache_size": 0, # Redundant guard for different driver versions
56
- "max_cached_statement_lifetime": 0,
57
  "command_timeout": 60,
58
  }
59
  )
 
38
  # We rely 100% on the server-side PgBouncer for pooling.
39
  # This prevents SQLAlchemy from keeping 'warm' local connections that
40
  # lead to prepared statement conflicts in transaction-mode proxies.
 
 
 
 
 
41
  _engine = create_async_engine(
42
  settings.async_database_url,
43
  echo=settings.DEBUG,
44
  future=True,
45
+ poolclass=NullPool,
46
+ pool_pre_ping=False, # Disabled for statement safety in transaction mode
47
+ prepared_statement_cache_size=0, # Force dialect to disable statement preparation
48
  connect_args={
49
  "ssl": "require",
50
+ "statement_cache_size": 0, # Force driver to disable internal cache
51
  "prepared_statement_cache_size": 0, # Redundant guard for different driver versions
 
52
  "command_timeout": 60,
53
  }
54
  )
seeds/seed_data.py CHANGED
@@ -20,7 +20,17 @@ from core.llm import get_embeddings
20
  settings = get_settings()
21
 
22
  # Create local engine for seeding
23
- engine = create_async_engine(settings.DATABASE_URL, echo=True)
 
 
 
 
 
 
 
 
 
 
24
  async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
25
 
26
  async def seed_data():
 
20
  settings = get_settings()
21
 
22
  # Create local engine for seeding
23
+ engine = create_async_engine(
24
+ settings.async_database_url,
25
+ echo=True,
26
+ poolclass=NullPool,
27
+ prepared_statement_cache_size=0,
28
+ connect_args={
29
+ "ssl": "require",
30
+ "statement_cache_size": 0,
31
+ "prepared_statement_cache_size": 0
32
+ }
33
+ )
34
  async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
35
 
36
  async def seed_data():