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

fix buildLockKey failed #497

Merged
merged 2 commits into from
Mar 13, 2023
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
5 changes: 2 additions & 3 deletions pkg/datasource/sql/exec/at/select_for_update_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func TestBuildSelectPKSQL(t *testing.T) {

selSQL, err := e.buildSelectPKSQL(ctx.SelectStmt, &metaData)
assert.Nil(t, err)
equal := "SELECT SQL_NO_CACHE order_id,id FROM t_user WHERE age>? FOR UPDATE" == selSQL ||
"SELECT SQL_NO_CACHE id,order_id FROM t_user WHERE age>? FOR UPDATE" == selSQL
assert.Equal(t, equal, true)
assert.Equal(t, "SELECT SQL_NO_CACHE id,order_id FROM t_user WHERE age>? FOR UPDATE" == selSQL, true)
}

func TestBuildLockKey(t *testing.T) {
Expand Down Expand Up @@ -123,6 +121,7 @@ func TestBuildLockKey(t *testing.T) {
ColumnName: "age",
},
},
ColumnNames: []string{"id", "order_id", "age"},
}
rows := mockRows{}
lockKey := e.buildLockKey(rows, &metaData)
Expand Down
10 changes: 10 additions & 0 deletions pkg/datasource/sql/types/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package types
import (
"fmt"
"reflect"
"sort"
)

// ColumnMeta
Expand Down Expand Up @@ -119,6 +120,15 @@ func (m TableMeta) GetPrimaryKeyOnlyName() []string {
}
}
}

// need sort again according the m.ColumnNames
order := make(map[string]int, len(m.ColumnNames))
for i, name := range m.ColumnNames {
order[name] = i
}
sort.Slice(keys, func(i, j int) bool {
return order[keys[i]] < order[keys[j]]
})
return keys
}

Expand Down