From c9a55ea2e9bcf01efc2ada40163a4b1a44ee661b Mon Sep 17 00:00:00 2001 From: Timmy Date: Mon, 17 Jul 2023 14:58:27 +0800 Subject: [PATCH] bugfix: subject system groups sql for mysql 8.0 (#258) --- pkg/database/dao/subject_system_group.go | 25 ++++++++++--------- pkg/database/dao/subject_system_group_test.go | 15 +++++------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pkg/database/dao/subject_system_group.go b/pkg/database/dao/subject_system_group.go index e7578ec3..1b5bc13c 100644 --- a/pkg/database/dao/subject_system_group.go +++ b/pkg/database/dao/subject_system_group.go @@ -13,6 +13,7 @@ package dao import ( "database/sql" "errors" + "fmt" "github.com/jmoiron/sqlx" @@ -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) } @@ -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) } diff --git a/pkg/database/dao/subject_system_group_test.go b/pkg/database/dao/subject_system_group_test.go index 70542d25..0f4bd79f 100644 --- a/pkg/database/dao/subject_system_group_test.go +++ b/pkg/database/dao/subject_system_group_test.go @@ -11,6 +11,7 @@ package dao import ( + "fmt" "testing" sqlmock "github.com/DATA-DOG/go-sqlmock" @@ -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) @@ -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) @@ -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()