Skip to content

Commit

Permalink
Merge pull request #33 from src-d/db_context
Browse files Browse the repository at this point in the history
Add context to db queries
  • Loading branch information
smacker authored Oct 14, 2019
2 parents 556ea44 + bd41ab1 commit 7d14c5c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 83 deletions.
12 changes: 7 additions & 5 deletions examples/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ func (c *Ghsync) Execute(args []string) error {
type bodyFunc = func(logger log.Logger, httpClient *http.Client, downloader *github.Downloader) error

func (c *DownloaderCmd) ExecuteBody(logger log.Logger, fn bodyFunc) error {
client := oauth2.NewClient(context.TODO(), oauth2.StaticTokenSource(
ctx := context.Background()

client := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.Token},
))

Expand Down Expand Up @@ -149,7 +151,7 @@ func (c *DownloaderCmd) ExecuteBody(logger log.Logger, fn bodyFunc) error {
downloader, err = github.NewDownloader(client, db)
}

rate0, err := downloader.RateRemaining(context.TODO())
rate0, err := downloader.RateRemaining(ctx)
if err != nil {
return err
}
Expand All @@ -160,18 +162,18 @@ func (c *DownloaderCmd) ExecuteBody(logger log.Logger, fn bodyFunc) error {
return err
}

err = downloader.SetCurrent(c.Version)
err = downloader.SetCurrent(ctx, c.Version)
if err != nil {
return err
}

if c.Cleanup {
return downloader.Cleanup(c.Version)
return downloader.Cleanup(ctx, c.Version)
}

elapsed := time.Since(t0)

rate1, err := downloader.RateRemaining(context.TODO())
rate1, err := downloader.RateRemaining(ctx)
if err != nil {
return err
}
Expand Down
52 changes: 26 additions & 26 deletions github/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ const (
)

type storer interface {
SaveOrganization(organization *graphql.Organization) error
SaveUser(user *graphql.UserExtended) error
SaveRepository(repository *graphql.RepositoryFields, topics []string) error
SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error
SaveIssueComment(repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error
SavePullRequest(repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error
SavePullRequestComment(repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error
SavePullRequestReview(repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error
SavePullRequestReviewComment(repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error
SaveOrganization(ctx context.Context, organization *graphql.Organization) error
SaveUser(ctx context.Context, user *graphql.UserExtended) error
SaveRepository(ctx context.Context, repository *graphql.RepositoryFields, topics []string) error
SaveIssue(ctx context.Context, repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error
SaveIssueComment(ctx context.Context, repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error
SavePullRequest(ctx context.Context, repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error
SavePullRequestComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error
SavePullRequestReview(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error
SavePullRequestReviewComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error

Begin() error
Commit() error
Rollback() error
Version(v int)
SetActiveVersion(v int) error
Cleanup(currentVersion int) error
SetActiveVersion(ctx context.Context, v int) error
Cleanup(ctx context.Context, currentVersion int) error
}

// Downloader fetches GitHub data using the v4 API
Expand Down Expand Up @@ -148,7 +148,7 @@ func (d Downloader) DownloadRepository(ctx context.Context, owner string, name s
return err
}

err = d.storer.SaveRepository(&q.Repository.RepositoryFields, topics)
err = d.storer.SaveRepository(ctx, &q.Repository.RepositoryFields, topics)
if err != nil {
return fmt.Errorf("failed to save repository %v: %v", q.Repository.NameWithOwner, err)
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (d Downloader) downloadIssues(ctx context.Context, owner string, name strin
return err
}

err = d.storer.SaveIssue(owner, name, issue, assignees, labels)
err = d.storer.SaveIssue(ctx, owner, name, issue, assignees, labels)
if err != nil {
return err
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func (d Downloader) downloadIssueLabels(ctx context.Context, issue *graphql.Issu
func (d Downloader) downloadIssueComments(ctx context.Context, owner string, name string, issue *graphql.Issue) error {
// save first page of comments
for _, comment := range issue.Comments.Nodes {
err := d.storer.SaveIssueComment(owner, name, issue.Number, &comment)
err := d.storer.SaveIssueComment(ctx, owner, name, issue.Number, &comment)
if err != nil {
return err
}
Expand Down Expand Up @@ -454,7 +454,7 @@ func (d Downloader) downloadIssueComments(ctx context.Context, owner string, nam
}

for _, comment := range q.Node.Issue.Comments.Nodes {
err := d.storer.SaveIssueComment(owner, name, issue.Number, &comment)
err := d.storer.SaveIssueComment(ctx, owner, name, issue.Number, &comment)
if err != nil {
return fmt.Errorf("failed to save issue comments for issue #%v: %v", issue.Number, err)
}
Expand Down Expand Up @@ -483,7 +483,7 @@ func (d Downloader) downloadPullRequests(ctx context.Context, owner string, name
return err
}

err = d.storer.SavePullRequest(owner, name, pr, assignees, labels)
err = d.storer.SavePullRequest(ctx, owner, name, pr, assignees, labels)
if err != nil {
return err
}
Expand Down Expand Up @@ -664,7 +664,7 @@ func (d Downloader) downloadPullRequestLabels(ctx context.Context, pr *graphql.P
func (d Downloader) downloadPullRequestComments(ctx context.Context, owner string, name string, pr *graphql.PullRequest) error {
// save first page of comments
for _, comment := range pr.Comments.Nodes {
err := d.storer.SavePullRequestComment(owner, name, pr.Number, &comment)
err := d.storer.SavePullRequestComment(ctx, owner, name, pr.Number, &comment)
if err != nil {
return fmt.Errorf("failed to save PR comments for PR #%v: %v", pr.Number, err)
}
Expand Down Expand Up @@ -699,7 +699,7 @@ func (d Downloader) downloadPullRequestComments(ctx context.Context, owner strin
}

for _, comment := range q.Node.PullRequest.Comments.Nodes {
err := d.storer.SavePullRequestComment(owner, name, pr.Number, &comment)
err := d.storer.SavePullRequestComment(ctx, owner, name, pr.Number, &comment)
if err != nil {
return fmt.Errorf("failed to save PR comments for PR #%v: %v", pr.Number, err)
}
Expand All @@ -714,7 +714,7 @@ func (d Downloader) downloadPullRequestComments(ctx context.Context, owner strin

func (d Downloader) downloadPullRequestReviews(ctx context.Context, owner string, name string, pr *graphql.PullRequest) error {
process := func(review *graphql.PullRequestReview) error {
err := d.storer.SavePullRequestReview(owner, name, pr.Number, review)
err := d.storer.SavePullRequestReview(ctx, owner, name, pr.Number, review)
if err != nil {
return fmt.Errorf("failed to save PR review for PR #%v: %v", pr.Number, err)
}
Expand Down Expand Up @@ -776,7 +776,7 @@ func (d Downloader) downloadPullRequestReviews(ctx context.Context, owner string

func (d Downloader) downloadReviewComments(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error {
process := func(comment *graphql.PullRequestReviewComment) error {
err := d.storer.SavePullRequestReviewComment(repositoryOwner, repositoryName, pullRequestNumber, review.DatabaseId, comment)
err := d.storer.SavePullRequestReviewComment(ctx, repositoryOwner, repositoryName, pullRequestNumber, review.DatabaseId, comment)
if err != nil {
return fmt.Errorf(
"failed to save PullRequestReviewComment for PR #%v, review ID %v: %v",
Expand Down Expand Up @@ -877,7 +877,7 @@ func (d Downloader) DownloadOrganization(ctx context.Context, name string, versi
return fmt.Errorf("organization query failed: %v", err)
}

err = d.storer.SaveOrganization(&q.Organization)
err = d.storer.SaveOrganization(ctx, &q.Organization)
if err != nil {
return fmt.Errorf("failed to save organization %v: %v", name, err)
}
Expand All @@ -898,7 +898,7 @@ func (d Downloader) downloadUsers(ctx context.Context, name string, organization
defer logger.Infof("finished downloading users")

process := func(user *graphql.UserExtended) error {
err := d.storer.SaveUser(user)
err := d.storer.SaveUser(ctx, user)
if err != nil {
return fmt.Errorf("failed to save UserExtended: %v", err)
}
Expand Down Expand Up @@ -955,17 +955,17 @@ func (d Downloader) downloadUsers(ctx context.Context, name string, organization
}

// SetCurrent enables the given version as the current one accessible in the DB
func (d Downloader) SetCurrent(version int) error {
err := d.storer.SetActiveVersion(version)
func (d Downloader) SetCurrent(ctx context.Context, version int) error {
err := d.storer.SetActiveVersion(ctx, version)
if err != nil {
return fmt.Errorf("failed to set current DB version to %v: %v", version, err)
}
return nil
}

// Cleanup deletes from the DB all records that do not belong to the currentVersion
func (d Downloader) Cleanup(currentVersion int) error {
err := d.storer.Cleanup(currentVersion)
func (d Downloader) Cleanup(ctx context.Context, currentVersion int) error {
err := d.storer.Cleanup(ctx, currentVersion)
if err != nil {
return fmt.Errorf("failed to do cleanup for DB version %v: %v", currentVersion, err)
}
Expand Down
Loading

0 comments on commit 7d14c5c

Please sign in to comment.