Skip to content

Commit

Permalink
Removes reliance on server specific SQL (#393)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
btrepp authored and lunny committed Jan 14, 2017
1 parent 88f45ce commit 302fa42
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,29 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
if page <= 0 {
page = 1
}

repos := make([]*Repository, 0, pageSize)

if err := x.Select("`repository`.*").
if err := x.
Select("`repository`.id").
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where(cond).
GroupBy("`repository`.id").
GroupBy("`repository`.id,`repository`.updated_unix").
OrderBy("updated_unix DESC").
Limit(pageSize, (page-1)*pageSize).
Find(&repos); err != nil {
return nil, 0, fmt.Errorf("get repository ids: %v", err)
}

repoIDs := make([]int64,pageSize)
for i := range repos {
repoIDs[i] = repos[i].ID
}

if err := x.
Select("`repository`.*").
Where(builder.In("`repository`.id",repoIDs)).
Find(&repos); err!=nil {
return nil, 0, fmt.Errorf("get repositories: %v", err)
}

Expand Down

0 comments on commit 302fa42

Please sign in to comment.