diff --git a/engine/servermaster/server.go b/engine/servermaster/server.go index aa6bddd08ba..cdf2e76b2d5 100644 --- a/engine/servermaster/server.go +++ b/engine/servermaster/server.go @@ -456,7 +456,7 @@ func (s *Server) initElector() error { return errors.Errorf("failed to convert conn to *sql.DB, got %T", conn) } - sqlStorage, err := election.NewSQLStorage(db, leaderElectionTable) + sqlStorage, err := election.NewORMStorageFromSQLDB(db, leaderElectionTable) if err != nil { return err } diff --git a/pkg/election/storage_orm.go b/pkg/election/storage_orm.go index 1b21bc0bef4..fc1f383b670 100644 --- a/pkg/election/storage_orm.go +++ b/pkg/election/storage_orm.go @@ -15,10 +15,12 @@ package election import ( "context" + "database/sql" "database/sql/driver" "encoding/json" "github.com/pingcap/log" + ormUtil "github.com/pingcap/tiflow/engine/pkg/orm" "github.com/pingcap/tiflow/pkg/errors" "gorm.io/gorm" ) @@ -70,6 +72,15 @@ type ORMStorage struct { tableName string } +// NewORMStorageFromSQLDB creates a new ORMStorage from sql.DB. +func NewORMStorageFromSQLDB(backendDB *sql.DB, tableName string) (*ORMStorage, error) { + db, err := ormUtil.NewGormDB(backendDB, "mysql") + if err != nil { + return nil, err + } + return NewORMStorage(db, tableName) +} + // NewORMStorage creates a new ORMStorage. func NewORMStorage(db *gorm.DB, tableName string) (*ORMStorage, error) { TableNameElection = tableName