Skip to content
Open
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 dm/master/openapi_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (s *Server) DMAPIGetSourceTableList(c *gin.Context, sourceName string, sche
return
}
defer baseDB.Close()
tableList, err := dbutil.GetTables(c.Request.Context(), baseDB.DB, schemaName)
tableList, err := conn.GetTables(c.Request.Context(), baseDB.DB, schemaName)
if err != nil {
_ = c.Error(err)
return
Expand Down
4 changes: 2 additions & 2 deletions dm/pkg/checker/onlineddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"context"
"database/sql"

"github.com/pingcap/tidb/pkg/util/dbutil"
"github.com/pingcap/tidb/pkg/util/filter"
"github.com/pingcap/tiflow/dm/pkg/conn"
onlineddl "github.com/pingcap/tiflow/dm/syncer/online-ddl-tools"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func (c *OnlineDDLChecker) Check(ctx context.Context) *Result {
}

for schema := range c.checkSchemas {
tableList, err := dbutil.GetTables(ctx, c.db, schema)
tableList, err := conn.GetTables(ctx, c.db, schema)
if err != nil {
markCheckError(r, err)
return r
Expand Down
46 changes: 45 additions & 1 deletion dm/pkg/conn/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func FetchAllDoTables(ctx context.Context, db *BaseDB, bw *filter.Filter) (map[s
schemaToTables := make(map[string][]string)
for _, ftSchema := range ftSchemas {
schema := ftSchema.Schema
tables, err := dbutil.GetTables(ctx, db.DB, schema)
tables, err := GetTables(ctx, db.DB, schema)
if err != nil {
return nil, terror.DBErrorAdapt(err, db.Scope, terror.ErrDBDriverError)
}
Expand Down Expand Up @@ -555,3 +555,47 @@ func FetchTargetDoTables(

return tableMapper, extendedColumnPerTable, nil
}

// GetTables returns all table names in the schema.
// It supports both standard MySQL (2 columns) and Ali RDS (4 columns).
func GetTables(ctx context.Context, db *sql.DB, schema string) ([]string, error) {
query := fmt.Sprintf("SHOW FULL TABLES IN `%s`", strings.ReplaceAll(schema, "`", "``"))
rows, err := db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()

cols, err := rows.Columns()
if err != nil {
return nil, err
}

if len(cols) < 2 {
return nil, errors.New("SHOW FULL TABLES returned less than 2 columns")
}
Comment on lines +574 to +576

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the error handling in this package, it's better to return a terror error instead of a raw error created with errors.New. You can use terror.ErrDBUnExpect.Generate for this.

Suggested change
if len(cols) < 2 {
return nil, errors.New("SHOW FULL TABLES returned less than 2 columns")
}
if len(cols) < 2 {
return nil, terror.ErrDBUnExpect.Generate("SHOW FULL TABLES returned less than 2 columns")
}


var tableName, tableType string
scanArgs := make([]interface{}, len(cols))
scanArgs[0] = &tableName
scanArgs[1] = &tableType
for i := 2; i < len(cols); i++ {
var dummy interface{}
scanArgs[i] = &dummy
Comment on lines +583 to +584

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This loop for creating dummy scan arguments can be made more efficient and idiomatic. Instead of declaring a new dummy variable in each iteration, you can allocate space for the columns you want to ignore directly. Using sql.RawBytes is a good practice for this as it can avoid extra allocations.

Suggested change
var dummy interface{}
scanArgs[i] = &dummy
scanArgs[i] = new(sql.RawBytes)

}

var tables []string
for rows.Next() {
err = rows.Scan(scanArgs...)
if err != nil {
return nil, err
}
if tableType == "BASE TABLE" {
tables = append(tables, tableName)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return tables, nil
}