mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:07:30 +00:00
feat(storage): read only transactions (#6417)
feat(storage): read only transactions for queries (#6415) * fix: tests * bastle wie en grosse * fix(database): scan as callback * fix tests * fix merge failures * remove as of system time * refactor: remove unused test * refacotr: remove unused lines
This commit is contained in:
@@ -175,48 +175,58 @@ func (db *dbMock) expectRollback(err error) *dbMock {
|
||||
|
||||
func (db *dbMock) expectGetByID(table, key, value string) *dbMock {
|
||||
query := fmt.Sprintf(expectedGetByID, table, key)
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnRows(sqlmock.NewRows([]string{key}).
|
||||
AddRow(key))
|
||||
db.mock.ExpectCommit()
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *dbMock) expectGetByIDErr(table, key, value string, err error) *dbMock {
|
||||
query := fmt.Sprintf(expectedGetByID, table, key)
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnError(err)
|
||||
db.mock.ExpectCommit()
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *dbMock) expectGetByQuery(table, key, method, value string) *dbMock {
|
||||
query := fmt.Sprintf(expectedGetByQuery, table, key, method)
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnRows(sqlmock.NewRows([]string{key}).
|
||||
AddRow(key))
|
||||
db.mock.ExpectCommit()
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *dbMock) expectGetByQueryCaseSensitive(table, key, method, value string) *dbMock {
|
||||
query := fmt.Sprintf(expectedGetByQueryCaseSensitive, table, key, method)
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnRows(sqlmock.NewRows([]string{key}).
|
||||
AddRow(key))
|
||||
db.mock.ExpectCommit()
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *dbMock) expectGetByQueryErr(table, key, method, value string, err error) *dbMock {
|
||||
query := fmt.Sprintf(expectedGetByQuery, table, key, method)
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnError(err)
|
||||
db.mock.ExpectCommit()
|
||||
|
||||
return db
|
||||
}
|
||||
@@ -313,10 +323,14 @@ func (db *dbMock) expectGetSearchRequestNoParams(table string, resultAmount, tot
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WillReturnRows(rows)
|
||||
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -329,10 +343,12 @@ func (db *dbMock) expectGetSearchRequestWithLimit(table string, limit, resultAmo
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WillReturnRows(rows)
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -345,10 +361,12 @@ func (db *dbMock) expectGetSearchRequestWithOffset(table string, offset, resultA
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WillReturnRows(rows)
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -361,10 +379,12 @@ func (db *dbMock) expectGetSearchRequestWithSorting(table, sorting string, sorti
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WillReturnRows(rows)
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -377,12 +397,14 @@ func (db *dbMock) expectGetSearchRequestWithSearchQuery(table, key, method, valu
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WithArgs(value).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnRows(rows)
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -395,12 +417,14 @@ func (db *dbMock) expectGetSearchRequestWithAllParams(table, key, method, value,
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WithArgs(value).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WithArgs(value).
|
||||
WillReturnRows(rows)
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -413,9 +437,11 @@ func (db *dbMock) expectGetSearchRequestErr(table string, resultAmount, total in
|
||||
rows.AddRow(fmt.Sprintf("hodor-%d", i))
|
||||
}
|
||||
|
||||
db.mock.ExpectBegin()
|
||||
db.mock.ExpectQuery(queryCount).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(total))
|
||||
db.mock.ExpectQuery(query).
|
||||
WillReturnError(err)
|
||||
db.mock.ExpectCommit()
|
||||
return db
|
||||
}
|
||||
|
@@ -1,10 +1,14 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
||||
@@ -48,6 +52,13 @@ func PrepareSearchQuery(table string, request SearchRequest) func(db *gorm.DB, r
|
||||
}
|
||||
}
|
||||
|
||||
query = query.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true})
|
||||
defer func() {
|
||||
if err := query.Commit().Error; err != nil {
|
||||
logging.OnError(err).Info("commit failed")
|
||||
}
|
||||
}()
|
||||
|
||||
query = query.Count(&count)
|
||||
if res == nil {
|
||||
return count, nil
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -13,7 +15,14 @@ import (
|
||||
|
||||
func PrepareGetByKey(table string, key ColumnKey, id string) func(db *gorm.DB, res interface{}) error {
|
||||
return func(db *gorm.DB, res interface{}) error {
|
||||
err := db.Table(table).
|
||||
tx := db.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true})
|
||||
defer func() {
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
logging.OnError(err).Info("commit failed")
|
||||
}
|
||||
}()
|
||||
|
||||
err := tx.Table(table).
|
||||
Where(fmt.Sprintf("%s = ?", key.ToColumnName()), id).
|
||||
Take(res).
|
||||
Error
|
||||
@@ -39,7 +48,14 @@ func PrepareGetByQuery(table string, queries ...SearchQuery) func(db *gorm.DB, r
|
||||
}
|
||||
}
|
||||
|
||||
err := query.Take(res).Error
|
||||
tx := query.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true})
|
||||
defer func() {
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
logging.OnError(err).Info("commit failed")
|
||||
}
|
||||
}()
|
||||
|
||||
err := tx.Take(res).Error
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user