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

     1  package database
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/drone/drone/pkg/model"
     7  	"github.com/russross/meddler"
     8  )
     9  
    10  // Name of the Commit table in the database
    11  const commitTable = "commits"
    12  
    13  // SQL Queries to retrieve a list of all Commits belonging to a Repo.
    14  const commitStmt = `
    15  SELECT id, repo_id, status, started, finished, duration,
    16  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
    17  FROM commits
    18  WHERE repo_id = ? AND branch = ?
    19  ORDER BY created DESC, id DESC
    20  LIMIT 10
    21  `
    22  
    23  // SQL Queries to retrieve the latest Commit.
    24  const commitLatestStmt = `
    25  SELECT id, repo_id, status, started, finished, duration,
    26  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
    27  FROM commits
    28  WHERE repo_id = ? AND branch = ?
    29  ORDER BY created DESC, id DESC
    30  LIMIT 1
    31  `
    32  
    33  // SQL Queries to retrieve a Commit by id.
    34  const commitFindStmt = `
    35  SELECT id, repo_id, status, started, finished, duration,
    36  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
    37  FROM commits
    38  WHERE id = ?
    39  `
    40  
    41  // SQL Queries to retrieve a Commit by hash and repo id.
    42  const commitFindHashStmt = `
    43  SELECT id, repo_id, status, started, finished, duration,
    44  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
    45  FROM commits
    46  WHERE hash = ? AND repo_id = ?
    47  LIMIT 1
    48  `
    49  
    50  // SQL Queries to retrieve a Commit by branch, hash, and repo id.
    51  const commitFindBranchHashStmt = `
    52  SELECT id, repo_id, status, started, finished, duration,
    53  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
    54  FROM commits
    55  WHERE branch = ? AND hash = ? AND repo_id = ?
    56  LIMIT 1
    57  `
    58  
    59  // SQL Query to retrieve a list of recent commits by user.
    60  const userCommitRecentStmt = `
    61  SELECT r.slug, r.host, r.owner, r.name,
    62  c.status, c.started, c.finished, c.duration, c.hash, c.branch, c.pull_request,
    63  c.author, c.gravatar, c.timestamp, c.message, c.created, c.updated
    64  FROM repos r, commits c
    65  WHERE r.user_id = ?
    66  AND   r.team_id = 0
    67  AND   r.id = c.repo_id
    68  AND   c.status IN ('Success', 'Failure')
    69  ORDER BY c.created desc, c.id desc
    70  LIMIT 10
    71  `
    72  
    73  // SQL Query to retrieve a list of recent commits by team.
    74  const teamCommitRecentStmt = `
    75  SELECT r.slug, r.host, r.owner, r.name,
    76  c.status, c.started, c.finished, c.duration, c.hash, c.branch, c.pull_request,
    77  c.author, c.gravatar, c.timestamp, c.message, c.created, c.updated
    78  FROM repos r, commits c
    79  WHERE r.team_id = ?
    80  AND   r.id = c.repo_id
    81  AND   c.status IN ('Success', 'Failure')
    82  ORDER BY c.created desc, c.id desc
    83  LIMIT 10
    84  `
    85  
    86  // SQL Queries to delete a Commit.
    87  const commitDeleteStmt = `
    88  DELETE FROM commits WHERE id = ?
    89  `
    90  
    91  // SQL Queries to retrieve the latest Commits for each branch.
    92  const commitBranchesStmt = `
    93  SELECT id, repo_id, status, started, finished, duration,
    94  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
    95  FROM commits
    96  WHERE id IN (
    97      SELECT MAX(id)
    98      FROM commits
    99      WHERE repo_id = ?
   100      GROUP BY branch)
   101   ORDER BY branch ASC
   102   `
   103  
   104  // SQL Queries to retrieve the latest Commits for each branch.
   105  const commitBranchStmt = `
   106  SELECT id, repo_id, status, started, finished, duration,
   107  hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
   108  FROM commits
   109  WHERE id IN (
   110      SELECT MAX(id)
   111      FROM commits
   112      WHERE repo_id = ?
   113      AND   branch  = ?
   114      GROUP BY branch)
   115  LIMIT 1
   116   `
   117  
   118  // SQL Queries to fail all commits that are currently building
   119  const commitFailUnfinishedStmt = `
   120  UPDATE commits
   121  SET status = 'Failure'
   122  WHERE status IN ('Started', 'Pending')
   123  `
   124  
   125  // Returns the Commit with the given ID.
   126  func GetCommit(id int64) (*Commit, error) {
   127  	commit := Commit{}
   128  	err := meddler.QueryRow(db, &commit, commitFindStmt, id)
   129  	return &commit, err
   130  }
   131  
   132  // Returns the Commit with the given hash.
   133  func GetCommitHash(hash string, repo int64) (*Commit, error) {
   134  	commit := Commit{}
   135  	err := meddler.QueryRow(db, &commit, commitFindHashStmt, hash, repo)
   136  	return &commit, err
   137  }
   138  
   139  // Returns the Commit on the given branch with the given hash.
   140  func GetCommitBranchHash(branch string, hash string, repo int64) (*Commit, error) {
   141  	commit := Commit{}
   142  	err := meddler.QueryRow(db, &commit, commitFindBranchHashStmt, branch, hash, repo)
   143  	return &commit, err
   144  }
   145  
   146  // Returns the most recent Commit for the given branch.
   147  func GetBranch(repo int64, branch string) (*Commit, error) {
   148  	commit := Commit{}
   149  	err := meddler.QueryRow(db, &commit, commitBranchStmt, repo, branch)
   150  	return &commit, err
   151  }
   152  
   153  // Creates a new Commit.
   154  func SaveCommit(commit *Commit) error {
   155  	if commit.ID == 0 {
   156  		commit.Created = time.Now().UTC()
   157  	}
   158  	commit.Updated = time.Now().UTC()
   159  	return meddler.Save(db, commitTable, commit)
   160  }
   161  
   162  // Deletes an existing Commit.
   163  func DeleteCommit(id int64) error {
   164  	_, err := db.Exec(commitDeleteStmt, id)
   165  	return err
   166  }
   167  
   168  // Returns a list of all Commits associated
   169  // with the specified Repo ID.
   170  func ListCommits(repo int64, branch string) ([]*Commit, error) {
   171  	var commits []*Commit
   172  	err := meddler.QueryAll(db, &commits, commitStmt, repo, branch)
   173  	return commits, err
   174  }
   175  
   176  // Returns a list of recent Commits associated
   177  // with the specified User ID
   178  func ListCommitsUser(user int64) ([]*RepoCommit, error) {
   179  	var commits []*RepoCommit
   180  	err := meddler.QueryAll(db, &commits, userCommitRecentStmt, user)
   181  	return commits, err
   182  }
   183  
   184  // Returns a list of recent Commits associated
   185  // with the specified Team ID
   186  func ListCommitsTeam(team int64) ([]*RepoCommit, error) {
   187  	var commits []*RepoCommit
   188  	err := meddler.QueryAll(db, &commits, teamCommitRecentStmt, team)
   189  	return commits, err
   190  }
   191  
   192  // Returns a list of the most recent commits for each branch.
   193  func ListBranches(repo int64) ([]*Commit, error) {
   194  	var commits []*Commit
   195  	err := meddler.QueryAll(db, &commits, commitBranchesStmt, repo)
   196  	return commits, err
   197  }
   198  
   199  func FailUnfinishedCommits() error {
   200  	_, err := db.Exec(commitFailUnfinishedStmt)
   201  	return err
   202  }