Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion database/gorm/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (e *Event) ColumnNamesWithDbColumnNames() map[string]string {
}

func (e *Event) Context() context.Context {
return e.query.instance.Statement.Context
return e.query.ctx
}

func (e *Event) DestOfMap() map[string]any {
Expand Down
5 changes: 5 additions & 0 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ func (r *QueryImpl) Select(query any, args ...any) ormcontract.Query {
return r.setConditions(conditions)
}

func (r *QueryImpl) SetContext(ctx context.Context) {
r.ctx = ctx
r.instance.Statement.Context = ctx
}

func (r *QueryImpl) SharedLock() ormcontract.Query {
conditions := r.conditions
conditions.sharedLock = true
Expand Down
20 changes: 16 additions & 4 deletions database/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (r *OrmImpl) Connection(name string) ormcontract.Orm {
}

func (r *OrmImpl) DB() (*sql.DB, error) {
db := r.Query().(*databasegorm.QueryImpl)
query := r.Query().(*databasegorm.QueryImpl)

return db.Instance().DB()
return query.Instance().DB()
}

func (r *OrmImpl) Query() ormcontract.Query {
Expand Down Expand Up @@ -105,7 +105,19 @@ func (r *OrmImpl) Transaction(txFunc func(tx ormcontract.Transaction) error) err
}

func (r *OrmImpl) WithContext(ctx context.Context) ormcontract.Orm {
instance, _ := NewOrmImpl(ctx, r.config, r.connection, r.query)
for _, query := range r.queries {
query := query.(*databasegorm.QueryImpl)
query.SetContext(ctx)
}

query := r.query.(*databasegorm.QueryImpl)
query.SetContext(ctx)

return instance
return &OrmImpl{
ctx: ctx,
config: r.config,
connection: r.connection,
query: query,
queries: r.queries,
}
}
52 changes: 48 additions & 4 deletions database/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var connections = []contractsorm.Driver{
contractsorm.DriverSqlserver,
}

type contextKey int

const testContextKey contextKey = 0

type User struct {
orm.Model
orm.SoftDeletes
Expand Down Expand Up @@ -92,8 +96,9 @@ func TestOrmSuite(t *testing.T) {

func (s *OrmSuite) SetupTest() {
s.orm = &OrmImpl{
ctx: context.Background(),
query: testMysqlQuery,
connection: contractsorm.DriverMysql.String(),
ctx: context.Background(),
query: testMysqlQuery,
queries: map[string]contractsorm.Query{
contractsorm.DriverMysql.String(): testMysqlQuery,
contractsorm.DriverPostgresql.String(): testPostgresqlQuery,
Expand Down Expand Up @@ -194,6 +199,38 @@ func (s *OrmSuite) TestTransactionError() {
}
}

func (s *OrmSuite) TestWithContext() {
s.orm.Observe(User{}, &UserObserver{})
ctx := context.WithValue(context.Background(), testContextKey, "with_context_goravel")
user := User{Name: "with_context_name"}

// Call Query directly
err := s.orm.WithContext(ctx).Query().Create(&user)
s.Nil(err)
s.Equal("with_context_name", user.Name)
s.Equal("with_context_goravel", user.Avatar)

// Call Connection, then call WithContext
for _, connection := range connections {
user.ID = 0
user.Avatar = ""
err := s.orm.Connection(connection.String()).WithContext(ctx).Query().Create(&user)
s.Nil(err)
s.Equal("with_context_name", user.Name)
s.Equal("with_context_goravel", user.Avatar)
}

// Call WithContext, then call Connection
for _, connection := range connections {
user.ID = 0
user.Avatar = ""
err := s.orm.WithContext(ctx).Connection(connection.String()).Query().Create(&user)
s.Nil(err)
s.Equal("with_context_name", user.Name)
s.Equal("with_context_goravel", user.Avatar)
}
}

type UserObserver struct{}

func (u *UserObserver) Retrieved(event contractsorm.Event) error {
Expand All @@ -202,8 +239,15 @@ func (u *UserObserver) Retrieved(event contractsorm.Event) error {

func (u *UserObserver) Creating(event contractsorm.Event) error {
name := event.GetAttribute("name")
if name != nil && name.(string) == "observer_name" {
return errors.New("error")
if name != nil {
if name.(string) == "observer_name" {
return errors.New("error")
}
if name.(string) == "with_context_name" {
if avatar := event.Context().Value(testContextKey); avatar != nil {
event.SetAttribute("avatar", avatar.(string))
}
}
}

return nil
Expand Down