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

Remove ambiguity in GetUserRepositories SQL #393

Merged
merged 1 commit into from
Jan 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Removes reliance on server specific SQL
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
Beau Trepp authored and bt701 committed Jan 9, 2017
commit 624573247081866d3ec6d369117a8ad037641bf1
18 changes: 16 additions & 2 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,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