Commit a012bb7
fix: strip tzinfo from datetime for MySQL in create_session
Merge google#5084
### Link to Issue or Description of Change
Fixes google#5085
When using `DatabaseSessionService` with MySQL, the very first `append_event()` after `create_session()` always fails with:
```
ValueError: The session has been modified in storage since it was loaded.
Please reload the session before appending more events.
```
**Root cause:** `create_session()` produces a timezone-aware `_storage_update_marker` (with `+00:00`), but MySQL `DATETIME(fsp=6)` doesn't store timezone info ([MySQL docs](https://dev.mysql.com/doc/refman/8.0/en/datetime.html)). When `append_event()` reads `update_time` back, it gets a naive datetime — different marker string, strict comparison fails.
The code already strips `tzinfo` for SQLite and PostgreSQL but MySQL was missed:
```python
# Before (line 464):
if is_sqlite or is_postgresql: # MySQL not included
now = now.replace(tzinfo=None)
# After:
is_mysql = self.db_engine.dialect.name == _MYSQL_DIALECT
if is_sqlite or is_postgresql or is_mysql:
now = now.replace(tzinfo=None)
```
### Testing Plan
**Unit Tests:**
- [x] I have added or updated unit tests for my change.
- [x] All unit tests pass locally.
Added `mysql` to the existing parametrized dialect test. Removed the test that incorrectly asserted MySQL preserves timezone.
**Manual End-to-End (E2E) Tests:**
Verified marker values with Testcontainers MySQL 8.0:
| | create_session marker | get_session marker | Match? |
|---|---|---|---|
| Before fix | `...T14:39:46.014977+00:00` | `...T14:39:46.014977` | No |
| After fix | `...T14:44:49.388273` | `...T14:44:49.388273` | Yes |
Confirmed that `append_event()` works immediately after `create_session()` without needing a re-fetch workaround.
### Checklist
- [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] New and existing unit tests pass locally with my changes.
- [x] I have manually tested my changes end-to-end.
- [x] Any dependent changes have been merged and published in downstream modules.
Co-authored-by: Haran Rajkumar <haranrk@google.com>
COPYBARA_INTEGRATE_REVIEW=google#5084 from andreikravchenko-oviva:fix/mysql-stale-session-marker 30bccf1
PiperOrigin-RevId: 9344462691 parent 066fbce commit a012bb7
2 files changed
Lines changed: 11 additions & 26 deletions
File tree
- src/google/adk/sessions
- tests/unittests/sessions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
531 | 531 | | |
532 | 532 | | |
533 | 533 | | |
534 | | - | |
| 534 | + | |
| 535 | + | |
535 | 536 | | |
536 | 537 | | |
537 | 538 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
113 | | - | |
| 112 | + | |
114 | 113 | | |
115 | | - | |
116 | | - | |
117 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
| 123 | + | |
122 | 124 | | |
123 | 125 | | |
124 | 126 | | |
125 | 127 | | |
126 | | - | |
| 128 | + | |
127 | 129 | | |
128 | 130 | | |
129 | | - | |
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | 134 | | |
151 | 135 | | |
152 | 136 | | |
| |||
0 commit comments