From 281144a6c9f1c8e666413fe10b695e4e320bb21a Mon Sep 17 00:00:00 2001 From: verdverm Date: Tue, 25 Nov 2025 01:36:28 -0800 Subject: [PATCH 1/4] Add NewSessionServiceGorm to use already initialized database connections --- session/database/service.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/session/database/service.go b/session/database/service.go index 56e808ad..a9135024 100644 --- a/session/database/service.go +++ b/session/database/service.go @@ -49,6 +49,19 @@ func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.S return &databaseService{db: db}, nil } +// NewSessionServiceGorm creates a new [session.Service] implementation that uses a +// relational database (e.g., PostgreSQL, Spanner, SQLite) via an already initialized GORM DB. +// This allows reusing the same database across multiple ADK services and your application. +// +// It requires a [gorm.Dialector] to specify the database connection and +// accepts optional [gorm.Option] values for further GORM configuration. +// +// It returns the new [session.Service] or an error if the database connection +// [gorm.Open] fails. +func NewSessionServiceGorm(db *gorm.DB) (session.Service, error) { + return &databaseService{db: db}, nil +} + // AutoMigrate runs the GORM auto-migration tool to ensure the database schema // matches the internal storage models (e.g., storageSession, storageEvent). // From 8cd9f048f8d34d13b0e45f346b3a5c79bde3d42d Mon Sep 17 00:00:00 2001 From: verdverm Date: Tue, 25 Nov 2025 01:40:35 -0800 Subject: [PATCH 2/4] fix comment --- session/database/service.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/session/database/service.go b/session/database/service.go index a9135024..ee744c33 100644 --- a/session/database/service.go +++ b/session/database/service.go @@ -53,11 +53,9 @@ func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.S // relational database (e.g., PostgreSQL, Spanner, SQLite) via an already initialized GORM DB. // This allows reusing the same database across multiple ADK services and your application. // -// It requires a [gorm.Dialector] to specify the database connection and -// accepts optional [gorm.Option] values for further GORM configuration. +// It requires a an initialized [*gorm.DB] to specify the database connection. // -// It returns the new [session.Service] or an error if the database connection -// [gorm.Open] fails. +// It returns the new [session.Service]. Error is always nil, but may be used in the future. func NewSessionServiceGorm(db *gorm.DB) (session.Service, error) { return &databaseService{db: db}, nil } From 333965fe103260dfea3bc15529ac976dceb6880b Mon Sep 17 00:00:00 2001 From: verdverm Date: Wed, 3 Dec 2025 21:08:47 -0800 Subject: [PATCH 3/4] address pr comments --- session/database/service.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/session/database/service.go b/session/database/service.go index ee744c33..896a1cdb 100644 --- a/session/database/service.go +++ b/session/database/service.go @@ -53,10 +53,13 @@ func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.S // relational database (e.g., PostgreSQL, Spanner, SQLite) via an already initialized GORM DB. // This allows reusing the same database across multiple ADK services and your application. // -// It requires a an initialized [*gorm.DB] to specify the database connection. +// It requires an initialized [*gorm.DB] to specify the database connection. // // It returns the new [session.Service]. Error is always nil, but may be used in the future. func NewSessionServiceGorm(db *gorm.DB) (session.Service, error) { + if db == nil { + return nil, fmt.Errorf("session service: gorm.DB cannot be nil") + } return &databaseService{db: db}, nil } From 474c05412241e7af5ca8e468c1a8ca18d2fc636f Mon Sep 17 00:00:00 2001 From: Tony Worm <1390600+verdverm@users.noreply.github.com> Date: Thu, 4 Dec 2025 17:01:14 -0800 Subject: [PATCH 4/4] Update session/database/service.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- session/database/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session/database/service.go b/session/database/service.go index 896a1cdb..212fb36c 100644 --- a/session/database/service.go +++ b/session/database/service.go @@ -55,7 +55,7 @@ func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.S // // It requires an initialized [*gorm.DB] to specify the database connection. // -// It returns the new [session.Service]. Error is always nil, but may be used in the future. +// It returns the new [session.Service] or an error if the provided *gorm.DB is nil. func NewSessionServiceGorm(db *gorm.DB) (session.Service, error) { if db == nil { return nil, fmt.Errorf("session service: gorm.DB cannot be nil")