refactor cluster discovery store to use squirrel (#14560)
Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c2f171af07
Коммит
88e8f56f03
@@ -4,6 +4,7 @@
|
|||||||
package sqlstore
|
package sqlstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
sq "github.com/Masterminds/squirrel"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
@@ -41,22 +42,18 @@ func (s sqlClusterDiscoveryStore) Save(ClusterDiscovery *model.ClusterDiscovery)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) {
|
func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) {
|
||||||
count, err := s.GetMaster().SelectInt(
|
query := s.getQueryBuilder().
|
||||||
`
|
Delete("ClusterDiscovery").
|
||||||
DELETE
|
Where(sq.Eq{"Type": ClusterDiscovery.Type}).
|
||||||
FROM
|
Where(sq.Eq{"ClusterName": ClusterDiscovery.ClusterName}).
|
||||||
ClusterDiscovery
|
Where(sq.Eq{"Hostname": ClusterDiscovery.Hostname})
|
||||||
WHERE
|
|
||||||
Type = :Type
|
queryString, args, err := query.ToSql()
|
||||||
AND ClusterName = :ClusterName
|
if err != nil {
|
||||||
AND Hostname = :Hostname
|
return false, model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
`,
|
}
|
||||||
map[string]interface{}{
|
|
||||||
"Type": ClusterDiscovery.Type,
|
count, err := s.GetMaster().SelectInt(queryString, args...)
|
||||||
"ClusterName": ClusterDiscovery.ClusterName,
|
|
||||||
"Hostname": ClusterDiscovery.Hostname,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql_cluster_discovery.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
return false, model.NewAppError("SqlClusterDiscoveryStore.Delete", "store.sql_cluster_discovery.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
@@ -67,23 +64,19 @@ func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscover
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) {
|
func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) (bool, *model.AppError) {
|
||||||
count, err := s.GetMaster().SelectInt(
|
query := s.getQueryBuilder().
|
||||||
`
|
Select("COUNT(*)").
|
||||||
SELECT
|
From("ClusterDiscovery").
|
||||||
COUNT(*)
|
Where(sq.Eq{"Type": ClusterDiscovery.Type}).
|
||||||
FROM
|
Where(sq.Eq{"ClusterName": ClusterDiscovery.ClusterName}).
|
||||||
ClusterDiscovery
|
Where(sq.Eq{"Hostname": ClusterDiscovery.Hostname})
|
||||||
WHERE
|
|
||||||
Type = :Type
|
queryString, args, err := query.ToSql()
|
||||||
AND ClusterName = :ClusterName
|
if err != nil {
|
||||||
AND Hostname = :Hostname
|
return false, model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
`,
|
}
|
||||||
map[string]interface{}{
|
|
||||||
"Type": ClusterDiscovery.Type,
|
count, err := s.GetMaster().SelectInt(queryString, args...)
|
||||||
"ClusterName": ClusterDiscovery.ClusterName,
|
|
||||||
"Hostname": ClusterDiscovery.Hostname,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql_cluster_discovery.exists.app_error", nil, err.Error(), http.StatusInternalServerError)
|
return false, model.NewAppError("SqlClusterDiscoveryStore.Exists", "store.sql_cluster_discovery.exists.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
@@ -94,67 +87,56 @@ func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscover
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) ([]*model.ClusterDiscovery, *model.AppError) {
|
func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) ([]*model.ClusterDiscovery, *model.AppError) {
|
||||||
lastPingAt := model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS
|
query := s.getQueryBuilder().
|
||||||
|
Select("*").
|
||||||
|
From("ClusterDiscovery").
|
||||||
|
Where(sq.Eq{"Type": ClusterDiscoveryType}).
|
||||||
|
Where(sq.Eq{"ClusterName": clusterName}).
|
||||||
|
Where(sq.Gt{"LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS})
|
||||||
|
|
||||||
|
queryString, args, err := query.ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return nil, model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
var list []*model.ClusterDiscovery
|
var list []*model.ClusterDiscovery
|
||||||
if _, err := s.GetMaster().Select(
|
if _, err := s.GetMaster().Select(&list, queryString, args...); err != nil {
|
||||||
&list,
|
|
||||||
`
|
|
||||||
SELECT
|
|
||||||
*
|
|
||||||
FROM
|
|
||||||
ClusterDiscovery
|
|
||||||
WHERE
|
|
||||||
Type = :ClusterDiscoveryType
|
|
||||||
AND ClusterName = :ClusterName
|
|
||||||
AND LastPingAt > :LastPingAt
|
|
||||||
`,
|
|
||||||
map[string]interface{}{
|
|
||||||
"ClusterDiscoveryType": ClusterDiscoveryType,
|
|
||||||
"ClusterName": clusterName,
|
|
||||||
"LastPingAt": lastPingAt,
|
|
||||||
},
|
|
||||||
); err != nil {
|
|
||||||
return nil, model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
|
return nil, model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) *model.AppError {
|
func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) *model.AppError {
|
||||||
if _, err := s.GetMaster().Exec(
|
query := s.getQueryBuilder().
|
||||||
`
|
Update("ClusterDiscovery").
|
||||||
UPDATE ClusterDiscovery
|
Set("LastPingAt", model.GetMillis()).
|
||||||
SET
|
Where(sq.Eq{"Type": ClusterDiscovery.Type}).
|
||||||
LastPingAt = :LastPingAt
|
Where(sq.Eq{"ClusterName": ClusterDiscovery.ClusterName}).
|
||||||
WHERE
|
Where(sq.Eq{"Hostname": ClusterDiscovery.Hostname})
|
||||||
Type = :Type
|
|
||||||
AND ClusterName = :ClusterName
|
queryString, args, err := query.ToSql()
|
||||||
AND Hostname = :Hostname
|
if err != nil {
|
||||||
`,
|
return model.NewAppError("SqlClusterDiscoveryStore.SetLastPingAt", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
map[string]interface{}{
|
}
|
||||||
"LastPingAt": model.GetMillis(),
|
|
||||||
"Type": ClusterDiscovery.Type,
|
if _, err := s.GetMaster().Exec(queryString, args...); err != nil {
|
||||||
"ClusterName": ClusterDiscovery.ClusterName,
|
return model.NewAppError("SqlClusterDiscoveryStore.SetLastPingAt", "store.sql_cluster_discovery.set_last_ping.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
"Hostname": ClusterDiscovery.Hostname,
|
|
||||||
},
|
|
||||||
); err != nil {
|
|
||||||
return model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "store.sql_cluster_discovery.set_last_ping.app_error", nil, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sqlClusterDiscoveryStore) Cleanup() *model.AppError {
|
func (s sqlClusterDiscoveryStore) Cleanup() *model.AppError {
|
||||||
if _, err := s.GetMaster().Exec(
|
query := s.getQueryBuilder().
|
||||||
`
|
Delete("ClusterDiscovery").
|
||||||
DELETE FROM ClusterDiscovery
|
Where(sq.Lt{"LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS})
|
||||||
WHERE
|
|
||||||
LastPingAt < :LastPingAt
|
queryString, args, err := query.ToSql()
|
||||||
`,
|
if err != nil {
|
||||||
map[string]interface{}{
|
return model.NewAppError("SqlClusterDiscoveryStore.Cleanup", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
"LastPingAt": model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS,
|
}
|
||||||
},
|
|
||||||
); err != nil {
|
if _, err := s.GetMaster().Exec(queryString, args...); err != nil {
|
||||||
return model.NewAppError("SqlClusterDiscoveryStore.Save", "store.sql_cluster_discovery.cleanup.app_error", nil, err.Error(), http.StatusInternalServerError)
|
return model.NewAppError("SqlClusterDiscoveryStore.Cleanup", "store.sql_cluster_discovery.cleanup.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user