Skip to content

Commit

Permalink
bugfix: subject system groups sql for mysql 8.0 (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 authored Jul 17, 2023
1 parent 25468fe commit c9a55ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
25 changes: 13 additions & 12 deletions pkg/database/dao/subject_system_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package dao
import (
"database/sql"
"errors"
"fmt"

"github.com/jmoiron/sqlx"

Expand Down Expand Up @@ -97,11 +98,11 @@ func (m *subjectSystemGroupManager) selectGroups(
systemID string,
subjectPKs []int64,
) error {
query := `SELECT
query := fmt.Sprintf(`SELECT
subject_pk,
groups
%s
FROM subject_system_group
WHERE system_id = ? AND subject_pk IN (?)`
WHERE system_id = ? AND subject_pk IN (?)`, "`groups`")
return database.SqlxSelect(m.DB, groups, query, systemID, subjectPKs)
}

Expand All @@ -110,37 +111,37 @@ func (m *subjectSystemGroupManager) selectBySystemSubject(
systemID string,
subjectPK int64,
) error {
query := `SELECT
query := fmt.Sprintf(`SELECT
pk,
system_id,
subject_pk,
groups,
%s,
reversion
FROM subject_system_group
WHERE system_id = ? AND subject_pk = ?`
WHERE system_id = ? AND subject_pk = ?`, "`groups`")
return database.SqlxGet(m.DB, subjectSystemGroup, query, systemID, subjectPK)
}

func (m *subjectSystemGroupManager) insertWithTx(tx *sqlx.Tx, subjectSystemGroup *SubjectSystemGroup) error {
sql := `INSERT INTO subject_system_group (
sql := fmt.Sprintf(`INSERT INTO subject_system_group (
system_id,
subject_pk,
groups
%s
) VALUES (
:system_id,
:subject_pk,
:groups
)`
)`, "`groups`")
return database.SqlxInsertWithTx(tx, sql, subjectSystemGroup)
}

func (m *subjectSystemGroupManager) updateWithTx(tx *sqlx.Tx, subjectSystemGroup *SubjectSystemGroup) (int64, error) {
sql := `UPDATE subject_system_group SET
groups = :groups,
sql := fmt.Sprintf(`UPDATE subject_system_group SET
%s = :groups,
reversion = reversion + 1
WHERE system_id = :system_id
AND subject_pk = :subject_pk
AND reversion = :reversion`
AND reversion = :reversion`, "`groups`")
return database.SqlxUpdateWithTx(tx, sql, subjectSystemGroup)
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/database/dao/subject_system_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package dao

import (
"fmt"
"testing"

sqlmock "github.com/DATA-DOG/go-sqlmock"
Expand All @@ -22,11 +23,11 @@ import (

func Test_subjectSystemGroupManager_ListGroups(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT
mockQuery := fmt.Sprintf(`^SELECT
subject_pk,
groups
%s
FROM subject_system_group
WHERE system_id = (.*) AND subject_pk IN (.*)`
WHERE system_id = (.*) AND subject_pk IN (.*)`, "`groups`")
mockRows := sqlmock.NewRows([]string{"subject_pk", "groups"}).AddRow(int64(1), "test")
mock.ExpectQuery(mockQuery).WithArgs("system", int64(1)).WillReturnRows(mockRows)

Expand Down Expand Up @@ -58,14 +59,14 @@ func Test_subjectSystemGroupManager_DeleteBySystemSubjectWithTx(t *testing.T) {

func Test_subjectSystemGroupManager_GetBySystemSubject(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT
mockQuery := fmt.Sprintf(`^SELECT
pk,
system_id,
subject_pk,
groups,
%s,
reversion
FROM subject_system_group
WHERE system_id = (.*) AND subject_pk = (.*)`
WHERE system_id = (.*) AND subject_pk = (.*)`, "`groups`")
mockRows := sqlmock.NewRows([]string{"system_id", "subject_pk", "groups", "reversion"}).
AddRow("test", int64(1), "[]", int64(2))
mock.ExpectQuery(mockQuery).WithArgs("system", int64(1)).WillReturnRows(mockRows)
Expand Down Expand Up @@ -108,7 +109,7 @@ func Test_subjectSystemGroupManager_CreateWithTx(t *testing.T) {
func Test_subjectSystemGroupManager_UpdateWithTx(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mock.ExpectBegin()
mock.ExpectExec(`^UPDATE subject_system_group SET groups = (.*)`).WithArgs(
mock.ExpectExec(fmt.Sprintf(`^UPDATE subject_system_group SET %s = (.*)`, "`groups`")).WithArgs(
"[]", "system", int64(1), int64(2),
).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
Expand Down

0 comments on commit c9a55ea

Please sign in to comment.