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

Feature add has undo log table #245

Merged
merged 10 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions pkg/constant/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ const (
// UndoLogTableName Todo get from config
UndoLogTableName = DefaultTransactionUndoLogTable
DeleteUndoLogSql = DeleteFrom + UndoLogTableName + " WHERE " + UndoLogBranchXid + " = ? AND " + UndoLogXid + " = ?"
// CheckUndoLogTableExistSql check undo log if exist
CheckUndoLogTableExistSql = "SELECT 1 FROM " + UndoLogTableName + " LIMIT 1"
Copy link
Contributor

Choose a reason for hiding this comment

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

sql put with data model

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix done

)

const ErrCodeTableNotExist = "1146"
16 changes: 16 additions & 0 deletions pkg/datasource/sql/undo/base/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func (m *BaseUndoLogManager) DBType() types.DBType {
panic("implement me")
}

// HasUndoLogTable check undo log table if exist
func (m *BaseUndoLogManager) HasUndoLogTable(ctx context.Context, conn *sql.Conn) (res bool, err error) {
_, err = conn.QueryContext(ctx, constant.CheckUndoLogTableExistSql)
// 1146 mysql table not exist fault code
if err != nil && strings.Contains(err.Error(), constant.ErrCodeTableNotExist) {
Copy link
Contributor

Choose a reason for hiding this comment

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

use errors.Is instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

看了一下,data/sql 包里没有定义err对象的类型,所以只能这样判断一下了。

Copy link
Contributor

@luky116 luky116 Sep 25, 2022

Choose a reason for hiding this comment

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

这样写是否好点

 if err != nil {
     if e, ok := err.(*mysql.SQLError);ok &&e.Code == mysql.ErrNoSuchTable {
       return false, nil
    }
    return
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

确实,已更改。

return false, nil
}

if err != nil {
log.Errorf("[HasUndoLogTable] query sql fail, err: %v", err)
return
}

return true, nil
}

// getBatchDeleteUndoLogSql build batch delete undo log
func (m *BaseUndoLogManager) getBatchDeleteUndoLogSql(xid []string, branchID []int64) (string, error) {
if len(xid) == 0 || len(branchID) == 0 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/datasource/sql/undo/mysql/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ func (m *undoLogManager) RunUndo(xid string, branchID int64, conn *sql.Conn) err
func (m *undoLogManager) DBType() types.DBType {
return types.DBTypeMySQL
}

// HasUndoLogTable check undo log table if exist
func (m *undoLogManager) HasUndoLogTable(ctx context.Context, conn *sql.Conn) (bool, error) {
return m.Base.HasUndoLogTable(ctx, conn)
}
2 changes: 2 additions & 0 deletions pkg/datasource/sql/undo/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type UndoLogManager interface {
RunUndo(xid string, branchID int64, conn *sql.Conn) error
// DBType
DBType() types.DBType
// HasUndoLogTable
HasUndoLogTable(ctx context.Context, conn *sql.Conn) (bool, error)
}

// GetUndoLogManager
Expand Down
25 changes: 25 additions & 0 deletions pkg/datasource/sql/undo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,28 @@ func TestDeleteUndoLogs(t *testing.T) {
testDeleteUndoLogs()
})
}

// TestHasUndoLogTable
func TestHasUndoLogTable(t *testing.T) {
// local test can annotation t.SkipNow()
t.SkipNow()

testHasUndoLogTable := func() {
db, err := sql.Open(SeataMySQLDriver, "root:12345678@tcp(127.0.0.1:3306)/seata_order?multiStatements=true")
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe the db operation need to mock?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,可以 mock,后面可以考虑做个优化什么的,现在的写法主要是为了能看出来实际效果才这样写。

assert.Nil(t, err)

ctx := context.Background()
sqlConn, err := db.Conn(ctx)
assert.Nil(t, err)

undoLogManager := new(base.BaseUndoLogManager)

res, err := undoLogManager.HasUndoLogTable(ctx, sqlConn)
assert.Nil(t, err)
assert.True(t, res)
}

t.Run("test_has_undo_log_table", func(t *testing.T) {
testHasUndoLogTable()
})
}