github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/builds.go (about)

     1  package database
     2  
     3  import (
     4  	. "github.com/drone/drone/pkg/model"
     5  	"github.com/russross/meddler"
     6  )
     7  
     8  // Name of the Build table in the database
     9  const buildTable = "builds"
    10  
    11  // SQL Queries to retrieve a list of all Commits belonging to a Repo.
    12  const buildStmt = `
    13  SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout
    14  FROM builds
    15  WHERE commit_id = ?
    16  ORDER BY slug ASC
    17  `
    18  
    19  // SQL Queries to retrieve a Build by id.
    20  const buildFindStmt = `
    21  SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout
    22  FROM builds
    23  WHERE id = ?
    24  LIMIT 1
    25  `
    26  
    27  // SQL Queries to retrieve a Commit by name and repo id.
    28  const buildFindSlugStmt = `
    29  SELECT id, commit_id, slug, status, started, finished, duration, created, updated, stdout
    30  FROM builds
    31  WHERE slug = ? AND commit_id = ?
    32  LIMIT 1
    33  `
    34  
    35  // SQL Queries to fail all builds that are running or pending
    36  const buildFailUnfinishedStmt = `
    37  UPDATE builds
    38  SET status = 'Failure'
    39  WHERE status IN ('Started', 'Pending')
    40  `
    41  
    42  // SQL Queries to delete a Commit.
    43  const buildDeleteStmt = `
    44  DELETE FROM builds WHERE id = ?
    45  `
    46  
    47  // Returns the Build with the given ID.
    48  func GetBuild(id int64) (*Build, error) {
    49  	build := Build{}
    50  	err := meddler.QueryRow(db, &build, buildFindStmt, id)
    51  	return &build, err
    52  }
    53  
    54  // Returns the Build with the given slug.
    55  func GetBuildSlug(slug string, commit int64) (*Build, error) {
    56  	build := Build{}
    57  	err := meddler.QueryRow(db, &build, buildFindSlugStmt, slug, commit)
    58  	return &build, err
    59  }
    60  
    61  // Creates a new Build.
    62  func SaveBuild(build *Build) error {
    63  	return meddler.Save(db, buildTable, build)
    64  }
    65  
    66  // Deletes an existing Build.
    67  func DeleteBuild(id int64) error {
    68  	_, err := db.Exec(buildDeleteStmt, id)
    69  	return err
    70  }
    71  
    72  // Returns a list of all Builds associated
    73  // with the specified Commit ID and branch.
    74  func ListBuilds(id int64) ([]*Build, error) {
    75  	var builds []*Build
    76  	err := meddler.QueryAll(db, &builds, buildStmt, id)
    77  	return builds, err
    78  }
    79  
    80  // FailUnfinishedBuilds sets status=Failure to all builds
    81  // in the Pending and Started states
    82  func FailUnfinishedBuilds() error {
    83  	_, err := db.Exec(buildFailUnfinishedStmt)
    84  	return err
    85  }