2014-06-12 23:41:04 +00:00
|
|
|
package database
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommitManager interface {
|
|
|
|
// Find finds the commit by ID.
|
2014-06-12 23:41:04 +00:00
|
|
|
Find(id int64) (*model.Commit, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// FindSha finds the commit for the branch and sha.
|
2014-06-12 23:41:04 +00:00
|
|
|
FindSha(repo int64, branch, sha string) (*model.Commit, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// FindLatest finds the most recent commit for the branch.
|
2014-06-12 23:41:04 +00:00
|
|
|
FindLatest(repo int64, branch string) (*model.Commit, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// FindOutput finds the commit's output.
|
2014-06-07 19:57:20 +00:00
|
|
|
FindOutput(commit int64) ([]byte, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// List finds recent commits for the repository
|
2014-06-12 23:41:04 +00:00
|
|
|
List(repo int64) ([]*model.Commit, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// ListBranch finds recent commits for the repository and branch.
|
2014-06-12 23:41:04 +00:00
|
|
|
ListBranch(repo int64, branch string) ([]*model.Commit, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// ListBranches finds most recent commit for each branch.
|
2014-06-12 23:41:04 +00:00
|
|
|
ListBranches(repo int64) ([]*model.Commit, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// ListUser finds most recent commits for a user.
|
2014-06-12 23:41:04 +00:00
|
|
|
ListUser(repo int64) ([]*model.CommitRepo, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Insert persists the commit to the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
Insert(commit *model.Commit) error
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Update persists changes to the commit to the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
Update(commit *model.Commit) error
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// UpdateOutput persists a commit's stdout to the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
UpdateOutput(commit *model.Commit, out []byte) error
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Delete removes the commit from the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
Delete(commit *model.Commit) error
|
2014-06-11 19:39:29 +00:00
|
|
|
|
|
|
|
// CancelAll will update the status of all Started or Pending
|
|
|
|
// builds to a status of Killed (cancelled).
|
|
|
|
CancelAll() error
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// commitManager manages a list of commits in a SQL database.
|
|
|
|
type commitManager struct {
|
|
|
|
*sql.DB
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
// NewCommitManager initiales a new CommitManager intended to
|
2014-06-04 21:25:38 +00:00
|
|
|
// manage and persist commits.
|
2014-06-12 23:41:04 +00:00
|
|
|
func NewCommitManager(db *sql.DB) CommitManager {
|
2014-06-04 21:25:38 +00:00
|
|
|
return &commitManager{db}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SQL query to retrieve the latest Commits for each branch.
|
|
|
|
const listBranchesQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM commits
|
|
|
|
WHERE commit_id IN (
|
|
|
|
SELECT MAX(commit_id)
|
|
|
|
FROM commits
|
|
|
|
WHERE repo_id=?
|
|
|
|
AND commit_status NOT IN ('Started', 'Pending')
|
|
|
|
GROUP BY commit_branch)
|
|
|
|
ORDER BY commit_branch ASC
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve the latest Commits for a specific branch.
|
|
|
|
const listBranchQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM commits
|
|
|
|
WHERE repo_id=?
|
|
|
|
AND commit_branch=?
|
|
|
|
ORDER BY commit_id DESC
|
|
|
|
LIMIT 20
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve the latest Commits for a user's repositories.
|
2014-06-12 23:41:04 +00:00
|
|
|
const listUserCommitsQuery = `
|
2014-06-07 20:37:00 +00:00
|
|
|
SELECT r.repo_remote, r.repo_host, r.repo_owner, r.repo_name, c.*
|
2014-06-04 21:25:38 +00:00
|
|
|
FROM commits c, repos r, perms p
|
|
|
|
WHERE c.repo_id=r.repo_id
|
|
|
|
AND r.repo_id=p.repo_id
|
|
|
|
AND p.user_id=?
|
|
|
|
AND c.commit_status NOT IN ('Started', 'Pending')
|
|
|
|
ORDER BY commit_id DESC
|
|
|
|
LIMIT 20
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve the latest Commits across all branches.
|
2014-06-12 23:41:04 +00:00
|
|
|
const listCommitsQuery = `
|
2014-06-04 21:25:38 +00:00
|
|
|
SELECT *
|
|
|
|
FROM commits
|
|
|
|
WHERE repo_id=?
|
|
|
|
ORDER BY commit_id DESC
|
|
|
|
LIMIT 20
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve a Commit by branch and sha.
|
2014-06-12 23:41:04 +00:00
|
|
|
const findCommitQuery = `
|
2014-06-04 21:25:38 +00:00
|
|
|
SELECT *
|
|
|
|
FROM commits
|
|
|
|
WHERE repo_id=?
|
|
|
|
AND commit_branch=?
|
|
|
|
AND commit_sha=?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve the most recent Commit for a branch.
|
2014-06-12 23:41:04 +00:00
|
|
|
const findLatestCommitQuery = `
|
2014-06-04 21:25:38 +00:00
|
|
|
SELECT *
|
|
|
|
FROM commits
|
|
|
|
WHERE commit_id IN (
|
|
|
|
SELECT MAX(commit_id)
|
|
|
|
FROM commits
|
|
|
|
WHERE repo_id=?
|
|
|
|
AND commit_branch=?)
|
|
|
|
`
|
|
|
|
|
2014-06-07 19:57:20 +00:00
|
|
|
// SQL query to retrieve a Commit's stdout.
|
|
|
|
const findOutputQuery = `
|
|
|
|
SELECT output_raw
|
|
|
|
FROM output
|
|
|
|
WHERE commit_id = ?
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to insert a Commit's stdout.
|
|
|
|
const insertOutputStmt = `
|
|
|
|
INSERT INTO output (commit_id, output_raw) values (?,?);
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to update a Commit's stdout.
|
|
|
|
const updateOutputStmt = `
|
|
|
|
UPDATE output SET output_raw = ? WHERE commit_id = ?;
|
|
|
|
`
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// SQL statement to delete a Commit by ID.
|
2014-06-12 23:41:04 +00:00
|
|
|
const deleteCommitStmt = `
|
2014-06-11 19:39:29 +00:00
|
|
|
DELETE FROM commits WHERE commit_id = ?;
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to cancel all running Commits.
|
2014-06-12 23:41:04 +00:00
|
|
|
const cancelCommitStmt = `
|
2014-06-11 19:39:29 +00:00
|
|
|
UPDATE commits SET
|
|
|
|
commit_status = ?,
|
|
|
|
commit_started = ?,
|
|
|
|
commit_finished = ?
|
|
|
|
WHERE commit_status IN ('Started', 'Pending');
|
2014-06-04 21:25:38 +00:00
|
|
|
`
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) Find(id int64) (*model.Commit, error) {
|
|
|
|
dst := model.Commit{}
|
2014-06-04 21:25:38 +00:00
|
|
|
err := meddler.Load(db, "commits", &dst, id)
|
|
|
|
return &dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) FindSha(repo int64, branch, sha string) (*model.Commit, error) {
|
|
|
|
dst := model.Commit{}
|
|
|
|
err := meddler.QueryRow(db, &dst, findCommitQuery, repo, branch, sha)
|
2014-06-04 21:25:38 +00:00
|
|
|
return &dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) FindLatest(repo int64, branch string) (*model.Commit, error) {
|
|
|
|
dst := model.Commit{}
|
|
|
|
err := meddler.QueryRow(db, &dst, findLatestCommitQuery, repo, branch)
|
2014-06-04 21:25:38 +00:00
|
|
|
return &dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-07 19:57:20 +00:00
|
|
|
func (db *commitManager) FindOutput(commit int64) ([]byte, error) {
|
|
|
|
var dst string
|
|
|
|
err := db.QueryRow(findOutputQuery, commit).Scan(&dst)
|
|
|
|
return []byte(dst), err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) List(repo int64) ([]*model.Commit, error) {
|
|
|
|
var dst []*model.Commit
|
|
|
|
err := meddler.QueryAll(db, &dst, listCommitsQuery, repo)
|
2014-06-04 21:25:38 +00:00
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) ListBranch(repo int64, branch string) ([]*model.Commit, error) {
|
|
|
|
var dst []*model.Commit
|
2014-06-04 21:25:38 +00:00
|
|
|
err := meddler.QueryAll(db, &dst, listBranchQuery, repo, branch)
|
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) ListBranches(repo int64) ([]*model.Commit, error) {
|
|
|
|
var dst []*model.Commit
|
2014-06-04 21:25:38 +00:00
|
|
|
err := meddler.QueryAll(db, &dst, listBranchesQuery, repo)
|
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) ListUser(user int64) ([]*model.CommitRepo, error) {
|
|
|
|
var dst []*model.CommitRepo
|
|
|
|
err := meddler.QueryAll(db, &dst, listUserCommitsQuery, user)
|
2014-06-04 21:25:38 +00:00
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) Insert(commit *model.Commit) error {
|
2014-06-04 21:25:38 +00:00
|
|
|
commit.Created = time.Now().Unix()
|
|
|
|
commit.Updated = time.Now().Unix()
|
|
|
|
return meddler.Insert(db, "commits", commit)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) Update(commit *model.Commit) error {
|
2014-06-04 21:25:38 +00:00
|
|
|
commit.Updated = time.Now().Unix()
|
|
|
|
return meddler.Update(db, "commits", commit)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) UpdateOutput(commit *model.Commit, out []byte) error {
|
2014-06-07 19:57:20 +00:00
|
|
|
_, err := db.Exec(insertOutputStmt, commit.ID, out)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err = db.Exec(updateOutputStmt, out, commit.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *commitManager) Delete(commit *model.Commit) error {
|
|
|
|
_, err := db.Exec(deleteCommitStmt, commit.ID)
|
2014-06-04 21:25:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-06-11 19:39:29 +00:00
|
|
|
|
|
|
|
func (db *commitManager) CancelAll() error {
|
2014-06-12 23:41:04 +00:00
|
|
|
_, err := db.Exec(cancelCommitStmt, model.StatusKilled, time.Now().Unix(), time.Now().Unix())
|
2014-06-11 19:39:29 +00:00
|
|
|
return err
|
|
|
|
}
|