Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use db.Find instead of writing methods for every object #28084

Merged
merged 32 commits into from Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
72b3ac7
Use db.Find instead of writing methods for every object
lunny Nov 16, 2023
4e5b90d
remove comment code
lunny Nov 16, 2023
e4ab82f
Move FindProjects
lunny Nov 16, 2023
172181c
Fix lint
lunny Nov 16, 2023
42b06ac
Fix bug
lunny Nov 16, 2023
95a8eef
refactor more find/count functions
lunny Nov 17, 2023
926d455
Merge branch 'main' into lunny/use_db_find
lunny Nov 17, 2023
990cfcf
more refactor
lunny Nov 17, 2023
d98c24d
more refactor
lunny Nov 17, 2023
1228312
Fix check
lunny Nov 17, 2023
a5c62d1
Merge branch 'main' into lunny/use_db_find
lunny Nov 17, 2023
b29cb74
Follow @delvh suggestion
lunny Nov 18, 2023
1266b2e
Merge branch 'main' into lunny/use_db_find
lunny Nov 18, 2023
a32c00d
Fix lint
lunny Nov 18, 2023
90a4937
Fix typo
lunny Nov 18, 2023
ed0f18f
Merge branch 'main' into lunny/use_db_find
lunny Nov 18, 2023
108c087
more refactors
lunny Nov 19, 2023
08e41fb
Merge branch 'main' into lunny/use_db_find
lunny Nov 19, 2023
8108df9
refactor for keys
lunny Nov 21, 2023
5572c2b
more refactors
lunny Nov 21, 2023
7d6bb45
Merge branch 'main' into lunny/use_db_find
lunny Nov 21, 2023
b71d0aa
Merge branch 'main' into lunny/use_db_find
lunny Nov 21, 2023
463add4
Fix check
lunny Nov 21, 2023
abac7ac
follow delvh's suggestion
lunny Nov 22, 2023
9f8d57e
Merge branch 'main' into lunny/use_db_find
lunny Nov 22, 2023
7952cd0
Keep all db.Find and db.FindAndCount with pointer of struct and make …
lunny Nov 23, 2023
4281592
Merge branch 'main' into lunny/use_db_find
lunny Nov 23, 2023
395c8aa
Follow @wxiaoguang's suggestion
lunny Nov 23, 2023
28b0b5b
Merge branch 'main' into lunny/use_db_find
lunny Nov 23, 2023
743f9ff
make review easier
lunny Nov 23, 2023
3345e90
make review easier
lunny Nov 23, 2023
c81056b
Merge branch 'main' into lunny/use_db_find
lunny Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/admin_auth.go
Expand Up @@ -9,6 +9,7 @@ import (
"text/tabwriter"

auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
auth_service "code.gitea.io/gitea/services/auth"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -62,7 +63,7 @@ func runListAuth(c *cli.Context) error {
return err
}

authSources, err := auth_model.FindSources(ctx, auth_model.FindSourcesOptions{})
authSources, err := db.Find[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
if err != nil {
return err
}
Expand Down
50 changes: 24 additions & 26 deletions models/actions/artifact.go
Expand Up @@ -14,6 +14,8 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)

// ArtifactStatus is the status of an artifact, uploading, expired or need-delete
Expand Down Expand Up @@ -108,29 +110,37 @@ func UpdateArtifactByID(ctx context.Context, id int64, art *ActionArtifact) erro
return err
}

