Skip to content

fix(sessions): support MySQL and MariaDB schema creation - #4741

Open
linhongyu510 wants to merge 1 commit into
openai:mainfrom
linhongyu510:fix/mysql-sqlalchemy-session-schema
Open

fix(sessions): support MySQL and MariaDB schema creation#4741
linhongyu510 wants to merge 1 commit into
openai:mainfrom
linhongyu510:fix/mysql-sqlalchemy-session-schema

Conversation

@linhongyu510

Copy link
Copy Markdown

This pull request fixes automatic SQLAlchemy session table creation on MySQL and MariaDB. Both dialects reject an unbounded String when compiling indexed VARCHAR columns, so SQLAlchemySession(create_tables=True) currently fails before issuing any DDL.

Summary

  • Use a dialect-specific VARCHAR(190) for both session_id columns on MySQL and MariaDB.
  • Preserve the existing unbounded VARCHAR type on PostgreSQL and SQLite.
  • Keep the parent primary-key and child foreign-key column types identical.
  • Use 190 characters so the (session_id, created_at) index remains within the traditional 767-byte InnoDB key limit under utf8mb4.
  • Add offline MetaData.create_all() regression tests for MySQL and MariaDB covering both tables, ON DELETE CASCADE, and the full composite index.

Test plan

  • uv run --extra sqlalchemy pytest -q tests/extensions/memory/test_sqlalchemy_session.py (46 passed)
  • .agents/skills/code-change-verification/scripts/run.sh
    • format: passed
    • lint: passed
    • typecheck: passed
    • tests: passed

Issue number

Closes #4740

Checks

  • I've added new tests, if relevant
  • I've run .agents/skills/code-change-verification/scripts/run.sh
  • I've confirmed all verification steps pass
  • If using Codex, I've run /review before submitting this PR

Signed-off-by: linhongyu510 <linhongyu510@users.noreply.github.com>
@linhongyu510

Copy link
Copy Markdown
Author

The Tests workflow is currently waiting with action_required on this first-time contribution. The branch is a single focused commit, is not behind main, and the changed source/test files pass Ruff and formatting checks locally. Could a maintainer approve the workflow run when convenient?

@linhongyu510

Copy link
Copy Markdown
Author

Follow-up live-database verification completed against a fresh MySQL 8.0.46 instance using SQLAlchemy 2.0.43 and aiomysql 0.3.2.

I exercised the actual SQLAlchemySession.from_url(..., create_tables=True) path, then:

  • inserted one user item and one assistant item,
  • read both items back through get_items(),
  • queried information_schema.COLUMNS and confirmed session_id is VARCHAR(190) in both agent_sessions and agent_messages,
  • queried information_schema.STATISTICS and confirmed idx_agent_messages_session_time contains (session_id, created_at) in that order, and
  • cleared the session and confirmed get_items() returned an empty list.

The run completed with MYSQL_E2E_OK. The database was an ephemeral local container and was removed after the check. No additional code changes were needed.

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.

SQLAlchemySession(create_tables=True) cannot create tables on MySQL due to unbounded String columns

1 participant