Skip to content
Open
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
9 changes: 6 additions & 3 deletions contributing/samples/migrate_session_db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ ADK provides a migration script to update the database schema. Run the following

```bash
# Clean up the previous run before executing the migration
cp dnd_sessions.db sessions.db
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" "google.adk.sessions.database_session_service"
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.old
Comment on lines +30 to +36
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 sequence of shell commands can be made more robust and secure.

  1. Security: Piping curl to sh executes remote code without inspection, which is a security risk. It's safer to download the script first, inspect it, and then execute it.
  2. Robustness: The commands are not chained. If one command fails, the subsequent commands will still be executed. For example, if the python migration script fails, rm sessions.db.old will still run, deleting the backup database file which might be needed for debugging.

I suggest chaining the commands with && to ensure that the script will stop if any command fails. This also combines the steps into a single, convenient copy-paste command.

Suggested change
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" "google.adk.sessions.database_session_service"
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.old
cp dnd_sessions.db sessions.db.old && \
curl -fsSL -o db_migration.sh https://raw.githubusercontent.com/google/adk-python/main/scripts/db_migration.sh && \
sh db_migration.sh "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.old db_migration.sh

```

This script uses `alembic` to compare the existing schema against the current model definition and automatically generates and applies the necessary migrations.
Expand All @@ -52,4 +55,4 @@ You should see output indicating that the old session was successfully loaded.

## Limitations

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.
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.
4 changes: 2 additions & 2 deletions contributing/samples/migrate_session_db/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
from google.adk.cli.utils import logs
from google.adk.runners import Runner
from google.adk.sessions.database_session_service import DatabaseSessionService
from google.adk.sessions.sqlite_session_service import SqliteSessionService
from google.adk.sessions.session import Session
from google.genai import types

Expand All @@ -32,7 +32,7 @@
async def main():
app_name = 'migrate_session_db_app'
user_id_1 = 'user1'
session_service = DatabaseSessionService('sqlite:///./sessions.db')
session_service = SqliteSessionService('./sessions.db')
artifact_service = InMemoryArtifactService()
runner = Runner(
app_name=app_name,
Expand Down
Loading