This example demonstrates how to upgrade a session database created with an older version of ADK to be compatible with the current version.
This sample includes dnd_sessions.db, a database created with ADK v1.15.0. The following steps show how to run into a schema error and then resolve it using the migration script.
First, copy the old database to sessions.db, which is the file the sample application expects.
cp dnd_sessions.db sessions.db
python main.pyRunning the application against the old database will fail with a schema mismatch error, as the events table is missing a column required by newer ADK versions:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: events.usage_metadata
ADK provides a migration script to update the database schema. Run the following command to download and execute it.
# Clean up the previous run before executing the migration
cp dnd_sessions.db sessions.db.old
# Download and run the migration script
curl -fsSL https://raw.githubusercontent.com/google/adk-python/main/scripts/db_migration.sh | sh -s -- "sqlite:///%(here)s/sessions.db.old" "google.adk.sessions.database_session_service"
python -m google.adk.sessions.migrate_from_sqlalchemy_sqlite --source_db_path ./sessions.db.old --dest_db_path ./sessions.db
rm sessions.db.oldThis script uses alembic to compare the existing schema against the current model definition and automatically generates and applies the necessary migrations.
Note on generated files:
- The script will create an
alembic.inifile and analembic/directory. You must delete these before re-running the script. - The
sample-outputdirectory in this example contains a reference of the generated files for your inspection. - The
%(here)svariable in the database URL is analembicplaceholder that refers to the current directory.
With the database schema updated, the application can now load the session correctly.
python main.pyYou should see output indicating that the old session was successfully loaded.
The migration script is designed to add new columns that have been introduced in newer ADK versions. It does not handle more complex schema changes, such as modifying a column's data type (e.g., from int to string) or altering the internal structure of stored data.