Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema: improve usability of SchemaByTable #56803

Merged
merged 1 commit into from
Oct 23, 2024
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
9 changes: 8 additions & 1 deletion pkg/infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,14 @@ func SchemaByTable(is InfoSchema, tableInfo *model.TableInfo) (val *model.DBInfo
if tableInfo == nil {
return nil, false
}
return is.SchemaByID(tableInfo.DBID)
if tableInfo.DBID > 0 {
return is.SchemaByID(tableInfo.DBID)
}
tbl, ok := is.TableByID(stdctx.Background(), tableInfo.ID)
if !ok {
return nil, false
}
return is.SchemaByID(tbl.Meta().DBID)
}

func (is *infoSchema) TableByID(_ stdctx.Context, id int64) (val table.Table, ok bool) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ func TestBasic(t *testing.T) {
require.True(t, ok)
require.NotNil(t, schema)

b, err := json.Marshal(tblInfo)
require.NoError(t, err)
tblUnmarshal := &model.TableInfo{}
err = json.Unmarshal(b, tblUnmarshal)
require.NoError(t, err)
schema2, ok := infoschema.SchemaByTable(is, tblUnmarshal)
require.True(t, ok)
require.Equal(t, schema, schema2)

noexistTblInfo := &model.TableInfo{ID: 12345, Name: tblInfo.Name}
schema, ok = infoschema.SchemaByTable(is, noexistTblInfo)
require.False(t, ok)
Expand Down