Browse Source

feat: add has undo log table (#245)

feat:add has undo log table func
tags/1.0.2-RC1
wang1309 GitHub 3 years ago
parent
commit
b7a9673821
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 3 deletions
  1. +1
    -1
      go.mod
  2. +8
    -2
      go.sum
  3. +2
    -0
      pkg/constant/undo.go
  4. +18
    -0
      pkg/datasource/sql/undo/base/undo.go
  5. +5
    -0
      pkg/datasource/sql/undo/mysql/undo.go
  6. +2
    -0
      pkg/datasource/sql/undo/undo.go
  7. +25
    -0
      pkg/datasource/sql/undo_test.go

+ 1
- 1
go.mod View File

@@ -7,7 +7,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/agiledragon/gomonkey v2.0.2+incompatible
github.com/apache/dubbo-getty v1.4.8
github.com/arana-db/parser v0.2.4
github.com/arana-db/parser v0.2.5
github.com/dubbogo/gost v1.12.5
github.com/gin-gonic/gin v1.8.0
github.com/go-sql-driver/mysql v1.6.0


+ 8
- 2
go.sum View File

@@ -90,8 +90,11 @@ github.com/apache/dubbo-go-hessian2 v1.11.0 h1:VTdT6NStuEqNmyT3AdSN2DLDBqhXvAAyA
github.com/apache/dubbo-go-hessian2 v1.11.0/go.mod h1:7rEw9guWABQa6Aqb8HeZcsYPHsOS7XT1qtJvkmI6c5w=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/arana-db/parser v0.2.4 h1:V08PFVL8G4e6cjFPYLXwSiGCoKZbwRc7+lryidkWCN0=
github.com/arana-db/parser v0.2.4/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/apache/thrift v0.13.1-0.20201008052519-daf620915714/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/appleboy/gin-jwt/v2 v2.6.3/go.mod h1:MfPYA4ogzvOcVkRwAxT7quHOtQmVKDpTwxyUrC2DNw0=
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
github.com/arana-db/parser v0.2.5 h1:X7SZUjs52nNkX+PL3wrJVd7+BL4VALIXahX+Bx+pmOQ=
github.com/arana-db/parser v0.2.5/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-metrics v0.3.9/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
@@ -1407,6 +1410,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.2.0/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
modernc.org/fileutil v1.0.0/go.mod h1:JHsWpkrk/CnVV1H/eGlFf85BEpfkrp56ro8nojIq9Q8=
modernc.org/golex v1.0.1/go.mod h1:QCA53QtsT1NdGkaZZkF5ezFwk4IXh4BGNafAARTC254=
modernc.org/lex v1.0.0/go.mod h1:G6rxMTy3cH2iA0iXL/HRRv4Znu8MK4higxph/lE7ypk=
@@ -1420,6 +1425,7 @@ modernc.org/sortutil v1.0.0/go.mod h1:1QO0q8IlIlmjBIwm6t/7sof874+xCfZouyqZMLIAtx
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/strutil v1.1.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/y v1.0.1/go.mod h1:Ho86I+LVHEI+LYXoUKlmOMAM1JTXOCfj8qi1T8PsClE=
moul.io/zapgorm2 v1.1.0/go.mod h1:emRfKjNqSzVj5lcgasBdovIXY1jSOwFz2GQZn1Rddks=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=


+ 2
- 0
pkg/constant/undo.go View File

@@ -24,3 +24,5 @@ const (
UndoLogTableName = DefaultTransactionUndoLogTable
DeleteUndoLogSql = DeleteFrom + UndoLogTableName + " WHERE " + UndoLogBranchXid + " = ? AND " + UndoLogXid + " = ?"
)

const ErrCodeTableNotExist = "1146"

+ 18
- 0
pkg/datasource/sql/undo/base/undo.go View File

@@ -21,6 +21,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"github.com/arana-db/parser/mysql"
"strings"

"github.com/pkg/errors"
@@ -36,6 +37,9 @@ var _ undo.UndoLogManager = (*BaseUndoLogManager)(nil)

var ErrorDeleteUndoLogParamsFault = errors.New("xid or branch_id can't nil")

// CheckUndoLogTableExistSql check undo log if exist
const CheckUndoLogTableExistSql = "SELECT 1 FROM " + constant.UndoLogTableName + " LIMIT 1"

// BaseUndoLogManager
type BaseUndoLogManager struct{}

@@ -112,6 +116,20 @@ 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) {
if _, err = conn.QueryContext(ctx, CheckUndoLogTableExistSql); err != nil {
// 1146 mysql table not exist fault code
if e, ok := err.(*mysql.SQLError); ok && e.Code == mysql.ErrNoSuchTable {
return false, 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 {


+ 5
- 0
pkg/datasource/sql/undo/mysql/undo.go View File

@@ -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
- 0
pkg/datasource/sql/undo/undo.go View File

@@ -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


+ 25
- 0
pkg/datasource/sql/undo_test.go View File

@@ -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")
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()
})
}

Loading…
Cancel
Save