Skip to content

Upgrade SQLAlchemy from v1 to v2 for Python 3.14 support#3290

Closed
MaxGhenis wants to merge 1 commit intoPolicyEngine:masterfrom
MaxGhenis:upgrade-sqlalchemy-v2
Closed

Upgrade SQLAlchemy from v1 to v2 for Python 3.14 support#3290
MaxGhenis wants to merge 1 commit intoPolicyEngine:masterfrom
MaxGhenis:upgrade-sqlalchemy-v2

Conversation

@MaxGhenis
Copy link
Collaborator

Summary

  • Upgrades SQLAlchemy from >=1.4,<2 to >=2,<3 to unblock Python 3.14 support (SQLAlchemy v1 does not support Python 3.14)
  • Replaces removed engine.execute() with connection-based execution (conn.exec_driver_sql()) inside a context manager, with explicit conn.commit()
  • Adds _ResultProxy wrapper that eagerly fetches results within the connection context so they survive connection closure, maintaining the existing fetchone()/fetchall() API contract
  • Replaces LegacyRow (removed in v2) with Row in type annotations across all service files
  • Updates test mocks that used spec=LegacyRow to use plain MagicMock() since Row in v2 has a different interface (no .keys() method)
  • Adds 10 dedicated SQLAlchemy v2 compatibility tests validating _ResultProxy, _execute_remote, and the version requirement

Test plan

  • All 443 existing + new tests pass locally (tests/unit + tests/to_refactor)
  • CI passes on the PR (lint, check-version, test_env_vars, test, docker)
  • Verify the _ResultProxy wrapper correctly handles SELECT, INSERT, UPDATE, DELETE via the new test_sqlalchemy_v2.py tests
  • Confirm no regressions in household, policy, simulation, and report output service tests

🤖 Generated with Claude Code

SQLAlchemy v1 does not support Python 3.14, making this upgrade a blocker.

Key changes:
- Update setup.py pin from sqlalchemy>=1.4,<2 to sqlalchemy>=2,<3
- Replace removed engine.execute() with connection-based execution
  using conn.exec_driver_sql() inside a connection context manager
- Add _ResultProxy wrapper to eagerly fetch results so they survive
  connection closure (maintains existing fetchone()/fetchall() API)
- Replace LegacyRow (removed in v2) with Row in type annotations
- Update test mocks that used spec=LegacyRow to use plain MagicMock()
  since Row in v2 has a different interface
- Add 10 dedicated SQLAlchemy v2 compatibility tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@MaxGhenis MaxGhenis force-pushed the upgrade-sqlalchemy-v2 branch from 562207c to 8cad222 Compare February 25, 2026 15:21
@MaxGhenis
Copy link
Collaborator Author

Recreating from org branch so CI has access to secrets.

@MaxGhenis MaxGhenis closed this Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant