Skip to content

Commit 94d48fc

Browse files
DineshThumma9copybara-github
authored andcommitted
fix: Database reserved keyword issue
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
1 parent 2bd984a commit 94d48fc

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/google/adk/sessions/migration/_schema_check_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Database schema version check utility."""
15+
1516
from __future__ import annotations
1617

1718
import logging
@@ -32,8 +33,11 @@ def _get_schema_version_impl(inspector, connection) -> str:
3233
"""Gets DB schema version using inspector and connection."""
3334
if inspector.has_table("adk_internal_metadata"):
3435
try:
36+
key_col = inspector.dialect.identifier_preparer.quote("key")
3537
result = connection.execute(
36-
text("SELECT value FROM adk_internal_metadata WHERE key = :key"),
38+
text(
39+
f"SELECT value FROM adk_internal_metadata WHERE {key_col} = :key"
40+
),
3741
{"key": SCHEMA_VERSION_KEY},
3842
).fetchone()
3943
if result:
@@ -49,6 +53,7 @@ def _get_schema_version_impl(inspector, connection) -> str:
4953
e,
5054
)
5155
raise
56+
5257
# Metadata table doesn't exist, check for v0 schema.
5358
# V0 schema has an 'events' table with an 'actions' column.
5459
if inspector.has_table("events"):

tests/unittests/sessions/migration/test_database_schema.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ async def test_new_db_uses_latest_schema(tmp_path):
5151
lambda sync_conn: inspect(sync_conn).has_table('adk_internal_metadata')
5252
)
5353
assert has_metadata_table
54-
schema_version = await conn.run_sync(
55-
lambda sync_conn: sync_conn.execute(
56-
text('SELECT value FROM adk_internal_metadata WHERE key = :key'),
57-
{'key': _schema_check_utils.SCHEMA_VERSION_KEY},
58-
).scalar_one_or_none()
59-
)
54+
55+
def get_schema_version(sync_conn):
56+
inspector = inspect(sync_conn)
57+
key_col = inspector.dialect.identifier_preparer.quote('key')
58+
return sync_conn.execute(
59+
text(
60+
f'SELECT value FROM adk_internal_metadata WHERE {key_col} = :key'
61+
),
62+
{'key': _schema_check_utils.SCHEMA_VERSION_KEY},
63+
).scalar_one_or_none()
64+
65+
schema_version = await conn.run_sync(get_schema_version)
6066
assert schema_version == _schema_check_utils.LATEST_SCHEMA_VERSION
6167

6268
# Verify events table columns for v1

0 commit comments

Comments
 (0)