avoid SELECT * in jobs store (#30832)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3c9b2511bc
Коммит
f6aeca7e50
@@ -26,10 +26,32 @@ const (
|
|||||||
|
|
||||||
type SqlJobStore struct {
|
type SqlJobStore struct {
|
||||||
*SqlStore
|
*SqlStore
|
||||||
|
|
||||||
|
jobColumns []string
|
||||||
|
jobQuery sq.SelectBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSqlJobStore(sqlStore *SqlStore) store.JobStore {
|
func newSqlJobStore(sqlStore *SqlStore) store.JobStore {
|
||||||
return &SqlJobStore{sqlStore}
|
s := &SqlJobStore{
|
||||||
|
SqlStore: sqlStore,
|
||||||
|
jobColumns: []string{
|
||||||
|
"Id",
|
||||||
|
"Type",
|
||||||
|
"Priority",
|
||||||
|
"CreateAt",
|
||||||
|
"StartAt",
|
||||||
|
"LastActivityAt",
|
||||||
|
"Status",
|
||||||
|
"Progress",
|
||||||
|
"Data",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
s.jobQuery = s.getQueryBuilder().
|
||||||
|
Select(s.jobColumns...).
|
||||||
|
From("Jobs")
|
||||||
|
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) Save(job *model.Job) (*model.Job, error) {
|
func (jss SqlJobStore) Save(job *model.Job) (*model.Job, error) {
|
||||||
@@ -204,9 +226,7 @@ func (jss SqlJobStore) UpdateStatusOptimistically(id string, currentStatus strin
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
getBuilder := jss.getQueryBuilder().
|
getBuilder := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Id": id, "Status": newStatus})
|
Where(sq.Eq{"Id": id, "Status": newStatus})
|
||||||
|
|
||||||
var job model.Job
|
var job model.Job
|
||||||
@@ -231,7 +251,7 @@ func (jss SqlJobStore) UpdateStatusOptimistically(id string, currentStatus strin
|
|||||||
Set("LastActivityAt", lastActivityAndStartTime).
|
Set("LastActivityAt", lastActivityAndStartTime).
|
||||||
Set("Status", newStatus).
|
Set("Status", newStatus).
|
||||||
Where(sq.Eq{"Id": id, "Status": currentStatus}).
|
Where(sq.Eq{"Id": id, "Status": currentStatus}).
|
||||||
Suffix("RETURNING *")
|
Suffix("RETURNING " + strings.Join(jss.jobColumns, ", "))
|
||||||
|
|
||||||
if newStatus == model.JobStatusInProgress {
|
if newStatus == model.JobStatusInProgress {
|
||||||
builder = builder.Set("StartAt", lastActivityAndStartTime)
|
builder = builder.Set("StartAt", lastActivityAndStartTime)
|
||||||
@@ -255,9 +275,7 @@ func (jss SqlJobStore) UpdateStatusOptimistically(id string, currentStatus strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) Get(c request.CTX, id string) (*model.Job, error) {
|
func (jss SqlJobStore) Get(c request.CTX, id string) (*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Id": id}).ToSql()
|
Where(sq.Eq{"Id": id}).ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "job_tosql")
|
return nil, errors.Wrap(err, "job_tosql")
|
||||||
@@ -275,9 +293,7 @@ func (jss SqlJobStore) Get(c request.CTX, id string) (*model.Job, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) GetAllByTypesPage(c request.CTX, jobTypes []string, offset int, limit int) ([]*model.Job, error) {
|
func (jss SqlJobStore) GetAllByTypesPage(c request.CTX, jobTypes []string, offset int, limit int) ([]*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Type": jobTypes}).
|
Where(sq.Eq{"Type": jobTypes}).
|
||||||
OrderBy("CreateAt DESC").
|
OrderBy("CreateAt DESC").
|
||||||
Limit(uint64(limit)).
|
Limit(uint64(limit)).
|
||||||
@@ -295,9 +311,7 @@ func (jss SqlJobStore) GetAllByTypesPage(c request.CTX, jobTypes []string, offse
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) GetAllByType(c request.CTX, jobType string) ([]*model.Job, error) {
|
func (jss SqlJobStore) GetAllByType(c request.CTX, jobType string) ([]*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Type": jobType}).
|
Where(sq.Eq{"Type": jobType}).
|
||||||
OrderBy("CreateAt DESC").ToSql()
|
OrderBy("CreateAt DESC").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -313,9 +327,7 @@ func (jss SqlJobStore) GetAllByType(c request.CTX, jobType string) ([]*model.Job
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) GetAllByTypeAndStatus(c request.CTX, jobType string, status string) ([]*model.Job, error) {
|
func (jss SqlJobStore) GetAllByTypeAndStatus(c request.CTX, jobType string, status string) ([]*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Type": jobType, "Status": status}).
|
Where(sq.Eq{"Type": jobType, "Status": status}).
|
||||||
OrderBy("CreateAt DESC").ToSql()
|
OrderBy("CreateAt DESC").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -331,9 +343,7 @@ func (jss SqlJobStore) GetAllByTypeAndStatus(c request.CTX, jobType string, stat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) GetAllByTypePage(c request.CTX, jobType string, offset int, limit int) ([]*model.Job, error) {
|
func (jss SqlJobStore) GetAllByTypePage(c request.CTX, jobType string, offset int, limit int) ([]*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Type": jobType}).
|
Where(sq.Eq{"Type": jobType}).
|
||||||
OrderBy("CreateAt DESC").
|
OrderBy("CreateAt DESC").
|
||||||
Limit(uint64(limit)).
|
Limit(uint64(limit)).
|
||||||
@@ -352,9 +362,7 @@ func (jss SqlJobStore) GetAllByTypePage(c request.CTX, jobType string, offset in
|
|||||||
|
|
||||||
func (jss SqlJobStore) GetAllByStatus(c request.CTX, status string) ([]*model.Job, error) {
|
func (jss SqlJobStore) GetAllByStatus(c request.CTX, status string) ([]*model.Job, error) {
|
||||||
statuses := []*model.Job{}
|
statuses := []*model.Job{}
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Status": status}).
|
Where(sq.Eq{"Status": status}).
|
||||||
OrderBy("CreateAt ASC").ToSql()
|
OrderBy("CreateAt ASC").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -369,9 +377,7 @@ func (jss SqlJobStore) GetAllByStatus(c request.CTX, status string) ([]*model.Jo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) GetAllByTypeAndStatusPage(c request.CTX, jobType []string, status string, offset int, limit int) ([]*model.Job, error) {
|
func (jss SqlJobStore) GetAllByTypeAndStatusPage(c request.CTX, jobType []string, status string, offset int, limit int) ([]*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Type": jobType, "Status": status}).
|
Where(sq.Eq{"Type": jobType, "Status": status}).
|
||||||
OrderBy("CreateAt DESC").
|
OrderBy("CreateAt DESC").
|
||||||
Limit(uint64(limit)).
|
Limit(uint64(limit)).
|
||||||
@@ -393,9 +399,7 @@ func (jss SqlJobStore) GetNewestJobByStatusAndType(status string, jobType string
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jss SqlJobStore) GetNewestJobByStatusesAndType(status []string, jobType string) (*model.Job, error) {
|
func (jss SqlJobStore) GetNewestJobByStatusesAndType(status []string, jobType string) (*model.Job, error) {
|
||||||
query, args, err := jss.getQueryBuilder().
|
query, args, err := jss.jobQuery.
|
||||||
Select("*").
|
|
||||||
From("Jobs").
|
|
||||||
Where(sq.Eq{"Status": status, "Type": jobType}).
|
Where(sq.Eq{"Status": status, "Type": jobType}).
|
||||||
OrderBy("CreateAt DESC").
|
OrderBy("CreateAt DESC").
|
||||||
Limit(1).ToSql()
|
Limit(1).ToSql()
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user