From 06d1f1b8d498e283961972e808c4bf1adbbac26f Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 13 Jun 2015 18:40:07 -0700 Subject: [PATCH] remove meddler from all sql --- pkg/store/builtin/commit.go | 64 ++-- pkg/store/builtin/commit_sql.go | 520 +++++++++++++++++++++++++++ pkg/store/builtin/commit_test.go | 62 ++-- pkg/store/builtin/migrate/migrate.go | 10 +- pkg/store/builtin/user.go | 39 +- pkg/types/commit.go | 8 +- pkg/types/repo.go | 2 +- 7 files changed, 616 insertions(+), 89 deletions(-) create mode 100644 pkg/store/builtin/commit_sql.go diff --git a/pkg/store/builtin/commit.go b/pkg/store/builtin/commit.go index 3fd90004..da369871 100644 --- a/pkg/store/builtin/commit.go +++ b/pkg/store/builtin/commit.go @@ -4,8 +4,7 @@ import ( "database/sql" "time" - "github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler" - common "github.com/drone/drone/pkg/types" + "github.com/drone/drone/pkg/types" ) type Commitstore struct { @@ -17,38 +16,30 @@ func NewCommitstore(db *sql.DB) *Commitstore { } // Commit gets a commit by ID -func (db *Commitstore) Commit(id int64) (*common.Commit, error) { - var commit = new(common.Commit) - var err = meddler.Load(db, commitTable, commit, id) - return commit, err +func (db *Commitstore) Commit(id int64) (*types.Commit, error) { + return getCommit(db, rebind(stmtCommitSelect), id) } // CommitSeq gets the specified commit sequence for the // named repository and commit number -func (db *Commitstore) CommitSeq(repo *common.Repo, seq int) (*common.Commit, error) { - var commit = new(common.Commit) - var err = meddler.QueryRow(db, commit, rebind(commitNumberQuery), repo.ID, seq) - return commit, err +func (db *Commitstore) CommitSeq(repo *types.Repo, seq int) (*types.Commit, error) { + return getCommit(db, rebind(stmtCommitSelectCommitSeq), repo.ID, seq) } // CommitLast gets the last executed commit for the // named repository. -func (db *Commitstore) CommitLast(repo *common.Repo, branch string) (*common.Commit, error) { - var commit = new(common.Commit) - var err = meddler.QueryRow(db, commit, rebind(commitLastQuery), repo.ID, branch) - return commit, err +func (db *Commitstore) CommitLast(repo *types.Repo, branch string) (*types.Commit, error) { + return getCommit(db, rebind(commitLastQuery), repo.ID, branch) } // CommitList gets a list of recent commits for the // named repository. -func (db *Commitstore) CommitList(repo *common.Repo, limit, offset int) ([]*common.Commit, error) { - var commits []*common.Commit - var err = meddler.QueryAll(db, &commits, rebind(commitListQuery), repo.ID, limit, offset) - return commits, err +func (db *Commitstore) CommitList(repo *types.Repo, limit, offset int) ([]*types.Commit, error) { + return getCommits(db, rebind(commitListQuery), repo.ID, limit, offset) } // AddCommit inserts a new commit in the datastore. -func (db *Commitstore) AddCommit(commit *common.Commit) error { +func (db *Commitstore) AddCommit(commit *types.Commit) error { tx, err := db.Begin() if err != nil { return err @@ -64,7 +55,7 @@ func (db *Commitstore) AddCommit(commit *common.Commit) error { commit.Sequence = commit.Sequence + 1 // increment commit.Created = time.Now().UTC().Unix() commit.Updated = time.Now().UTC().Unix() - err = meddler.Insert(tx, commitTable, commit) + err = createCommit(tx, rebind(stmtCommitInsert), commit) if err != nil { return err } @@ -73,7 +64,7 @@ func (db *Commitstore) AddCommit(commit *common.Commit) error { build.CommitID = commit.ID build.Created = commit.Created build.Updated = commit.Updated - err := meddler.Insert(tx, buildTable, build) + err := createBuild(tx, rebind(stmtBuildInsert), build) if err != nil { return err } @@ -82,7 +73,7 @@ func (db *Commitstore) AddCommit(commit *common.Commit) error { } // SetCommit updates an existing commit and commit tasks. -func (db *Commitstore) SetCommit(commit *common.Commit) error { +func (db *Commitstore) SetCommit(commit *types.Commit) error { tx, err := db.Begin() if err != nil { return err @@ -90,14 +81,14 @@ func (db *Commitstore) SetCommit(commit *common.Commit) error { defer tx.Rollback() commit.Updated = time.Now().UTC().Unix() - err = meddler.Update(tx, commitTable, commit) + err = updateCommit(tx, rebind(stmtCommitUpdate), commit) if err != nil { return err } for _, build := range commit.Builds { build.Updated = commit.Updated - err := meddler.Update(tx, buildTable, build) + err = updateBuild(tx, rebind(stmtBuildUpdate), build) if err != nil { return err } @@ -123,28 +114,19 @@ const commitTable = "commits" const commitListQuery = ` SELECT * FROM commits -WHERE repo_id = ? -ORDER BY commit_seq DESC +WHERE commit_repo_id = ? +ORDER BY commit_sequence DESC LIMIT ? OFFSET ? ` -// SQL query to retrieve a commit by number. -const commitNumberQuery = ` -SELECT * -FROM commits -WHERE repo_id = ? - AND commit_seq = ? -LIMIT 1 -` - // SQL query to retrieve the most recent commit. // TODO exclude pull requests const commitLastQuery = ` SELECT * FROM commits -WHERE repo_id = ? - AND commit_branch = ? -ORDER BY commit_seq DESC +WHERE commit_repo_id = ? + AND commit_branch = ? +ORDER BY commit_sequence DESC LIMIT 1 ` @@ -163,7 +145,7 @@ WHERE build_state IN ('pending', 'running'); // SQL statement to retrieve the commit number for // a commit const commitNumberLast = ` -SELECT MAX(commit_seq) -FROM commits -WHERE repo_id = ? +SELECT MAX(commit_sequence) +FROM commits +WHERE commit_repo_id = ? ` diff --git a/pkg/store/builtin/commit_sql.go b/pkg/store/builtin/commit_sql.go new file mode 100644 index 00000000..9357d416 --- /dev/null +++ b/pkg/store/builtin/commit_sql.go @@ -0,0 +1,520 @@ +package builtin + +// DO NOT EDIT +// code generated by go:generate + +import ( + "database/sql" + "encoding/json" + + . "github.com/drone/drone/pkg/types" +) + +var _ = json.Marshal + +// generic database interface, matching both *sql.Db and *sql.Tx +type commitDB interface { + Exec(query string, args ...interface{}) (sql.Result, error) + Query(query string, args ...interface{}) (*sql.Rows, error) + QueryRow(query string, args ...interface{}) *sql.Row +} + +func getCommit(db commitDB, query string, args ...interface{}) (*Commit, error) { + row := db.QueryRow(query, args...) + return scanCommit(row) +} + +func getCommits(db commitDB, query string, args ...interface{}) ([]*Commit, error) { + rows, err := db.Query(query, args...) + if err != nil { + return nil, err + } + defer rows.Close() + return scanCommits(rows) +} + +func createCommit(db commitDB, query string, v *Commit) error { + var v0 int64 + var v1 int + var v2 string + var v3 int64 + var v4 int64 + var v5 string + var v6 string + var v7 string + var v8 string + var v9 string + var v10 string + var v11 string + var v12 string + var v13 string + var v14 string + var v15 string + var v16 int64 + var v17 int64 + v0 = v.RepoID + v1 = v.Sequence + v2 = v.State + v3 = v.Started + v4 = v.Finished + v5 = v.Sha + v6 = v.Ref + v7 = v.PullRequest + v8 = v.Branch + v9 = v.Author + v10 = v.Gravatar + v11 = v.Timestamp + v12 = v.Message + v13 = v.SourceRemote + v14 = v.SourceBranch + v15 = v.SourceSha + v16 = v.Created + v17 = v.Updated + + res, err := db.Exec(query, + &v0, + &v1, + &v2, + &v3, + &v4, + &v5, + &v6, + &v7, + &v8, + &v9, + &v10, + &v11, + &v12, + &v13, + &v14, + &v15, + &v16, + &v17, + ) + if err != nil { + return err + } + + v.ID, err = res.LastInsertId() + return err +} + +func updateCommit(db commitDB, query string, v *Commit) error { + var v0 int64 + var v1 int64 + var v2 int + var v3 string + var v4 int64 + var v5 int64 + var v6 string + var v7 string + var v8 string + var v9 string + var v10 string + var v11 string + var v12 string + var v13 string + var v14 string + var v15 string + var v16 string + var v17 int64 + var v18 int64 + v0 = v.ID + v1 = v.RepoID + v2 = v.Sequence + v3 = v.State + v4 = v.Started + v5 = v.Finished + v6 = v.Sha + v7 = v.Ref + v8 = v.PullRequest + v9 = v.Branch + v10 = v.Author + v11 = v.Gravatar + v12 = v.Timestamp + v13 = v.Message + v14 = v.SourceRemote + v15 = v.SourceBranch + v16 = v.SourceSha + v17 = v.Created + v18 = v.Updated + + _, err := db.Exec(query, + &v1, + &v2, + &v3, + &v4, + &v5, + &v6, + &v7, + &v8, + &v9, + &v10, + &v11, + &v12, + &v13, + &v14, + &v15, + &v16, + &v17, + &v18, + &v0, + ) + return err +} + +func scanCommit(row *sql.Row) (*Commit, error) { + var v0 int64 + var v1 int64 + var v2 int + var v3 string + var v4 int64 + var v5 int64 + var v6 string + var v7 string + var v8 string + var v9 string + var v10 string + var v11 string + var v12 string + var v13 string + var v14 string + var v15 string + var v16 string + var v17 int64 + var v18 int64 + + err := row.Scan( + &v0, + &v1, + &v2, + &v3, + &v4, + &v5, + &v6, + &v7, + &v8, + &v9, + &v10, + &v11, + &v12, + &v13, + &v14, + &v15, + &v16, + &v17, + &v18, + ) + if err != nil { + return nil, err + } + + v := &Commit{} + v.ID = v0 + v.RepoID = v1 + v.Sequence = v2 + v.State = v3 + v.Started = v4 + v.Finished = v5 + v.Sha = v6 + v.Ref = v7 + v.PullRequest = v8 + v.Branch = v9 + v.Author = v10 + v.Gravatar = v11 + v.Timestamp = v12 + v.Message = v13 + v.SourceRemote = v14 + v.SourceBranch = v15 + v.SourceSha = v16 + v.Created = v17 + v.Updated = v18 + + return v, nil +} + +func scanCommits(rows *sql.Rows) ([]*Commit, error) { + var err error + var vv []*Commit + for rows.Next() { + var v0 int64 + var v1 int64 + var v2 int + var v3 string + var v4 int64 + var v5 int64 + var v6 string + var v7 string + var v8 string + var v9 string + var v10 string + var v11 string + var v12 string + var v13 string + var v14 string + var v15 string + var v16 string + var v17 int64 + var v18 int64 + err = rows.Scan( + &v0, + &v1, + &v2, + &v3, + &v4, + &v5, + &v6, + &v7, + &v8, + &v9, + &v10, + &v11, + &v12, + &v13, + &v14, + &v15, + &v16, + &v17, + &v18, + ) + if err != nil { + return vv, err + } + + v := &Commit{} + v.ID = v0 + v.RepoID = v1 + v.Sequence = v2 + v.State = v3 + v.Started = v4 + v.Finished = v5 + v.Sha = v6 + v.Ref = v7 + v.PullRequest = v8 + v.Branch = v9 + v.Author = v10 + v.Gravatar = v11 + v.Timestamp = v12 + v.Message = v13 + v.SourceRemote = v14 + v.SourceBranch = v15 + v.SourceSha = v16 + v.Created = v17 + v.Updated = v18 + vv = append(vv, v) + } + return vv, rows.Err() +} + +const stmtCommitSelectList = ` +SELECT + commit_id +,commit_repo_id +,commit_sequence +,commit_state +,commit_started +,commit_finished +,commit_sha +,commit_ref +,commit_pull_request +,commit_branch +,commit_author +,commit_gravatar +,commit_timestamp +,commit_message +,commit_source_remote +,commit_source_branch +,commit_source_sha +,commit_created +,commit_updated +FROM commits +` + +const stmtCommitSelectRange = ` +SELECT + commit_id +,commit_repo_id +,commit_sequence +,commit_state +,commit_started +,commit_finished +,commit_sha +,commit_ref +,commit_pull_request +,commit_branch +,commit_author +,commit_gravatar +,commit_timestamp +,commit_message +,commit_source_remote +,commit_source_branch +,commit_source_sha +,commit_created +,commit_updated +FROM commits +LIMIT ? OFFSET ? +` + +const stmtCommitSelect = ` +SELECT + commit_id +,commit_repo_id +,commit_sequence +,commit_state +,commit_started +,commit_finished +,commit_sha +,commit_ref +,commit_pull_request +,commit_branch +,commit_author +,commit_gravatar +,commit_timestamp +,commit_message +,commit_source_remote +,commit_source_branch +,commit_source_sha +,commit_created +,commit_updated +FROM commits +WHERE commit_id = ? +` + +const stmtCommitSelectCommitSeq = ` +SELECT + commit_id +,commit_repo_id +,commit_sequence +,commit_state +,commit_started +,commit_finished +,commit_sha +,commit_ref +,commit_pull_request +,commit_branch +,commit_author +,commit_gravatar +,commit_timestamp +,commit_message +,commit_source_remote +,commit_source_branch +,commit_source_sha +,commit_created +,commit_updated +FROM commits +WHERE commit_repo_id = ? +AND commit_sequence = ? +` + +const stmtCommitSelectCommitRepoId = ` +SELECT + commit_id +,commit_repo_id +,commit_sequence +,commit_state +,commit_started +,commit_finished +,commit_sha +,commit_ref +,commit_pull_request +,commit_branch +,commit_author +,commit_gravatar +,commit_timestamp +,commit_message +,commit_source_remote +,commit_source_branch +,commit_source_sha +,commit_created +,commit_updated +FROM commits +WHERE commit_repo_id = ? +` + +const stmtCommitSelectCount = ` +SELECT count(1) +FROM commits +` + +const stmtCommitInsert = ` +INSERT INTO commits ( + commit_repo_id +,commit_sequence +,commit_state +,commit_started +,commit_finished +,commit_sha +,commit_ref +,commit_pull_request +,commit_branch +,commit_author +,commit_gravatar +,commit_timestamp +,commit_message +,commit_source_remote +,commit_source_branch +,commit_source_sha +,commit_created +,commit_updated +) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?); +` + +const stmtCommitUpdate = ` +UPDATE commits SET + commit_repo_id = ? +,commit_sequence = ? +,commit_state = ? +,commit_started = ? +,commit_finished = ? +,commit_sha = ? +,commit_ref = ? +,commit_pull_request = ? +,commit_branch = ? +,commit_author = ? +,commit_gravatar = ? +,commit_timestamp = ? +,commit_message = ? +,commit_source_remote = ? +,commit_source_branch = ? +,commit_source_sha = ? +,commit_created = ? +,commit_updated = ? +WHERE commit_id = ? +` + +const stmtCommitDelete = ` +DELETE FROM commits +WHERE commit_id = ? +` + +const stmtCommitTable = ` +CREATE TABLE IF NOT EXISTS commits ( + commit_id INTEGER PRIMARY KEY AUTOINCREMENT +,commit_repo_id INTEGER +,commit_sequence INTEGER +,commit_state VARCHAR +,commit_started INTEGER +,commit_finished INTEGER +,commit_sha VARCHAR +,commit_ref VARCHAR +,commit_pull_request VARCHAR +,commit_branch VARCHAR +,commit_author VARCHAR +,commit_gravatar VARCHAR +,commit_timestamp VARCHAR +,commit_message VARCHAR +,commit_source_remote VARCHAR +,commit_source_branch VARCHAR +,commit_source_sha VARCHAR +,commit_created INTEGER +,commit_updated INTEGER +); +` + +const stmtCommitCommitSeqIndex = ` +CREATE UNIQUE INDEX IF NOT EXISTS ux_commit_seq ON commits (commit_repo_id,commit_sequence); +` + +const stmtCommitCommitRepoIdIndex = ` +CREATE INDEX IF NOT EXISTS ix_commit_repo_id ON commits (commit_repo_id); +` diff --git a/pkg/store/builtin/commit_test.go b/pkg/store/builtin/commit_test.go index befd3a48..b5b3b361 100644 --- a/pkg/store/builtin/commit_test.go +++ b/pkg/store/builtin/commit_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin" - common "github.com/drone/drone/pkg/types" + "github.com/drone/drone/pkg/types" ) func TestCommitstore(t *testing.T) { @@ -23,9 +23,9 @@ func TestCommitstore(t *testing.T) { }) g.It("Should Post a Commit", func() { - commit := common.Commit{ + commit := types.Commit{ RepoID: 1, - State: common.StateSuccess, + State: types.StateSuccess, Ref: "refs/heads/master", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } @@ -36,15 +36,15 @@ func TestCommitstore(t *testing.T) { }) g.It("Should Put a Commit", func() { - commit := common.Commit{ + commit := types.Commit{ RepoID: 1, Sequence: 5, - State: common.StatePending, + State: types.StatePending, Ref: "refs/heads/master", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } bs.AddCommit(&commit) - commit.State = common.StateRunning + commit.State = types.StateRunning err1 := bs.SetCommit(&commit) getcommit, err2 := bs.Commit(commit.ID) g.Assert(err1 == nil).IsTrue() @@ -56,9 +56,9 @@ func TestCommitstore(t *testing.T) { }) g.It("Should Get a Commit", func() { - commit := common.Commit{ + commit := types.Commit{ RepoID: 1, - State: common.StateSuccess, + State: types.StateSuccess, } bs.AddCommit(&commit) getcommit, err := bs.Commit(commit.ID) @@ -69,21 +69,21 @@ func TestCommitstore(t *testing.T) { }) g.It("Should Get a Commit by Sequence", func() { - commit1 := &common.Commit{ + commit1 := &types.Commit{ RepoID: 1, - State: common.StatePending, + State: types.StatePending, Ref: "refs/heads/master", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } - commit2 := &common.Commit{ + commit2 := &types.Commit{ RepoID: 1, - State: common.StatePending, + State: types.StatePending, Ref: "refs/heads/dev", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } err1 := bs.AddCommit(commit1) err2 := bs.AddCommit(commit2) - getcommit, err3 := bs.CommitSeq(&common.Repo{ID: 1}, commit2.Sequence) + getcommit, err3 := bs.CommitSeq(&types.Repo{ID: 1}, commit2.Sequence) g.Assert(err1 == nil).IsTrue() g.Assert(err2 == nil).IsTrue() g.Assert(err3 == nil).IsTrue() @@ -93,15 +93,15 @@ func TestCommitstore(t *testing.T) { }) g.It("Should Kill Pending or Started Commits", func() { - commit1 := &common.Commit{ + commit1 := &types.Commit{ RepoID: 1, - State: common.StateRunning, + State: types.StateRunning, Ref: "refs/heads/master", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } - commit2 := &common.Commit{ + commit2 := &types.Commit{ RepoID: 1, - State: common.StatePending, + State: types.StatePending, Ref: "refs/heads/dev", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } @@ -113,26 +113,26 @@ func TestCommitstore(t *testing.T) { g.Assert(err1 == nil).IsTrue() g.Assert(err2 == nil).IsTrue() g.Assert(err3 == nil).IsTrue() - g.Assert(getcommit1.State).Equal(common.StateKilled) - g.Assert(getcommit2.State).Equal(common.StateKilled) + g.Assert(getcommit1.State).Equal(types.StateKilled) + g.Assert(getcommit2.State).Equal(types.StateKilled) }) g.It("Should get recent Commits", func() { - commit1 := &common.Commit{ + commit1 := &types.Commit{ RepoID: 1, - State: common.StateFailure, + State: types.StateFailure, Ref: "refs/heads/master", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } - commit2 := &common.Commit{ + commit2 := &types.Commit{ RepoID: 1, - State: common.StateSuccess, + State: types.StateSuccess, Ref: "refs/heads/dev", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } bs.AddCommit(commit1) bs.AddCommit(commit2) - commits, err := bs.CommitList(&common.Repo{ID: 1}, 20, 0) + commits, err := bs.CommitList(&types.Repo{ID: 1}, 20, 0) g.Assert(err == nil).IsTrue() g.Assert(len(commits)).Equal(2) g.Assert(commits[0].ID).Equal(commit2.ID) @@ -141,23 +141,23 @@ func TestCommitstore(t *testing.T) { }) g.It("Should get the last Commit", func() { - commit1 := &common.Commit{ + commit1 := &types.Commit{ RepoID: 1, - State: common.StateFailure, + State: types.StateFailure, Branch: "master", Ref: "refs/heads/master", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", } - commit2 := &common.Commit{ + commit2 := &types.Commit{ RepoID: 1, - State: common.StateFailure, + State: types.StateFailure, Branch: "master", Ref: "refs/heads/master", Sha: "8d6a233744a5dcacbf2605d4592a4bfe8b37320d", } - commit3 := &common.Commit{ + commit3 := &types.Commit{ RepoID: 1, - State: common.StateSuccess, + State: types.StateSuccess, Branch: "dev", Ref: "refs/heads/dev", Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", @@ -165,7 +165,7 @@ func TestCommitstore(t *testing.T) { err1 := bs.AddCommit(commit1) err2 := bs.AddCommit(commit2) err3 := bs.AddCommit(commit3) - last, err4 := bs.CommitLast(&common.Repo{ID: 1}, "master") + last, err4 := bs.CommitLast(&types.Repo{ID: 1}, "master") g.Assert(err1 == nil).IsTrue() g.Assert(err2 == nil).IsTrue() g.Assert(err3 == nil).IsTrue() diff --git a/pkg/store/builtin/migrate/migrate.go b/pkg/store/builtin/migrate/migrate.go index d9f3d145..facd061b 100644 --- a/pkg/store/builtin/migrate/migrate.go +++ b/pkg/store/builtin/migrate/migrate.go @@ -119,15 +119,15 @@ CREATE TABLE IF NOT EXISTS stars ( var commitTable = ` CREATE TABLE IF NOT EXISTS commits ( commit_id INTEGER PRIMARY KEY AUTOINCREMENT - ,repo_id INTEGER - ,commit_seq INTEGER + ,commit_repo_id INTEGER + ,commit_sequence INTEGER ,commit_state VARCHAR(255) ,commit_started INTEGER ,commit_finished INTEGER ,commit_sha VARCHAR(255) ,commit_ref VARCHAR(255) ,commit_branch VARCHAR(255) - ,commit_pr VARCHAR(255) + ,commit_pull_request VARCHAR(255) ,commit_author VARCHAR(255) ,commit_gravatar VARCHAR(255) ,commit_timestamp VARCHAR(255) @@ -137,12 +137,12 @@ CREATE TABLE IF NOT EXISTS commits ( ,commit_source_sha VARCHAR(255) ,commit_created INTEGER ,commit_updated INTEGER - ,UNIQUE(repo_id, commit_seq) + ,UNIQUE(commit_repo_id, commit_sequence) ); ` var commitRepoIndex = ` -CREATE INDEX commits_repo_idx ON commits (repo_id); +CREATE INDEX commits_repo_idx ON commits (commit_repo_id); ` var tokenTable = ` diff --git a/pkg/store/builtin/user.go b/pkg/store/builtin/user.go index 183957ca..e7013b4e 100644 --- a/pkg/store/builtin/user.go +++ b/pkg/store/builtin/user.go @@ -4,7 +4,6 @@ import ( "database/sql" "time" - "github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler" "github.com/drone/drone/pkg/types" ) @@ -34,9 +33,12 @@ func (db *Userstore) UserList() ([]*types.User, error) { // UserFeed retrieves a digest of recent builds // from the datastore accessible to the specified user. func (db *Userstore) UserFeed(user *types.User, limit, offset int) ([]*types.RepoCommit, error) { - var builds []*types.RepoCommit - var err = meddler.QueryAll(db, &builds, rebind(userFeedQuery), user.ID, limit, offset) - return builds, err + rows, err := db.Query(rebind(userFeedQuery), user.ID, limit, offset) + if err != nil { + return nil, err + } + defer rows.Close() + return scanRepoCommits(rows) } // UserCount returns a count of all registered users. @@ -74,7 +76,7 @@ SELECT ,r.repo_owner ,r.repo_name ,r.repo_full_name -,c.commit_seq +,c.commit_sequence ,c.commit_state ,c.commit_started ,c.commit_finished @@ -82,9 +84,32 @@ FROM commits c ,repos r ,stars s -WHERE c.repo_id = r.repo_id +WHERE c.commit_repo_id = r.repo_id AND r.repo_id = s.star_repo_id AND s.star_user_id = ? -ORDER BY c.commit_seq DESC +ORDER BY c.commit_sequence DESC LIMIT ? OFFSET ? ` + +func scanRepoCommits(rows *sql.Rows) ([]*types.RepoCommit, error) { + var err error + var vv []*types.RepoCommit + for rows.Next() { + v := &types.RepoCommit{} + err = rows.Scan( + &v.ID, + &v.Owner, + &v.Name, + &v.FullName, + &v.Number, + &v.State, + &v.Started, + &v.Finished, + ) + if err != nil { + return vv, err + } + vv = append(vv, v) + } + return vv, rows.Err() +} diff --git a/pkg/types/commit.go b/pkg/types/commit.go index b46ed535..5aa9ed0a 100644 --- a/pkg/types/commit.go +++ b/pkg/types/commit.go @@ -11,14 +11,14 @@ const ( type Commit struct { ID int64 `meddler:"commit_id,pk" json:"id"` - RepoID int64 `meddler:"repo_id" json:"-"` - Sequence int `meddler:"commit_seq" json:"sequence"` + RepoID int64 `meddler:"commit_repo_id" json:"-" sql:"unique:ux_commit_seq,index:ix_commit_repo_id"` + Sequence int `meddler:"commit_sequence" json:"sequence" sql:"unique:ux_commit_seq"` State string `meddler:"commit_state" json:"state"` Started int64 `meddler:"commit_started" json:"started_at"` Finished int64 `meddler:"commit_finished" json:"finished_at"` Sha string `meddler:"commit_sha" json:"sha"` Ref string `meddler:"commit_ref" json:"ref"` - PullRequest string `meddler:"commit_pr" json:"pull_request,omitempty"` + PullRequest string `meddler:"commit_pull_request" json:"pull_request,omitempty"` Branch string `meddler:"commit_branch" json:"branch"` Author string `meddler:"commit_author" json:"author"` Gravatar string `meddler:"commit_gravatar" json:"gravatar"` @@ -30,5 +30,5 @@ type Commit struct { Created int64 `meddler:"commit_created" json:"created_at"` Updated int64 `meddler:"commit_updated" json:"updated_at"` - Builds []*Build `meddler:"-" json:"builds,omitempty"` + Builds []*Build `meddler:"-" json:"builds,omitempty" sql:"-"` } diff --git a/pkg/types/repo.go b/pkg/types/repo.go index 5126e389..37d3a54a 100644 --- a/pkg/types/repo.go +++ b/pkg/types/repo.go @@ -42,7 +42,7 @@ type RepoCommit struct { Owner string `meddler:"repo_owner" json:"owner"` Name string `meddler:"repo_name" json:"name"` FullName string `meddler:"repo_full_name" json:"full_name"` - Number int `meddler:"commit_seq" json:"number"` + Number int `meddler:"commit_sequence" json:"number"` State string `meddler:"commit_state" json:"state"` Started int64 `meddler:"commit_started" json:"started_at"` Finished int64 `meddler:"commit_finished" json:"finished_at"`