Skip to content
Merged
Changes from all 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
12 changes: 6 additions & 6 deletions go/internal/database/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type Model interface {
Expand Down Expand Up @@ -45,16 +46,15 @@ func get[T Model](db *gorm.DB, clauses ...Clause) (*T, error) {
return &model, nil
}

// TODO: Make this upsert actually idempotent
// save performs an upsert operation (INSERT ON CONFLICT DO UPDATE)
// args:
// - db: the database connection
// - model: the model to save
func save[T Model](db *gorm.DB, model *T) error {
if err := db.Create(model).Error; err != nil {
if err == gorm.ErrDuplicatedKey {
return db.Save(model).Error
}
return fmt.Errorf("failed to create model: %w", err)
if err := db.Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(model).Error; err != nil {
return fmt.Errorf("failed to upsert model: %w", err)
}
return nil
}
Comment on lines 53 to 60
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated save function lacks test coverage to verify the upsert behavior works correctly with both PostgreSQL and SQLite. Given that this change is specifically to ensure compatibility across database backends, consider adding tests that:

  1. Verify the upsert correctly inserts new records
  2. Verify the upsert updates existing records (especially with composite primary keys like Event, Session, Tool, etc.)
  3. Test with both SQLite and PostgreSQL to ensure the behavior is consistent
  4. Verify that CreatedAt and UpdatedAt timestamps are handled correctly on conflict

Copilot uses AI. Check for mistakes.
Expand Down
Loading