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 <linkinstar@foxmail.com>
Co-committed-by: LinkinStars <linkinstar@foxmail.com>
This commit is contained in:
LinkinStars 2023-07-14 07:35:35 +00:00 committed by Lunny Xiao
parent 0b18440d73
commit 9b71cb49cc
2 changed files with 5 additions and 5 deletions

View File

@ -360,15 +360,15 @@ func (engine *Engine) NoAutoCondition(no ...bool) *Session {
return session.NoAutoCondition(no...) return session.NoAutoCondition(no...)
} }
func (engine *Engine) loadTableInfo(table *schemas.Table) error { func (engine *Engine) loadTableInfo(ctx context.Context, table *schemas.Table) error {
colSeq, cols, err := engine.dialect.GetColumns(engine.db, engine.defaultContext, table.Name) colSeq, cols, err := engine.dialect.GetColumns(engine.db, ctx, table.Name)
if err != nil { if err != nil {
return err return err
} }
for _, name := range colSeq { for _, name := range colSeq {
table.AddColumn(cols[name]) 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 { if err != nil {
return err return err
} }
@ -404,7 +404,7 @@ func (engine *Engine) DBMetas() ([]*schemas.Table, error) {
} }
for _, table := range tables { for _, table := range tables {
if err = engine.loadTableInfo(table); err != nil { if err = engine.loadTableInfo(engine.defaultContext, table); err != nil {
return nil, err return nil, err
} }
} }

View File

@ -116,7 +116,7 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
} }
// this will modify an old table // 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 return nil, err
} }