mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-04-15 01:41:56 +00:00
removed builds table since not being used in the near term
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
package committest
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
func Load(db *sql.DB) {
|
||||
db.Exec("insert into builds values (1, 2, 1, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
|
||||
db.Exec("insert into builds values (2, 2, 2, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
|
||||
db.Exec("insert into builds values (3, 2, 3, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
|
||||
db.Exec("insert into builds values (4, 1, 1, '', 'Success', 'some output', 1398065345, 1398069999, 854, 1398065343, 1398065344);")
|
||||
db.Exec("insert into builds values (5, 3, 1, '', 'Started', 'some output', 1398065345, 0, 0, 1398065343, 1398065344);")
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/russross/meddler"
|
||||
)
|
||||
|
||||
type BuildManager interface {
|
||||
// Find finds the build by ID.
|
||||
Find(id int64) (*Build, error)
|
||||
|
||||
// FindNumber finds the build with the specified build number.
|
||||
FindNumber(commit, number int64) (*Build, error)
|
||||
|
||||
// FindOutput finds the build's output.
|
||||
FindOutput(commit, number int64) ([]byte, error)
|
||||
|
||||
// List finds all builds for the commit ID.
|
||||
List(commit int64) ([]*Build, error)
|
||||
|
||||
// Insert persists the build to the datastore.
|
||||
Insert(build *Build) error
|
||||
|
||||
// Update persists changes to the build to the datastore.
|
||||
Update(build *Build) error
|
||||
|
||||
// UpdateOutput persists a build's stdout to the datastore.
|
||||
UpdateOutput(build *Build, out []byte) error
|
||||
|
||||
// Delete removes the build from the datastore.
|
||||
Delete(build *Build) error
|
||||
}
|
||||
|
||||
// buildManager manages a list of builds in a SQL database.
|
||||
type buildManager struct {
|
||||
*sql.DB
|
||||
}
|
||||
|
||||
// SQL query to retrieve a list of builds by commit ID.
|
||||
const listQuery = `
|
||||
SELECT build_id, commit_id, build_number, build_matrix, build_status,
|
||||
build_started, build_finished, build_duration, build_created, build_updated
|
||||
FROM builds
|
||||
WHERE commit_id=?
|
||||
ORDER BY build_number ASC
|
||||
`
|
||||
|
||||
// SQL query to retrieve a build by commit ID and build number.
|
||||
const findQuery = `
|
||||
SELECT build_id, commit_id, build_number, build_matrix, build_status,
|
||||
build_started, build_finished, build_duration, build_created, build_updated
|
||||
FROM builds
|
||||
WHERE commit_id=?
|
||||
AND build_number=?
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
// SQL query to retrieve a build's console output by build ID.
|
||||
const findOutputQuery = `
|
||||
SELECT build_console
|
||||
FROM builds
|
||||
WHERE commit_id=?
|
||||
AND build_number=?
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
// SQL statement to update a build's console output.
|
||||
const updateStmt = `
|
||||
UPDATE builds set build_console=? WHERE build_id=?
|
||||
`
|
||||
|
||||
// SQL statement to delete a build by ID.
|
||||
const deleteStmt = `
|
||||
DELETE FROM builds WHERE build_id = ?
|
||||
`
|
||||
|
||||
// NewManager initiales a new BuildManager intended to
|
||||
// manage and persist builds.
|
||||
func NewManager(db *sql.DB) BuildManager {
|
||||
return &buildManager{db}
|
||||
}
|
||||
|
||||
func (db *buildManager) Find(id int64) (*Build, error) {
|
||||
dst := Build{}
|
||||
err := meddler.Load(db, "builds", &dst, id)
|
||||
return &dst, err
|
||||
}
|
||||
|
||||
func (db *buildManager) FindNumber(commit, number int64) (*Build, error) {
|
||||
dst := Build{}
|
||||
err := meddler.QueryRow(db, &dst, findQuery, commit, number)
|
||||
return &dst, err
|
||||
}
|
||||
|
||||
func (db *buildManager) FindOutput(commit, number int64) ([]byte, error) {
|
||||
var dst string
|
||||
err := db.QueryRow(findOutputQuery, commit, number).Scan(&dst)
|
||||
return []byte(dst), err
|
||||
}
|
||||
|
||||
func (db *buildManager) List(commit int64) ([]*Build, error) {
|
||||
var dst []*Build
|
||||
err := meddler.QueryAll(db, &dst, listQuery, commit)
|
||||
return dst, err
|
||||
}
|
||||
|
||||
func (db *buildManager) Insert(build *Build) error {
|
||||
build.Created = time.Now().Unix()
|
||||
build.Updated = time.Now().Unix()
|
||||
return meddler.Insert(db, "builds", build)
|
||||
}
|
||||
|
||||
func (db *buildManager) Update(build *Build) error {
|
||||
build.Updated = time.Now().Unix()
|
||||
return meddler.Update(db, "builds", build)
|
||||
}
|
||||
|
||||
func (db *buildManager) UpdateOutput(build *Build, out []byte) error {
|
||||
_, err := db.Exec(updateStmt, out, build.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *buildManager) Delete(build *Build) error {
|
||||
_, err := db.Exec(deleteStmt, build.ID)
|
||||
return err
|
||||
}
|
||||
@@ -1,248 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/drone/drone/pkg/database"
|
||||
"github.com/drone/drone/pkg/resource/build/buildtest"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// in-memory database instance for unit testing
|
||||
var db *sql.DB
|
||||
|
||||
// setup the test database and test fixtures
|
||||
func setup() {
|
||||
db, _ = sql.Open("sqlite3", ":memory:")
|
||||
database.Load(db)
|
||||
committest.Load(db)
|
||||
}
|
||||
|
||||
// teardown the test database
|
||||
func teardown() {
|
||||
db.Close()
|
||||
}
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
build, err := builds.Find(1)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build from ID, got %s", err)
|
||||
}
|
||||
|
||||
testBuild(t, build)
|
||||
}
|
||||
|
||||
func TestFindNumber(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
build, err := builds.FindNumber(2, 1)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build from Number, got %s", err)
|
||||
}
|
||||
|
||||
testBuild(t, build)
|
||||
}
|
||||
|
||||
func TestFindOutput(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
out, err := builds.FindOutput(2, 1)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build Output from Number, got %s", err)
|
||||
}
|
||||
|
||||
var got, want = string(out), "some output"
|
||||
if got != want {
|
||||
t.Errorf("Want build output %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
list, err := builds.List(2)
|
||||
if err != nil {
|
||||
t.Errorf("Want List from CommitID, got %s", err)
|
||||
}
|
||||
|
||||
var got, want = len(list), 3
|
||||
if got != want {
|
||||
t.Errorf("Want List size %v, got %v", want, got)
|
||||
}
|
||||
|
||||
testBuild(t, list[0])
|
||||
}
|
||||
|
||||
func TestInsert(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
if err := builds.Insert(&Build{CommitID: 4, Number: 1}); err != nil {
|
||||
t.Errorf("Want Build created, got %s", err)
|
||||
}
|
||||
|
||||
// verify that it is ok to add same commit ID for incremented build number
|
||||
if err := builds.Insert(&Build{CommitID: 4, Number: 2}); err != nil {
|
||||
t.Errorf("Want Build created, got %s", err)
|
||||
}
|
||||
|
||||
// verify that unique constraint fails when commit ID and build number already exist
|
||||
err := builds.Insert(&Build{CommitID: 4, Number: 2})
|
||||
if err == nil || !strings.Contains(err.Error(), "commit_id, build_number are not unique") {
|
||||
t.Errorf("Want unique constraint violated, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
build, err := builds.Find(5)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build from ID, got %s", err)
|
||||
}
|
||||
|
||||
// update the build's access token
|
||||
build.Status = "Success"
|
||||
build.Finished = time.Now().Unix()
|
||||
build.Duration = 999
|
||||
if err := builds.Update(build); err != nil {
|
||||
t.Errorf("Want Build updated, got %s", err)
|
||||
}
|
||||
|
||||
updated, _ := builds.Find(5)
|
||||
var got, want = updated.Status, "Success"
|
||||
if got != want {
|
||||
t.Errorf("Want updated Status %v, got %v", want, got)
|
||||
}
|
||||
|
||||
var gotInt64, wantInt64 = updated.ID, build.ID
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want build ID %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = updated.Duration, build.Duration
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want updated Duration %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = updated.Finished, build.Finished
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want updated Finished %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindUpdateOutput(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
build, err := builds.Find(5)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build from ID, got %s", err)
|
||||
}
|
||||
|
||||
if err := builds.UpdateOutput(build, []byte("some output ...")); err != nil {
|
||||
t.Errorf("Want Build updated, got %s", err)
|
||||
}
|
||||
|
||||
out, err := builds.FindOutput(build.CommitID, build.Number)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build Output, got %s", err)
|
||||
}
|
||||
|
||||
var got, want = string(out), "some output ..."
|
||||
if got != want {
|
||||
t.Errorf("Want Build Output %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
builds := NewManager(db)
|
||||
build, err := builds.Find(1)
|
||||
if err != nil {
|
||||
t.Errorf("Want Build from ID, got %s", err)
|
||||
}
|
||||
|
||||
// delete the builds
|
||||
if err := builds.Delete(build); err != nil {
|
||||
t.Errorf("Want Build deleted, got %s", err)
|
||||
}
|
||||
|
||||
// check to see if the deleted build is actually gone
|
||||
if _, err := builds.Find(1); err != sql.ErrNoRows {
|
||||
t.Errorf("Want ErrNoRows, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// testBuild is a helper function that compares the build
|
||||
// to an expected set of fixed field values.
|
||||
func testBuild(t *testing.T, build *Build) {
|
||||
var got, want = build.Status, "Success"
|
||||
if got != want {
|
||||
t.Errorf("Want Status %v, got %v", want, got)
|
||||
}
|
||||
|
||||
got, want = build.Matrix, ""
|
||||
if got != want {
|
||||
t.Errorf("Want Matrix %v, got %v", want, got)
|
||||
}
|
||||
|
||||
var gotInt64, wantInt64 = build.ID, int64(1)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want ID %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.CommitID, int64(2)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want CommitID %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.Number, int64(1)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want Number %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.Created, int64(1398065343)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want Created %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.Updated, int64(1398065344)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want Updated %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.Started, int64(1398065345)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want Started %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.Finished, int64(1398069999)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want Finished %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
|
||||
gotInt64, wantInt64 = build.Duration, int64(854)
|
||||
if gotInt64 != wantInt64 {
|
||||
t.Errorf("Want Duration %v, got %v", wantInt64, gotInt64)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package build
|
||||
|
||||
const (
|
||||
StatusNone = "None"
|
||||
StatusEnqueue = "Pending"
|
||||
StatusStarted = "Started"
|
||||
StatusSuccess = "Success"
|
||||
StatusFailure = "Failure"
|
||||
StatusError = "Error"
|
||||
)
|
||||
|
||||
type Build struct {
|
||||
ID int64 `meddler:"build_id,pk" json:"id"`
|
||||
CommitID int64 `meddler:"commit_id" json:"-"`
|
||||
Number int64 `meddler:"build_number" json:"number"`
|
||||
Matrix string `meddler:"build_matrix" json:"matrix"`
|
||||
Status string `meddler:"build_status" json:"status"`
|
||||
Started int64 `meddler:"build_started" json:"started_at"`
|
||||
Finished int64 `meddler:"build_finished" json:"finished_at"`
|
||||
Duration int64 `meddler:"build_duration" json:"duration"`
|
||||
Created int64 `meddler:"build_created" json:"created_at"`
|
||||
Updated int64 `meddler:"build_updated" json:"updated_at"`
|
||||
}
|
||||
|
||||
// IsRunning returns true if the Build statis is Started
|
||||
// or Pending, indicating it is currently running.
|
||||
func (b *Build) IsRunning() bool {
|
||||
return (b.Status == StatusStarted || b.Status == StatusEnqueue)
|
||||
}
|
||||
@@ -18,7 +18,7 @@ type CommitManager interface {
|
||||
FindLatest(repo int64, branch string) (*Commit, error)
|
||||
|
||||
// FindOutput finds the commit's output.
|
||||
//FindOutput(id int64) ([]byte, error)
|
||||
FindOutput(commit int64) ([]byte, error)
|
||||
|
||||
// List finds recent commits for the repository
|
||||
List(repo int64) ([]*Commit, error)
|
||||
@@ -39,7 +39,7 @@ type CommitManager interface {
|
||||
Update(commit *Commit) error
|
||||
|
||||
// UpdateOutput persists a commit's stdout to the datastore.
|
||||
//UpdateOutput(commit *Commit, out []byte) error
|
||||
UpdateOutput(commit *Commit, out []byte) error
|
||||
|
||||
// Delete removes the commit from the datastore.
|
||||
Delete(commit *Commit) error
|
||||
@@ -121,6 +121,23 @@ WHERE commit_id IN (
|
||||
AND commit_branch=?)
|
||||
`
|
||||
|
||||
// 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 = ?;
|
||||
`
|
||||
|
||||
// SQL statement to delete a Commit by ID.
|
||||
const deleteStmt = `
|
||||
DELETE FROM commits WHERE commit_id = ?
|
||||
@@ -144,6 +161,12 @@ func (db *commitManager) FindLatest(repo int64, branch string) (*Commit, error)
|
||||
return &dst, err
|
||||
}
|
||||
|
||||
func (db *commitManager) FindOutput(commit int64) ([]byte, error) {
|
||||
var dst string
|
||||
err := db.QueryRow(findOutputQuery, commit).Scan(&dst)
|
||||
return []byte(dst), err
|
||||
}
|
||||
|
||||
func (db *commitManager) List(repo int64) ([]*Commit, error) {
|
||||
var dst []*Commit
|
||||
err := meddler.QueryAll(db, &dst, listQuery, repo)
|
||||
@@ -179,6 +202,15 @@ func (db *commitManager) Update(commit *Commit) error {
|
||||
return meddler.Update(db, "commits", commit)
|
||||
}
|
||||
|
||||
func (db *commitManager) UpdateOutput(commit *Commit, out []byte) error {
|
||||
_, err := db.Exec(insertOutputStmt, commit.ID, out)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
_, err = db.Exec(updateOutputStmt, out, commit.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *commitManager) Delete(commit *Commit) error {
|
||||
_, err := db.Exec(deleteStmt, commit.ID)
|
||||
return err
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/drone/drone/pkg/database"
|
||||
"github.com/drone/drone/pkg/resource/commit/committest"
|
||||
"github.com/drone/drone/server/database"
|
||||
"github.com/drone/drone/server/database/testdata"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ var db *sql.DB
|
||||
func setup() {
|
||||
db, _ = sql.Open("sqlite3", ":memory:")
|
||||
database.Load(db)
|
||||
committest.Load(db)
|
||||
testdata.Load(db)
|
||||
}
|
||||
|
||||
// teardown the test database
|
||||
@@ -65,6 +65,22 @@ func TestFindLatest(t *testing.T) {
|
||||
testCommit(t, commit)
|
||||
}
|
||||
|
||||
func TestFindOutput(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
commits := NewManager(db)
|
||||
out, err := commits.FindOutput(1)
|
||||
if err != nil {
|
||||
t.Errorf("Want Commit stdout, got %s", err)
|
||||
}
|
||||
|
||||
var want, got = "sample console output", string(out)
|
||||
if want != got {
|
||||
t.Errorf("Want stdout %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
//"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone/pkg/database"
|
||||
"github.com/drone/drone/pkg/resource/perm/permdata"
|
||||
"github.com/drone/drone/pkg/resource/repo"
|
||||
"github.com/drone/drone/pkg/resource/user"
|
||||
"github.com/drone/drone/server/database"
|
||||
"github.com/drone/drone/server/database/testdata"
|
||||
"github.com/drone/drone/server/resource/repo"
|
||||
"github.com/drone/drone/server/resource/user"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
@@ -19,7 +19,7 @@ var db *sql.DB
|
||||
func setup() {
|
||||
db, _ = sql.Open("sqlite3", ":memory:")
|
||||
database.Load(db)
|
||||
permdata.Load(db)
|
||||
testdata.Load(db)
|
||||
}
|
||||
|
||||
// teardown the test database
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package permdata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
func Load(db *sql.DB) {
|
||||
db.Exec("insert into perms values (1, 101, 200, 1, 1, 1, 1398065343, 1398065344);")
|
||||
db.Exec("insert into perms values (2, 102, 200, 1, 1, 0, 1398065343, 1398065344);")
|
||||
db.Exec("insert into perms values (3, 103, 200, 1, 0, 0, 1398065343, 1398065344);")
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone/pkg/database"
|
||||
"github.com/drone/drone/pkg/database/testdata"
|
||||
"github.com/drone/drone/server/database"
|
||||
"github.com/drone/drone/server/database/testdata"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user