Add upsert helpers

This commit is contained in:
Chris Roberts 2022-09-28 08:36:29 -07:00
parent f9b3d67f6c
commit 247b6f55d0

View File

@ -167,14 +167,6 @@ func stateStoreSchema() *memdb.DBSchema {
return db return db
} }
// Provides db for searching
// NOTE: In most cases this should be used instead of accessing `db`
// directly when searching for values to ensure all associations are
// fully loaded in the results.
func (s *State) search() *gorm.DB {
return s.db.Preload(clause.Associations)
}
// Convert error to a GRPC status error when dealing with lookups // Convert error to a GRPC status error when dealing with lookups
func lookupErrorToStatus( func lookupErrorToStatus(
typeName string, // thing trying to be found (basis, project, etc) typeName string, // thing trying to be found (basis, project, etc)
@ -230,3 +222,31 @@ func errorToStatus(
return status.Error(codes.Internal, err.Error()) return status.Error(codes.Internal, err.Error())
} }
// Provides db for searching
// NOTE: In most cases this should be used instead of accessing `db`
// directly when searching for values to ensure all associations are
// fully loaded in the results.
func (s *State) search() *gorm.DB {
return s.db.Preload(clause.Associations)
}
func (s *State) upsert(arg interface{}) error {
return s.db.Clauses(
clause.OnConflict{
UpdateAll: true,
},
).Save(arg).Error
}
func (s *State) upsertFull(arg interface{}) error {
return s.db.Session(
&gorm.Session{
FullSaveAssociations: true,
},
).Clauses(
clause.OnConflict{
UpdateAll: true,
},
).Save(arg).Error
}