Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions src/google/adk/sessions/migration/_schema_check_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Database schema version check utility."""

from __future__ import annotations

import logging
Expand All @@ -33,7 +34,7 @@ def _get_schema_version_impl(inspector, connection) -> str:
if inspector.has_table("adk_internal_metadata"):
try:
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()
Comment on lines 37 to 42
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()

if result:
Expand All @@ -48,8 +49,7 @@ def _get_schema_version_impl(inspector, connection) -> str:
"Failed to query schema version from adk_internal_metadata: %s.",
e,
)
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.

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

# V0 schema has an 'events' table with an 'actions' column.
if inspector.has_table("events"):
try:
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/sessions/migration/test_database_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def test_new_db_uses_latest_schema(tmp_path):
assert has_metadata_table
schema_version = await conn.run_sync(
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()

)
Expand Down