From 9b71cb49cca4311cc84bcf2997b4d752bc8bfcea Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Fri, 14 Jul 2023 07:35:35 +0000 Subject: [PATCH] The loadTableInfo function supports passing the context. (#2297) ## `loadTableInfo` add context parameter ### Main change ```diff +++ func (engine *Engine) loadTableInfo(ctx context.Context, table *schemas.Table) error --- func (engine *Engine) loadTableInfo(table *schemas.Table) error ``` ### Situation After #2200, I built custom dialect to control the SQL. I find that everything else is fine, except when the `SYNC` method executes with an exception. The reason is that the `loadTableInfo` method calls the `GetIndexes` and `GetColumns` methods with the dialect during execution. **The context passed to these two methods are all `engine.defaultContext` not the current session's context.** So, I think the `loadTableInfo` method should add the context parameter to ensure that the correct context is used during execution. ### Review Note 1. dialect's `GetColumns` and `GetIndexes` methods are **only** used here, if the context here is incorrect, then the context parameter is invalid. 2. `loadTableInfo` method is only used in `SYNC` and `DBMetas` methods, `SYNC` should pass the session's context, while `DBMetas` has no problem passing `engine.defaultContext`. All in all, I think this change should not affect other function. Reviewed-on: https://gitea.com/xorm/xorm/pulls/2297 Co-authored-by: LinkinStars Co-committed-by: LinkinStars --- engine.go | 8 ++++---- sync.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/engine.go b/engine.go index aa9d8050..0cbfdede 100644 --- a/engine.go +++ b/engine.go @@ -360,15 +360,15 @@ func (engine *Engine) NoAutoCondition(no ...bool) *Session { return session.NoAutoCondition(no...) } -func (engine *Engine) loadTableInfo(table *schemas.Table) error { - colSeq, cols, err := engine.dialect.GetColumns(engine.db, engine.defaultContext, table.Name) +func (engine *Engine) loadTableInfo(ctx context.Context, table *schemas.Table) error { + colSeq, cols, err := engine.dialect.GetColumns(engine.db, ctx, table.Name) if err != nil { return err } for _, name := range colSeq { table.AddColumn(cols[name]) } - indexes, err := engine.dialect.GetIndexes(engine.db, engine.defaultContext, table.Name) + indexes, err := engine.dialect.GetIndexes(engine.db, ctx, table.Name) if err != nil { return err } @@ -404,7 +404,7 @@ func (engine *Engine) DBMetas() ([]*schemas.Table, error) { } for _, table := range tables { - if err = engine.loadTableInfo(table); err != nil { + if err = engine.loadTableInfo(engine.defaultContext, table); err != nil { return nil, err } } diff --git a/sync.go b/sync.go index 11e75404..635a8ba9 100644 --- a/sync.go +++ b/sync.go @@ -116,7 +116,7 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{}) } // this will modify an old table - if err = engine.loadTableInfo(oriTable); err != nil { + if err = engine.loadTableInfo(session.ctx, oriTable); err != nil { return nil, err }