mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* Remove QueryRowx (no-timeout) from sqlxDBWrapper; migrate caller to QueryRowX QueryRowx forwarded to context.Background() with no timeout, while QueryRowX (uppercase) already enforces the wrapper timeout. Removing the no-timeout variant eliminates an accidental footgun and migrates the one sqlstore caller (plugin_store) to the timeout-enforcing method. * sqlxRow, with timeout cancel after Scan Introduce sqlxRow, which pairs *sqlx.Row with its context cancel function. QueryRowX (on both sqlxDBWrapper and sqlxTxWrapper) now returns *sqlxRow; Scan calls cancel immediately after the row is consumed, releasing the timeout context as soon as possible rather than waiting for the timer. * sqlxRows, with timeout cancel on Close Introduce sqlxRows, which embeds *sqlx.Rows and holds the timeout context's cancel function. Close() cancels the context immediately after the rows are done, releasing timeout resources as soon as iteration completes rather than waiting for the timer to fire. Introduce rowScanner interface (Next/Scan/Err) so the internal helpers scanRowsIntoMap and scanRetentionIdsForDeletion accept any row iterator rather than the concrete *sql.Rows, accommodating the new return types without threading *sqlxRows through every caller. * abolish the X suffix altogether * delete unused NamedQuery * add timeout tests for Query and QueryRow on db and tx wrappers * fix tx/timeout tests: handle pq driver.ErrBadConn on killed connection * fixup! fix tx/timeout tests: handle pq driver.ErrBadConn on killed connection * override Next() on sqlxRows to cancel on EOF * rm redundant row.Err(), handled by Scan * apply timeout to context unless deadline set * rebind consistently * address review feedback: rename, trace, and QueryContext fix - Rename withQueryTimeout → ensureQueryTimeout to better convey that it respects existing deadlines rather than overriding them. - Add missing w.trace blocks to QueryRowContext and ExecContext. - Change QueryContext to use ensureQueryTimeout and return *sqlxRows (instead of *sql.Rows) so the cancel is deferred to Close/Next rather than released prematurely. * fix Beginx → Begin after master merge * fix golangci-lint inline warning in sqlx_wrapper_test --------- Co-authored-by: Mattermost Build <[email protected]>
146 lines
4.5 KiB
Go
146 lines
4.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
sq "github.com/mattermost/squirrel"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/request"
|
|
"github.com/mattermost/mattermost/server/v8/channels/store"
|
|
)
|
|
|
|
type SqlSystemStore struct {
|
|
*SqlStore
|
|
|
|
systemSelectQuery sq.SelectBuilder
|
|
}
|
|
|
|
func newSqlSystemStore(sqlStore *SqlStore) store.SystemStore {
|
|
s := SqlSystemStore{SqlStore: sqlStore}
|
|
s.systemSelectQuery = s.getQueryBuilder().Select("Name", "Value").From("Systems")
|
|
return &s
|
|
}
|
|
|
|
func (s SqlSystemStore) Save(system *model.System) error {
|
|
query := "INSERT INTO Systems (Name, Value) VALUES (:Name, :Value)"
|
|
if _, err := s.GetMaster().NamedExec(query, system); err != nil {
|
|
return errors.Wrapf(err, "failed to save system property with name=%s", system.Name)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s SqlSystemStore) SaveOrUpdate(system *model.System) error {
|
|
query := s.getQueryBuilder().
|
|
Insert("Systems").
|
|
Columns("Name", "Value").
|
|
Values(system.Name, system.Value)
|
|
|
|
query = query.SuffixExpr(sq.Expr("ON CONFLICT (name) DO UPDATE SET Value = ?", system.Value))
|
|
|
|
queryString, args, err := query.ToSql()
|
|
if err != nil {
|
|
return errors.Wrap(err, "system_tosql")
|
|
}
|
|
|
|
if _, err := s.GetMaster().Exec(queryString, args...); err != nil {
|
|
return errors.Wrap(err, "failed to upsert system property")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s SqlSystemStore) Update(system *model.System) error {
|
|
query := "UPDATE Systems SET Value=:Value WHERE Name=:Name"
|
|
if _, err := s.GetMaster().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) {
|
|
return s.GetWithContext(request.EmptyContext(s.logger))
|
|
}
|
|
|
|
func (s SqlSystemStore) GetWithContext(rctx request.CTX) (model.StringMap, error) {
|
|
systems := []model.System{}
|
|
props := make(model.StringMap)
|
|
|
|
query := s.systemSelectQuery
|
|
if err := s.DBXFromContext(rctx.Context()).SelectBuilder(&systems, query); err != nil {
|
|
return nil, errors.Wrap(err, "failed to get System list")
|
|
}
|
|
|
|
for _, prop := range systems {
|
|
props[prop.Name] = prop.Value
|
|
}
|
|
|
|
return props, nil
|
|
}
|
|
|
|
func (s SqlSystemStore) GetByName(name string) (*model.System, error) {
|
|
return s.GetByNameWithContext(store.RequestContextWithMaster(request.EmptyContext(s.logger)), name)
|
|
}
|
|
|
|
func (s SqlSystemStore) GetByNameWithContext(rctx request.CTX, name string) (*model.System, error) {
|
|
var system model.System
|
|
query := s.systemSelectQuery.Where(sq.Eq{"Name": name})
|
|
if err := s.DBXFromContext(rctx.Context()).GetBuilder(&system, query); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, store.NewErrNotFound("System", fmt.Sprintf("name=%s", system.Name))
|
|
}
|
|
return nil, errors.Wrapf(err, "failed to get system property with name=%s", system.Name)
|
|
}
|
|
|
|
return &system, nil
|
|
}
|
|
|
|
func (s SqlSystemStore) PermanentDeleteByName(name string) (*model.System, error) {
|
|
var system model.System
|
|
if _, err := s.GetMaster().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)
|
|
}
|
|
|
|
return &system, nil
|
|
}
|
|
|
|
// 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, err error) {
|
|
tx, err := s.GetMaster().BeginWithIsolation(&sql.TxOptions{
|
|
Isolation: sql.LevelSerializable,
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "begin_transaction")
|
|
}
|
|
defer finalizeTransactionX(tx, &err)
|
|
|
|
var origSystem model.System
|
|
query := s.systemSelectQuery.Where(sq.Eq{"Name": system.Name})
|
|
if err := tx.GetBuilder(&origSystem, query); err != nil && err != sql.ErrNoRows {
|
|
return nil, errors.Wrapf(err, "failed to get system property with name=%s", system.Name)
|
|
}
|
|
|
|
if origSystem.Value != "" {
|
|
// Already a value exists, return that.
|
|
return &origSystem, nil
|
|
}
|
|
|
|
// Key does not exist, need to insert.
|
|
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)
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, errors.Wrap(err, "commit_transaction")
|
|
}
|
|
return system, nil
|
|
}
|