// ListArtifactsByRunID returns all artifacts of a run
func ListArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) {
arts := make([]*ActionArtifact, 0, 10)
return arts, db.GetEngine(ctx).Where("run_id=?", runID).Find(&arts)
type FindArtifactsOptions struct {
db.ListOptions
RepoID int64
RunID int64
ArtifactName string
Status int
}

// ListArtifactsByRunIDAndArtifactName returns an artifacts of a run by artifact name
func ListArtifactsByRunIDAndArtifactName(ctx context.Context, runID int64, artifactName string) ([]*ActionArtifact, error) {
arts := make([]*ActionArtifact, 0, 10)
return arts, db.GetEngine(ctx).Where("run_id=? AND artifact_name=?", runID, artifactName).Find(&arts)
}
func (opts FindArtifactsOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.RepoID > 0 {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
}
if opts.RunID > 0 {
cond = cond.And(builder.Eq{"run_id": opts.RunID})
}
if opts.ArtifactName != "" {
cond = cond.And(builder.Eq{"artifact_name": opts.ArtifactName})
}
if opts.Status > 0 {
cond = cond.And(builder.Eq{"status": opts.Status})
}

// ListUploadedArtifactsByRunID returns all uploaded artifacts of a run
func ListUploadedArtifactsByRunID(ctx context.Context, runID int64) ([]*ActionArtifact, error) {
arts := make([]*ActionArtifact, 0, 10)
return arts, db.GetEngine(ctx).Where("run_id=? AND status=?", runID, ArtifactStatusUploadConfirmed).Find(&arts)
return cond
}

// ActionArtifactMeta is the meta data of an artifact
type ActionArtifactMeta struct {
ArtifactName string
FileSize int64
Status int64
Status ArtifactStatus
}

// ListUploadedArtifactsMeta returns all uploaded artifacts meta of a run
Expand All @@ -143,18 +153,6 @@ func ListUploadedArtifactsMeta(ctx context.Context, runID int64) ([]*ActionArtif
Find(&arts)
}

// ListArtifactsByRepoID returns all artifacts of a repo
func ListArtifactsByRepoID(ctx context.Context, repoID int64) ([]*ActionArtifact, error) {
arts := make([]*ActionArtifact, 0, 10)
return arts, db.GetEngine(ctx).Where("repo_id=?", repoID).Find(&arts)
}

// ListArtifactsByRunIDAndName returns artifacts by name of a run
func ListArtifactsByRunIDAndName(ctx context.Context, runID int64, name string) ([]*ActionArtifact, error) {
arts := make([]*ActionArtifact, 0, 10)
return arts, db.GetEngine(ctx).Where("run_id=? AND artifact_name=?", runID, name).Find(&arts)
}

// ListNeedExpiredArtifacts returns all need expired artifacts but not deleted
func ListNeedExpiredArtifacts(ctx context.Context) ([]*ActionArtifact, error) {
arts := make([]*ActionArtifact, 0, 10)
Expand Down
4 changes: 2 additions & 2 deletions models/actions/run.go
Expand Up @@ -170,7 +170,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow.
func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string) error {
// Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'.
runs, total, err := FindRuns(ctx, FindRunOptions{
runs, total, err := db.FindAndCount[*ActionRun](ctx, FindRunOptions{
RepoID: repoID,
Ref: ref,
WorkflowID: workflowID,
Expand All @@ -188,7 +188,7 @@ func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string
// Iterate over each found run and cancel its associated jobs.
for _, run := range runs {
// Find all jobs associated with the current run.
jobs, _, err := FindRunJobs(ctx, FindRunJobOptions{
jobs, err := db.Find[*ActionRunJob](ctx, FindRunJobOptions{
RunID: run.ID,
})
if err != nil {
Expand Down
16 changes: 1 addition & 15 deletions models/actions/run_job_list.go
Expand Up @@ -61,7 +61,7 @@ type FindRunJobOptions struct {
UpdatedBefore timeutil.TimeStamp
}

func (opts FindRunJobOptions) toConds() builder.Cond {
func (opts FindRunJobOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.RunID > 0 {
cond = cond.And(builder.Eq{"run_id": opts.RunID})
Expand All @@ -83,17 +83,3 @@ func (opts FindRunJobOptions) toConds() builder.Cond {
}
return cond
}

func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (ActionJobList, int64, error) {
e := db.GetEngine(ctx).Where(opts.toConds())
if opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var tasks ActionJobList
total, err := e.FindAndCount(&tasks)
return tasks, total, err
}

func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRunJob))
}
16 changes: 3 additions & 13 deletions models/actions/run_list.go
Expand Up @@ -75,7 +75,7 @@ type FindRunOptions struct {
Status []Status
}

func (opts FindRunOptions) toConds() builder.Cond {
func (opts FindRunOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.RepoID > 0 {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
Expand All @@ -101,18 +101,8 @@ func (opts FindRunOptions) toConds() builder.Cond {
return cond
}

func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) {
e := db.GetEngine(ctx).Where(opts.toConds())
if opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var runs RunList
total, err := e.Desc("id").FindAndCount(&runs)
return runs, total, err
}

func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRun))
func (opts FindRunOptions) ToOrders() string {
return "`id` DESC"
}

type StatusInfo struct {
Expand Down
23 changes: 3 additions & 20 deletions models/actions/runner.go
Expand Up @@ -156,7 +156,7 @@ type FindRunnerOptions struct {
WithAvailable bool // not only runners belong to, but also runners can be used
}

func (opts FindRunnerOptions) toCond() builder.Cond {
func (opts FindRunnerOptions) ToConds() builder.Cond {
cond := builder.NewCond()

if opts.RepoID > 0 {
Expand All @@ -181,7 +181,7 @@ func (opts FindRunnerOptions) toCond() builder.Cond {
return cond
}

func (opts FindRunnerOptions) toOrder() string {
func (opts FindRunnerOptions) ToOrders() string {
switch opts.Sort {
case "online":
return "last_online DESC"
Expand All @@ -199,22 +199,6 @@ func (opts FindRunnerOptions) toOrder() string {
return "last_online DESC"
}

func CountRunners(ctx context.Context, opts FindRunnerOptions) (int64, error) {
return db.GetEngine(ctx).
Where(opts.toCond()).
Count(ActionRunner{})
}

func FindRunners(ctx context.Context, opts FindRunnerOptions) (runners RunnerList, err error) {
sess := db.GetEngine(ctx).
Where(opts.toCond()).
OrderBy(opts.toOrder())
if opts.Page > 0 {
sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
return runners, sess.Find(&runners)
}

// GetRunnerByUUID returns a runner via uuid
func GetRunnerByUUID(ctx context.Context, uuid string) (*ActionRunner, error) {
var runner ActionRunner
Expand Down Expand Up @@ -263,8 +247,7 @@ func DeleteRunner(ctx context.Context, id int64) error {

// CreateRunner creates new runner.
func CreateRunner(ctx context.Context, t *ActionRunner) error {
_, err := db.GetEngine(ctx).Insert(t)
return err
return db.Insert(ctx, t)
}

func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
Expand Down
16 changes: 3 additions & 13 deletions models/actions/schedule_list.go
Expand Up @@ -67,7 +67,7 @@ type FindScheduleOptions struct {
OwnerID int64
}

func (opts FindScheduleOptions) toConds() builder.Cond {
func (opts FindScheduleOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.RepoID > 0 {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
Expand All @@ -79,16 +79,6 @@ func (opts FindScheduleOptions) toConds() builder.Cond {
return cond
}

func FindSchedules(ctx context.Context, opts FindScheduleOptions) (ScheduleList, int64, error) {
e := db.GetEngine(ctx).Where(opts.toConds())
if !opts.ListAll && opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var schedules ScheduleList
total, err := e.Desc("id").FindAndCount(&schedules)
return schedules, total, err
}

func CountSchedules(ctx context.Context, opts FindScheduleOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionSchedule))
func (opts FindScheduleOptions) ToOrders() string {
return "`id` DESC"
}
19 changes: 7 additions & 12 deletions models/actions/schedule_spec_list.go
Expand Up @@ -71,7 +71,7 @@ type FindSpecOptions struct {
Next int64
}

func (opts FindSpecOptions) toConds() builder.Cond {
func (opts FindSpecOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.RepoID > 0 {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
Expand All @@ -84,23 +84,18 @@ func (opts FindSpecOptions) toConds() builder.Cond {
return cond
}

func (opts FindSpecOptions) ToOrders() string {
return "`id` DESC"
}

func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
e := db.GetEngine(ctx).Where(opts.toConds())
if opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
var specs SpecList
total, err := e.Desc("id").FindAndCount(&specs)
specs, total, err := db.FindAndCount[*ActionScheduleSpec](ctx, opts)
if err != nil {
return nil, 0, err
}

if err := specs.LoadSchedules(ctx); err != nil {
if err := SpecList(specs).LoadSchedules(ctx); err != nil {
return nil, 0, err
}
return specs, total, nil
}

func CountSpecs(ctx context.Context, opts FindSpecOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionScheduleSpec))
}
17 changes: 4 additions & 13 deletions models/actions/task_list.go
Expand Up @@ -62,7 +62,7 @@ type FindTaskOptions struct {
IDOrderDesc bool
}

func (opts FindTaskOptions) toConds() builder.Cond {
func (opts FindTaskOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.RepoID > 0 {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
Expand All @@ -88,18 +88,9 @@ func (opts FindTaskOptions) toConds() builder.Cond {
return cond
}

func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, error) {
e := db.GetEngine(ctx).Where(opts.toConds())
if opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
func (opts FindTaskOptions) ToOrders() string {
if opts.IDOrderDesc {
e.OrderBy("id DESC")
return "`id` DESC"
}
var tasks TaskList
return tasks, e.Find(&tasks)
}

func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionTask))
return ""
}
11 changes: 1 addition & 10 deletions models/actions/variable.go
Expand Up @@ -56,7 +56,7 @@ type FindVariablesOpts struct {
RepoID int64
}

func (opts *FindVariablesOpts) toConds() builder.Cond {
func (opts FindVariablesOpts) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.OwnerID > 0 {
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
Expand All @@ -67,15 +67,6 @@ func (opts *FindVariablesOpts) toConds() builder.Cond {
return cond
}

func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) {
var variables []*ActionVariable
sess := db.GetEngine(ctx)
if opts.PageSize != 0 {
sess = db.SetSessionPagination(sess, &opts.ListOptions)
}
return variables, sess.Where(opts.toConds()).Find(&variables)
}

func GetVariableByID(ctx context.Context, variableID int64) (*ActionVariable, error) {
var variable ActionVariable
has, err := db.GetEngine(ctx).Where("id=?", variableID).Get(&variable)
Expand Down