Skip to content

Commit 302fa42

Browse files
btrepplunny
authored andcommitted
Removes reliance on server specific SQL (go-gitea#393)
Breaks the retrieval of repositories into two queries This fetches the paged ids in one go, then the actual repository information in a second query Some databases do not support SELECT with * when group by is used.
1 parent 88f45ce commit 302fa42

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

models/org.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,29 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
598598
if page <= 0 {
599599
page = 1
600600
}
601+
601602
repos := make([]*Repository, 0, pageSize)
602603

603-
if err := x.Select("`repository`.*").
604+
if err := x.
605+
Select("`repository`.id").
604606
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
605607
Where(cond).
606-
GroupBy("`repository`.id").
608+
GroupBy("`repository`.id,`repository`.updated_unix").
607609
OrderBy("updated_unix DESC").
608610
Limit(pageSize, (page-1)*pageSize).
609611
Find(&repos); err != nil {
612+
return nil, 0, fmt.Errorf("get repository ids: %v", err)
613+
}
614+
615+
repoIDs := make([]int64,pageSize)
616+
for i := range repos {
617+
repoIDs[i] = repos[i].ID
618+
}
619+
620+
if err := x.
621+
Select("`repository`.*").
622+
Where(builder.In("`repository`.id",repoIDs)).
623+
Find(&repos); err!=nil {
610624
return nil, 0, fmt.Errorf("get repositories: %v", err)
611625
}
612626

0 commit comments

Comments
 (0)