2015-10-21 23:14:02 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2017-05-23 10:43:58 +00:00
|
|
|
"fmt"
|
2015-10-21 23:14:02 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2017-05-23 10:43:58 +00:00
|
|
|
"github.com/drone/drone/store/datastore/sql"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuild(id int64) (*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var build = new(model.Build)
|
|
|
|
var err = meddler.Load(db, buildTable, build, id)
|
|
|
|
return build, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuildNumber(repo *model.Repo, num int) (*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var build = new(model.Build)
|
|
|
|
var err = meddler.QueryRow(db, build, rebind(buildNumberQuery), repo.ID, num)
|
|
|
|
return build, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuildRef(repo *model.Repo, ref string) (*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var build = new(model.Build)
|
|
|
|
var err = meddler.QueryRow(db, build, rebind(buildRefQuery), repo.ID, ref)
|
|
|
|
return build, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuildCommit(repo *model.Repo, sha, branch string) (*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var build = new(model.Build)
|
|
|
|
var err = meddler.QueryRow(db, build, rebind(buildCommitQuery), repo.ID, sha, branch)
|
|
|
|
return build, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuildLast(repo *model.Repo, branch string) (*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var build = new(model.Build)
|
|
|
|
var err = meddler.QueryRow(db, build, rebind(buildLastQuery), repo.ID, branch)
|
|
|
|
return build, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuildLastBefore(repo *model.Repo, branch string, num int64) (*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var build = new(model.Build)
|
|
|
|
var err = meddler.QueryRow(db, build, rebind(buildLastBeforeQuery), repo.ID, branch, num)
|
|
|
|
return build, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) GetBuildList(repo *model.Repo) ([]*model.Build, error) {
|
2015-10-21 23:14:02 +00:00
|
|
|
var builds = []*model.Build{}
|
|
|
|
var err = meddler.QueryAll(db, &builds, rebind(buildListQuery), repo.ID)
|
|
|
|
return builds, err
|
|
|
|
}
|
|
|
|
|
2016-05-11 05:19:45 +00:00
|
|
|
func (db *datastore) GetBuildQueue() ([]*model.Feed, error) {
|
|
|
|
feed := []*model.Feed{}
|
|
|
|
err := meddler.QueryAll(db, &feed, buildQueueList)
|
|
|
|
return feed, err
|
|
|
|
}
|
|
|
|
|
2017-04-02 14:13:26 +00:00
|
|
|
func (db *datastore) CreateBuild(build *model.Build, procs ...*model.Proc) error {
|
2017-05-23 10:43:58 +00:00
|
|
|
id, err := db.incrementRepoRetry(build.RepoID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
build.Number = id
|
2015-10-21 23:14:02 +00:00
|
|
|
build.Created = time.Now().UTC().Unix()
|
|
|
|
build.Enqueued = build.Created
|
2017-05-23 10:43:58 +00:00
|
|
|
err = meddler.Insert(db, buildTable, build)
|
2015-10-21 23:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-02 14:13:26 +00:00
|
|
|
for _, proc := range procs {
|
|
|
|
proc.BuildID = build.ID
|
|
|
|
err = meddler.Insert(db, "procs", proc)
|
2015-10-21 23:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:43:58 +00:00
|
|
|
func (db *datastore) incrementRepoRetry(id int64) (int, error) {
|
|
|
|
repo, err := db.GetRepo(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("database: cannot fetch repository: %s", err)
|
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
seq, err := db.incrementRepo(repo.ID, repo.Counter+i, repo.Counter+i+1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if seq == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return seq, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("cannot increment next build number")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *datastore) incrementRepo(id int64, old, new int) (int, error) {
|
|
|
|
results, err := db.Exec(sql.Lookup(db.driver, "repo-update-counter"), new, old, id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("database: update repository counter: %s", err)
|
|
|
|
}
|
|
|
|
updated, err := results.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("database: update repository counter: %s", err)
|
|
|
|
}
|
|
|
|
if updated == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return new, nil
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func (db *datastore) UpdateBuild(build *model.Build) error {
|
2015-10-21 23:14:02 +00:00
|
|
|
return meddler.Update(db, buildTable, build)
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:32:22 +00:00
|
|
|
func (db *datastore) GetBuildCount() (count int, err error) {
|
|
|
|
err = db.QueryRow(
|
|
|
|
sql.Lookup(db.driver, "count-builds"),
|
|
|
|
).Scan(&count)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
const buildTable = "builds"
|
|
|
|
|
|
|
|
const buildListQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
ORDER BY build_number DESC
|
|
|
|
LIMIT 50
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildNumberQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
AND build_number = ?
|
|
|
|
LIMIT 1;
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildLastQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
AND build_branch = ?
|
2015-10-28 18:50:11 +00:00
|
|
|
AND build_event = 'push'
|
2015-10-21 23:14:02 +00:00
|
|
|
ORDER BY build_number DESC
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildLastBeforeQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
AND build_branch = ?
|
|
|
|
AND build_id < ?
|
|
|
|
ORDER BY build_number DESC
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildCommitQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
AND build_commit = ?
|
|
|
|
AND build_branch = ?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildRefQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
AND build_ref = ?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildNumberLast = `
|
|
|
|
SELECT MAX(build_number)
|
|
|
|
FROM builds
|
|
|
|
WHERE build_repo_id = ?
|
|
|
|
`
|
2016-05-11 05:19:45 +00:00
|
|
|
|
|
|
|
const buildQueueList = `
|
|
|
|
SELECT
|
|
|
|
repo_owner
|
|
|
|
,repo_name
|
|
|
|
,repo_full_name
|
|
|
|
,build_number
|
|
|
|
,build_event
|
|
|
|
,build_status
|
|
|
|
,build_created
|
|
|
|
,build_started
|
|
|
|
,build_finished
|
|
|
|
,build_commit
|
|
|
|
,build_branch
|
|
|
|
,build_ref
|
|
|
|
,build_refspec
|
|
|
|
,build_remote
|
|
|
|
,build_title
|
|
|
|
,build_message
|
|
|
|
,build_author
|
|
|
|
,build_email
|
|
|
|
,build_avatar
|
|
|
|
FROM
|
|
|
|
builds b
|
|
|
|
,repos r
|
|
|
|
WHERE b.build_repo_id = r.repo_id
|
|
|
|
AND b.build_status IN ('pending','running')
|
|
|
|
`
|