2014-02-07 10:10:01 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/drone/drone/pkg/model"
|
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Name of the Build table in the database
|
|
|
|
const buildTable = "builds"
|
|
|
|
|
|
|
|
// SQL Queries to retrieve a list of all Commits belonging to a Repo.
|
|
|
|
const buildStmt = `
|
2014-05-23 03:16:01 +00:00
|
|
|
SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout, buildscript
|
2014-02-07 10:10:01 +00:00
|
|
|
FROM builds
|
|
|
|
WHERE commit_id = ?
|
|
|
|
ORDER BY slug ASC
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL Queries to retrieve a Build by id.
|
|
|
|
const buildFindStmt = `
|
2014-05-23 03:16:01 +00:00
|
|
|
SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout, buildscript
|
2014-02-07 10:10:01 +00:00
|
|
|
FROM builds
|
|
|
|
WHERE id = ?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL Queries to retrieve a Commit by name and repo id.
|
|
|
|
const buildFindSlugStmt = `
|
2014-05-23 03:16:01 +00:00
|
|
|
SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout, buildscript
|
2014-02-07 10:10:01 +00:00
|
|
|
FROM builds
|
|
|
|
WHERE slug = ? AND commit_id = ?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
2014-04-01 19:29:14 +00:00
|
|
|
// SQL Queries to fail all builds that are running or pending
|
|
|
|
const buildFailUnfinishedStmt = `
|
2014-03-12 04:30:00 +00:00
|
|
|
UPDATE builds
|
|
|
|
SET status = 'Failure'
|
2014-04-01 19:29:14 +00:00
|
|
|
WHERE status IN ('Started', 'Pending')
|
2014-03-24 04:21:33 +00:00
|
|
|
`
|
|
|
|
|
2014-02-07 10:10:01 +00:00
|
|
|
// SQL Queries to delete a Commit.
|
|
|
|
const buildDeleteStmt = `
|
|
|
|
DELETE FROM builds WHERE id = ?
|
|
|
|
`
|
|
|
|
|
|
|
|
// Returns the Build with the given ID.
|
|
|
|
func GetBuild(id int64) (*Build, error) {
|
|
|
|
build := Build{}
|
|
|
|
err := meddler.QueryRow(db, &build, buildFindStmt, id)
|
|
|
|
return &build, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Build with the given slug.
|
|
|
|
func GetBuildSlug(slug string, commit int64) (*Build, error) {
|
|
|
|
build := Build{}
|
|
|
|
err := meddler.QueryRow(db, &build, buildFindSlugStmt, slug, commit)
|
|
|
|
return &build, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new Build.
|
|
|
|
func SaveBuild(build *Build) error {
|
|
|
|
return meddler.Save(db, buildTable, build)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes an existing Build.
|
|
|
|
func DeleteBuild(id int64) error {
|
|
|
|
_, err := db.Exec(buildDeleteStmt, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of all Builds associated
|
|
|
|
// with the specified Commit ID and branch.
|
|
|
|
func ListBuilds(id int64) ([]*Build, error) {
|
|
|
|
var builds []*Build
|
|
|
|
err := meddler.QueryAll(db, &builds, buildStmt, id)
|
|
|
|
return builds, err
|
|
|
|
}
|
2014-03-12 04:30:00 +00:00
|
|
|
|
2014-04-01 19:29:14 +00:00
|
|
|
// FailUnfinishedBuilds sets status=Failure to all builds
|
|
|
|
// in the Pending and Started states
|
2014-03-24 04:21:33 +00:00
|
|
|
func FailUnfinishedBuilds() error {
|
2014-04-01 19:29:14 +00:00
|
|
|
_, err := db.Exec(buildFailUnfinishedStmt)
|
2014-03-12 04:30:00 +00:00
|
|
|
return err
|
|
|
|
}
|