Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions backend/app/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ def get_engine():

# Configure connection pool settings
# For testing, we need more connections since tests run in parallel
pool_size = 20 if settings.ENVIRONMENT == "development" else 5
max_overflow = 30 if settings.ENVIRONMENT == "development" else 10

pool_size = 5
max_overflow = 10
Comment on lines 12 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Stale comment: testing environment doesn't receive more connections.

The comment on line 13 states "For testing, we need more connections since tests run in parallel," but the testing environment is not explicitly handled and falls through to the base defaults (pool_size=5, max_overflow=10). Either update the comment to reflect the actual behavior or add explicit handling for the testing environment if parallel tests require larger pools.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/app/core/db.py` around lines 12 - 15, The comment about increasing
connections for testing is stale because the code sets fixed defaults (pool_size
and max_overflow) and does not special-case the testing environment; either
update the comment to reflect these defaults or add explicit logic that checks
the environment (e.g., an ENV var like "ENV" or "APP_ENV" == "testing") and sets
larger pool_size and/or max_overflow for tests (adjust the variables pool_size
and max_overflow where they are defined) so parallel tests get the intended
larger connection pool.


if settings.ENVIRONMENT == "development":
pool_size = 20
max_overflow = 30
elif settings.ENVIRONMENT == "staging":
pool_size = 16
# test it out
max_overflow = 0
Comment on lines +14 to +23
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if there are any other places where production pool sizing might be configured
# or documented, and verify the ENVIRONMENT values used in the codebase

echo "=== Checking ENVIRONMENT literal values ==="
rg -n "ENVIRONMENT" --type py -C2

echo ""
echo "=== Checking for any production-specific database config ==="
rg -n "production" --type py -C2

echo ""
echo "=== Checking for pool_size mentions elsewhere ==="
rg -n "pool_size" --type py

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 4704


🏁 Script executed:

cat -n backend/app/core/db.py | head -50

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 1994


🏁 Script executed:

rg -n "from sqlalchemy|from sqlmodel|import.*Engine" backend/app/core/db.py -A2 -B2

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 397


🏁 Script executed:

# Also check if there's any config documentation for production sizing
find backend -name "*.md" -o -name "*.txt" -o -name ".env*" | xargs grep -l "pool_size\|production" 2>/dev/null || echo "No matches found"

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 167


🏁 Script executed:

grep -n "pool_size\|production\|database" backend/README.md -C3 | head -40

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 2556


🏁 Script executed:

# Check what type create_engine returns (from sqlmodel/sqlalchemy)
python3 << 'EOF'
from sqlmodel import create_engine
import inspect
# Get the return type annotation
sig = inspect.signature(create_engine)
print(f"create_engine return type: {sig.return_annotation}")
EOF

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 195


🏁 Script executed:

# Check how get_engine() is used to infer return type
rg -n "get_engine\(\)" backend --type py -A3 -B1

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 666


🏁 Script executed:

# Check if Engine type is imported anywhere in the codebase
rg -n "Engine|from sqlalchemy" backend --type py | head -20

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 1695


🏁 Script executed:

# Check for any logging in db.py and verify function signature requirements
cat -n backend/app/core/db.py | tail -30

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 1328


🏁 Script executed:

# Verify Python version requirement in the codebase
rg -n "python_requires\|version" backend/pyproject.toml -A1 -B1 | head -20

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 57


🏁 Script executed:

cat backend/pyproject.toml | head -30

Repository: ProjectTech4DevAI/kaapi-backend

Length of output: 900


Add type hint to get_engine() function and clarify the production pool configuration.

The function at line 7 is missing a return type hint. Add -> Engine to comply with the coding guidelines requiring type hints on all functions. Additionally, the comment at lines 12–13 is misleading—it describes the base defaults, which production actually uses. Consider either:

  • Adding an explicit elif branch for production with a clear comment explaining the sizing strategy, or
  • Clarifying the existing comment to note that production intentionally uses the base configuration.

Currently, production has only 15 potential connections (5 + 10 overflow) compared to 50 for development and 16 for staging.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/app/core/db.py` around lines 14 - 23, Add a return type hint to the
get_engine() function by annotating it with -> Engine; then clarify production
sizing by either adding an explicit elif settings.ENVIRONMENT == "production"
branch that sets/notes pool_size and max_overflow to the intended production
values and includes a comment describing the sizing strategy, or update the
existing comment above the base defaults to state that those defaults are the
production configuration (so production has 5 pool_size + 10 max_overflow = 15
connections). Ensure you reference the get_engine() function and the variables
pool_size and max_overflow when making the change.

return create_engine(
str(settings.SQLALCHEMY_DATABASE_URI),
pool_size=pool_size,
Expand Down
Loading