Skip to content

Conversation

@DineshThumma9
Copy link
Contributor

@DineshThumma9 DineshThumma9 commented Jan 9, 2026

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

_If applicable, please follow the issue templates to provide as much detail as
possible.

Problem:
In the _get_schema_version_impl function of google/adk/sessions/migration/_schema_check_utils.py file, the SQL query statement uses the MySQL reserved keyword key without escaping, causing a syntax error when executing in MySQL database.

SELECT value FROM adk_internal_metadata WHERE key = :key

Solution:
changed from key to key to use backticks both in _schema_check_utils.py and also in test file

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Please include a summary of passed pytest results.
collected 4 items

tests/unittests/sessions/migration/test_database_schema.py::test_new_db_uses_latest_schema PASSED [ 25%]
tests/unittests/sessions/migration/test_database_schema.py::test_existing_v0_db_uses_v0_schema PASSED [ 50%]
tests/unittests/sessions/migration/test_database_schema.py::test_existing_latest_db_uses_latest_schema PASSED [ 75%]
tests/unittests/sessions/migration/test_migration.py::test_migrate_from_sqlalchemy_pickle PASSED [100%]

================================================================== 4 passed in 8.36s ==================================================================

==================================================================
Manual End-to-End (E2E) Tests:

  • Verified backtick syntax is compatible with SQLite (used in tests)
  • Schema version detection works correctly with the escaped column name
  • No breaking changes to existing functionality

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Add any other context or screenshots about the feature request here.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @DineshThumma9, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical database compatibility issue by correctly handling a MySQL reserved keyword in SQL queries. By escaping the key column name with backticks, the changes ensure that the application's schema version detection functions correctly across various SQL databases, including MySQL, PostgreSQL, and SQLite, preventing potential syntax errors and improving robustness.

Highlights

  • MySQL Reserved Keyword Fix: Addressed a syntax error in SQL queries where the 'key' column name conflicted with a MySQL reserved keyword, causing execution failures.
  • Backtick Escaping Implemented: The 'key' column name in SQL queries has been escaped using backticks (key) to resolve the conflict and ensure proper database interaction.
  • Code and Test Updates: The fix was applied in the core utility file _schema_check_utils.py and its corresponding unit test file test_database_schema.py to maintain consistency and verify the change.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Jan 9, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue with the MySQL reserved keyword key by escaping it with backticks in SQL queries. While this fixes the problem for MySQL, the use of backticks is not portable and will cause issues with other database systems like PostgreSQL. My review provides suggestions to use a database-agnostic approach for quoting identifiers, ensuring the code remains portable across different SQL dialects supported by SQLAlchemy.

Comment on lines 36 to 39
result = connection.execute(
text("SELECT value FROM adk_internal_metadata WHERE key = :key"),
text("SELECT value FROM adk_internal_metadata WHERE `key` = :key"),
{"key": SCHEMA_VERSION_KEY},
).fetchone()
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The use of backticks (`key`) to quote the key column is specific to MySQL and will cause syntax errors on other SQL databases like PostgreSQL. The pull request description incorrectly states that backticks are part of the SQL-92 standard and work on PostgreSQL; the standard uses double quotes. To ensure database portability, you should use a dialect-aware method for quoting identifiers. Since an inspector object is available, inspector.dialect.identifier_preparer.quote() can be used to get the correct quoting for the current database dialect.

      key_col = inspector.dialect.identifier_preparer.quote("key")
      result = connection.execute(
          text(f"SELECT value FROM adk_internal_metadata WHERE {key_col} = :key"),
          {"key": SCHEMA_VERSION_KEY},
      ).fetchone()

