|
23 | 23 | from google.adk.sessions.migration import migrate_from_sqlalchemy_pickle as mfsp |
24 | 24 | from google.adk.sessions.schemas import v0 |
25 | 25 | from google.adk.sessions.schemas import v1 |
| 26 | +import pytest |
26 | 27 | from sqlalchemy import create_engine |
27 | 28 | from sqlalchemy.orm import sessionmaker |
28 | 29 |
|
29 | 30 |
|
| 31 | +class TestToSyncUrl: |
| 32 | + """Tests for the to_sync_url function.""" |
| 33 | + |
| 34 | + @pytest.mark.parametrize( |
| 35 | + "input_url,expected_url", |
| 36 | + [ |
| 37 | + # PostgreSQL async drivers |
| 38 | + ( |
| 39 | + "postgresql+asyncpg://localhost/mydb", |
| 40 | + "postgresql://localhost/mydb", |
| 41 | + ), |
| 42 | + ( |
| 43 | + "postgresql+asyncpg://user:pass@localhost:5432/mydb", |
| 44 | + "postgresql://user:pass@localhost:5432/mydb", |
| 45 | + ), |
| 46 | + # PostgreSQL sync drivers (should still strip) |
| 47 | + ( |
| 48 | + "postgresql+psycopg2://localhost/mydb", |
| 49 | + "postgresql://localhost/mydb", |
| 50 | + ), |
| 51 | + # MySQL async drivers |
| 52 | + ( |
| 53 | + "mysql+aiomysql://localhost/mydb", |
| 54 | + "mysql://localhost/mydb", |
| 55 | + ), |
| 56 | + ( |
| 57 | + "mysql+asyncmy://user:pass@localhost:3306/mydb", |
| 58 | + "mysql://user:pass@localhost:3306/mydb", |
| 59 | + ), |
| 60 | + # SQLite async driver |
| 61 | + ( |
| 62 | + "sqlite+aiosqlite:///path/to/db.sqlite", |
| 63 | + "sqlite:///path/to/db.sqlite", |
| 64 | + ), |
| 65 | + ( |
| 66 | + "sqlite+aiosqlite:///:memory:", |
| 67 | + "sqlite:///:memory:", |
| 68 | + ), |
| 69 | + # URLs without driver specification (unchanged) |
| 70 | + ( |
| 71 | + "postgresql://localhost/mydb", |
| 72 | + "postgresql://localhost/mydb", |
| 73 | + ), |
| 74 | + ( |
| 75 | + "mysql://localhost/mydb", |
| 76 | + "mysql://localhost/mydb", |
| 77 | + ), |
| 78 | + ( |
| 79 | + "sqlite:///path/to/db.sqlite", |
| 80 | + "sqlite:///path/to/db.sqlite", |
| 81 | + ), |
| 82 | + # Edge cases |
| 83 | + ( |
| 84 | + "sqlite:///:memory:", |
| 85 | + "sqlite:///:memory:", |
| 86 | + ), |
| 87 | + # Complex URL with query parameters |
| 88 | + ( |
| 89 | + "postgresql+asyncpg://user:pass@host/db?ssl=require", |
| 90 | + "postgresql://user:pass@host/db?ssl=require", |
| 91 | + ), |
| 92 | + ], |
| 93 | + ) |
| 94 | + def test_to_sync_url(self, input_url, expected_url): |
| 95 | + """Test that async driver specifications are correctly removed.""" |
| 96 | + assert _schema_check_utils.to_sync_url(input_url) == expected_url |
| 97 | + |
| 98 | + def test_to_sync_url_no_scheme_separator(self): |
| 99 | + """Test that URLs without :// are returned unchanged.""" |
| 100 | + # This is an invalid URL but the function should handle it gracefully |
| 101 | + assert _schema_check_utils.to_sync_url("not-a-url") == "not-a-url" |
| 102 | + |
| 103 | + def test_to_sync_url_empty_string(self): |
| 104 | + """Test that empty string is returned unchanged.""" |
| 105 | + assert _schema_check_utils.to_sync_url("") == "" |
| 106 | + |
| 107 | + |
30 | 108 | def test_migrate_from_sqlalchemy_pickle(tmp_path): |
31 | 109 | """Tests for migrate_from_sqlalchemy_pickle.""" |
32 | 110 | source_db_path = tmp_path / "source_pickle.db" |
@@ -104,3 +182,66 @@ def test_migrate_from_sqlalchemy_pickle(tmp_path): |
104 | 182 | assert event_res.event_data["actions"]["state_delta"] == {"skey": 4} |
105 | 183 |
|
106 | 184 | dest_session.close() |
| 185 | + |
| 186 | + |
| 187 | +def test_migrate_from_sqlalchemy_pickle_with_async_driver_urls(tmp_path): |
| 188 | + """Tests that migration works with async driver URLs (fixes issue #4176). |
| 189 | +
|
| 190 | + Users often provide async driver URLs (e.g., postgresql+asyncpg://) since |
| 191 | + that's what ADK requires at runtime. The migration tool should handle these |
| 192 | + by automatically converting them to sync URLs. |
| 193 | + """ |
| 194 | + source_db_path = tmp_path / "source_pickle_async.db" |
| 195 | + dest_db_path = tmp_path / "dest_json_async.db" |
| 196 | + # Use async driver URLs like users would typically provide |
| 197 | + source_db_url = f"sqlite+aiosqlite:///{source_db_path}" |
| 198 | + dest_db_url = f"sqlite+aiosqlite:///{dest_db_path}" |
| 199 | + |
| 200 | + # Set up source DB with old pickle schema using sync URL |
| 201 | + sync_source_url = f"sqlite:///{source_db_path}" |
| 202 | + source_engine = create_engine(sync_source_url) |
| 203 | + v0.Base.metadata.create_all(source_engine) |
| 204 | + SourceSession = sessionmaker(bind=source_engine) |
| 205 | + source_session = SourceSession() |
| 206 | + |
| 207 | + # Populate source data |
| 208 | + now = datetime.now(timezone.utc) |
| 209 | + app_state = v0.StorageAppState( |
| 210 | + app_name="async_app", state={"key": "value"}, update_time=now |
| 211 | + ) |
| 212 | + session = v0.StorageSession( |
| 213 | + app_name="async_app", |
| 214 | + user_id="async_user", |
| 215 | + id="async_session", |
| 216 | + state={}, |
| 217 | + create_time=now, |
| 218 | + update_time=now, |
| 219 | + ) |
| 220 | + source_session.add_all([app_state, session]) |
| 221 | + source_session.commit() |
| 222 | + source_session.close() |
| 223 | + |
| 224 | + # This should NOT raise an error about async drivers (the fix for #4176) |
| 225 | + mfsp.migrate(source_db_url, dest_db_url) |
| 226 | + |
| 227 | + # Verify destination DB |
| 228 | + sync_dest_url = f"sqlite:///{dest_db_path}" |
| 229 | + dest_engine = create_engine(sync_dest_url) |
| 230 | + DestSession = sessionmaker(bind=dest_engine) |
| 231 | + dest_session = DestSession() |
| 232 | + |
| 233 | + metadata = dest_session.query(v1.StorageMetadata).first() |
| 234 | + assert metadata is not None |
| 235 | + assert metadata.key == _schema_check_utils.SCHEMA_VERSION_KEY |
| 236 | + assert metadata.value == _schema_check_utils.SCHEMA_VERSION_1_JSON |
| 237 | + |
| 238 | + app_state_res = dest_session.query(v1.StorageAppState).first() |
| 239 | + assert app_state_res is not None |
| 240 | + assert app_state_res.app_name == "async_app" |
| 241 | + assert app_state_res.state == {"key": "value"} |
| 242 | + |
| 243 | + session_res = dest_session.query(v1.StorageSession).first() |
| 244 | + assert session_res is not None |
| 245 | + assert session_res.id == "async_session" |
| 246 | + |
| 247 | + dest_session.close() |
0 commit comments