[MM-39643] Migrate from gorp to sqlx in store/sqlstore/system_store.go (#18893)
* Migrate from gorp to sqlx in SystemStore * Add BeginXWithIsolation method to *sqlxDBWrapper * Address PR comments
Этот коммит содержится в:
@@ -48,6 +48,15 @@ func (w *sqlxDBWrapper) Beginx() (*sqlxTxWrapper, error) {
|
||||
return newSqlxTxWrapper(tx, w.queryTimeout, w.trace), nil
|
||||
}
|
||||
|
||||
func (w *sqlxDBWrapper) BeginXWithIsolation(opts *sql.TxOptions) (*sqlxTxWrapper, error) {
|
||||
tx, err := w.DB.BeginTxx(context.Background(), opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newSqlxTxWrapper(tx, w.queryTimeout, w.trace), nil
|
||||
}
|
||||
|
||||
func (w *sqlxDBWrapper) Get(dest interface{}, query string, args ...interface{}) error {
|
||||
query = w.DB.Rebind(query)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), w.queryTimeout)
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -39,9 +38,11 @@ func (s SqlSystemStore) createIndexesIfNotExists() {
|
||||
}
|
||||
|
||||
func (s SqlSystemStore) Save(system *model.System) error {
|
||||
if err := s.GetMaster().Insert(system); err != nil {
|
||||
query := "INSERT INTO Systems (Name, Value) VALUES (:Name, :Value)"
|
||||
if _, err := s.GetMasterX().NamedExec(query, system); err != nil {
|
||||
return errors.Wrapf(err, "failed to save system property with name=%s", system.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -62,9 +63,10 @@ func (s SqlSystemStore) SaveOrUpdate(system *model.System) error {
|
||||
return errors.Wrap(err, "system_tosql")
|
||||
}
|
||||
|
||||
if _, err := s.GetMaster().Exec(queryString, args...); err != nil {
|
||||
if _, err := s.GetMasterX().Exec(queryString, args...); err != nil {
|
||||
return errors.Wrap(err, "failed to upsert system property")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -87,18 +89,22 @@ func (s SqlSystemStore) SaveOrUpdateWithWarnMetricHandling(system *model.System)
|
||||
}
|
||||
|
||||
func (s SqlSystemStore) Update(system *model.System) error {
|
||||
if _, err := s.GetMaster().Update(system); err != nil {
|
||||
query := "UPDATE Systems SET Name=:Name, Value=:Value"
|
||||
if _, err := s.GetMasterX().NamedExec(query, system); err != nil {
|
||||
return errors.Wrapf(err, "failed to update system property with name=%s", system.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlSystemStore) Get() (model.StringMap, error) {
|
||||
var systems []model.System
|
||||
systems := []model.System{}
|
||||
props := make(model.StringMap)
|
||||
if _, err := s.GetReplica().Select(&systems, "SELECT * FROM Systems"); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to system properties")
|
||||
|
||||
if err := s.GetReplicaX().Select(&systems, "SELECT * FROM Systems"); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get System list")
|
||||
}
|
||||
|
||||
for _, prop := range systems {
|
||||
props[prop.Name] = prop.Value
|
||||
}
|
||||
@@ -108,7 +114,7 @@ func (s SqlSystemStore) Get() (model.StringMap, error) {
|
||||
|
||||
func (s SqlSystemStore) GetByName(name string) (*model.System, error) {
|
||||
var system model.System
|
||||
if err := s.GetMaster().SelectOne(&system, "SELECT * FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
|
||||
if err := s.GetMasterX().Get(&system, "SELECT * FROM Systems WHERE Name = ?", name); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, store.NewErrNotFound("System", fmt.Sprintf("name=%s", system.Name))
|
||||
}
|
||||
@@ -120,7 +126,7 @@ func (s SqlSystemStore) GetByName(name string) (*model.System, error) {
|
||||
|
||||
func (s SqlSystemStore) PermanentDeleteByName(name string) (*model.System, error) {
|
||||
var system model.System
|
||||
if _, err := s.GetMaster().Exec("DELETE FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
|
||||
if _, err := s.GetMasterX().Exec("DELETE FROM Systems WHERE Name = ?", name); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to permanent delete system property with name=%s", system.Name)
|
||||
}
|
||||
|
||||
@@ -130,18 +136,17 @@ func (s SqlSystemStore) PermanentDeleteByName(name string) (*model.System, error
|
||||
// InsertIfExists inserts a given system value if it does not already exist. If a value
|
||||
// already exists, it returns the old one, else returns the new one.
|
||||
func (s SqlSystemStore) InsertIfExists(system *model.System) (*model.System, error) {
|
||||
tx, err := s.GetMaster().BeginTx(context.Background(), &sql.TxOptions{
|
||||
tx, err := s.GetMasterX().BeginXWithIsolation(&sql.TxOptions{
|
||||
Isolation: sql.LevelSerializable,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "begin_transaction")
|
||||
}
|
||||
defer finalizeTransaction(tx)
|
||||
defer finalizeTransactionX(tx)
|
||||
|
||||
var origSystem model.System
|
||||
if err := tx.SelectOne(&origSystem, `SELECT * FROM Systems
|
||||
WHERE Name = :Name`,
|
||||
map[string]interface{}{"Name": system.Name}); err != nil && err != sql.ErrNoRows {
|
||||
if err := tx.Get(&origSystem, `SELECT * FROM Systems
|
||||
WHERE Name = ?`, system.Name); err != nil && err != sql.ErrNoRows {
|
||||
return nil, errors.Wrapf(err, "failed to get system property with name=%s", system.Name)
|
||||
}
|
||||
|
||||
@@ -151,7 +156,7 @@ func (s SqlSystemStore) InsertIfExists(system *model.System) (*model.System, err
|
||||
}
|
||||
|
||||
// Key does not exist, need to insert.
|
||||
if err := tx.Insert(system); err != nil {
|
||||
if _, err := tx.NamedExec("INSERT INTO Systems (Name, Value) VALUES (:Name, :Value)", system); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to save system property with name=%s", system.Name)
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user