Comment on lines 55 to 58
lambda sync_conn: sync_conn.execute(
text('SELECT value FROM adk_internal_metadata WHERE key = :key'),
text('SELECT value FROM adk_internal_metadata WHERE `key` = :key'),
{'key': _schema_check_utils.SCHEMA_VERSION_KEY},
).scalar_one_or_none()
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Similar to the change in _schema_check_utils.py, using backticks for quoting is not portable across different database systems. To make this test compatible with various database backends, you should generate the quoted identifier dynamically. You can create an inspector from the sync_conn object and use it to quote the key column correctly for the dialect under test.

        lambda sync_conn: sync_conn.execute(
            text(
                "SELECT value FROM adk_internal_metadata WHERE"
                f" {inspect(sync_conn).dialect.identifier_preparer.quote('key')} = :key"
            ),
            {'key': _schema_check_utils.SCHEMA_VERSION_KEY},
        ).scalar_one_or_none()

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes an issue where a MySQL reserved keyword key was used in a raw SQL query, causing syntax errors. The solution uses SQLAlchemy's dialect-specific identifier quoting, which is a robust and portable approach. The corresponding test has also been updated. I have one minor suggestion to improve code readability by relocating a comment to its correct logical position.

)
raise
# Metadata table doesn't exist, check for v0 schema.
raise # Metadata table doesn't exist, check for v0 schema.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This comment appears to be misplaced. It describes the logic for when the adk_internal_metadata table doesn't exist, which corresponds to the code path taken after the if inspector.has_table("adk_internal_metadata"): block. Placing it here as an inline comment for the raise statement is misleading, as this except block is only entered if there's an error querying the metadata table that is known to exist. The original placement on a separate line was clearer.

Suggested change
raise # Metadata table doesn't exist, check for v0 schema.
raise
# Metadata table doesn't exist, check for v0 schema.

@DineshThumma9
Copy link
Contributor Author

@gemini-code-assist review my changes now

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes a MySQL syntax error by properly escaping a reserved keyword. The use of SQLAlchemy's dialect-aware quoting is a robust approach. The unit tests are also updated accordingly. I have one suggestion regarding a misplaced comment that affects code readability, but otherwise, the changes are good.

)
raise
# Metadata table doesn't exist, check for v0 schema.
raise # Metadata table doesn't exist, check for v0 schema.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The comment # Metadata table doesn't exist, check for v0 schema. has been moved to an incorrect location. It correctly describes the logic that follows when the adk_internal_metadata table doesn't exist (i.e., the code path after the if inspector.has_table(...) block). By attaching it to this raise statement inside the except block, it becomes misleading, as this block handles any query exception, not just the table's absence.

Please move the comment back to its original location, on a new line after the if block, to accurately describe the fallback logic for checking the v0 schema.

Suggested change
raise # Metadata table doesn't exist, check for v0 schema.
raise

@gemini-code-assist
Copy link
Contributor

Hello @DineshThumma9, it looks like you might have intended to ask for another review. I've already provided an initial summary of your changes in the first comment on this pull request. If you'd like me to perform a new code review based on the current state of the pull request, please use the command /gemini review in a new comment. The command @gemini-code-assist now is not a recognized command.

@DineshThumma9
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a MySQL-specific bug caused by an unescaped reserved keyword (key) in a raw SQL query. The fix is robust, employing SQLAlchemy's dialect-aware quoting mechanism, which is the correct approach. The corresponding test has also been updated appropriately. Overall, this is a high-quality contribution that correctly fixes the issue. I've added a couple of minor suggestions to improve code formatting and readability in the changed sections.

Comment on lines 37 to 42
result = connection.execute(
text("SELECT value FROM adk_internal_metadata WHERE key = :key"),
text(
f"SELECT value FROM adk_internal_metadata WHERE {key_col} = :key"
),
{"key": SCHEMA_VERSION_KEY},
).fetchone()
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved readability, the formatting of this execute call can be made more concise.

      result = connection.execute(
          text(f"SELECT value FROM adk_internal_metadata WHERE {key_col} = :key"),
          {"key": SCHEMA_VERSION_KEY},
      ).fetchone()

@wuliang229 wuliang229 self-requested a review January 9, 2026 19:52
@wuliang229 wuliang229 added the ready to pull [Status] This PR is ready to be importing back to Google label Jan 9, 2026
copybara-service bot pushed a commit that referenced this pull request Jan 10, 2026
The fix will use quotes to escape "key", which is column name in the metadata table. Should work for different database types.

Merge #4106

COPYBARA_INTEGRATE_REVIEW=#4106 from DineshThumma9:fix/mysql-reserved-keyword-issue e39d0d0
PiperOrigin-RevId: 854411915
@adk-bot
Copy link
Collaborator

adk-bot commented Jan 10, 2026

Thank you @DineshThumma9 for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 94d48fc.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot closed this Jan 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to pull [Status] This PR is ready to be importing back to Google services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants