Merge pull request #1539 from bradrydzewski/master

backported 0.5 database restructure to 0.4.1
This commit is contained in:
Brad Rydzewski 2016-03-25 13:51:08 -07:00
commit 98ea6883f9
34 changed files with 483 additions and 659 deletions

View file

@ -31,7 +31,7 @@ gen_template:
go generate github.com/drone/drone/template
gen_migrations:
go generate github.com/drone/drone/store/migration
go generate github.com/drone/drone/store/datastore/ddl
build:
go build

View file

@ -38,7 +38,7 @@ func GetLogin(c *gin.Context) {
// get the user from the database
u, err := store.GetUserLogin(c, tmpuser.Login)
if err != nil {
count, err := store.CountUsers(c)
count, err := store.GetUserCount(c)
if err != nil {
log.Errorf("cannot register %s. %s", tmpuser.Login, err)
c.Redirect(303, "/login?error=internal_error")

View file

@ -76,7 +76,7 @@ func Load(env envconfig.Env, s store.Store) Engine {
}
}
nodes, err := s.Nodes().GetList()
nodes, err := s.GetNodeList()
if err != nil {
log.Fatalf("failed to get nodes from database. %s", err)
}

View file

@ -1,71 +0,0 @@
package store
import (
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type BuildStore interface {
// Get gets a build by unique ID.
Get(int64) (*model.Build, error)
// GetNumber gets a build by number.
GetNumber(*model.Repo, int) (*model.Build, error)
// GetRef gets a build by its ref.
GetRef(*model.Repo, string) (*model.Build, error)
// GetCommit gets a build by its commit sha.
GetCommit(*model.Repo, string, string) (*model.Build, error)
// GetLast gets the last build for the branch.
GetLast(*model.Repo, string) (*model.Build, error)
// GetLastBefore gets the last build before build number N.
GetLastBefore(*model.Repo, string, int64) (*model.Build, error)
// GetList gets a list of builds for the repository
GetList(*model.Repo) ([]*model.Build, error)
// Create creates a new build and jobs.
Create(*model.Build, ...*model.Job) error
// Update updates a build.
Update(*model.Build) error
}
func GetBuild(c context.Context, id int64) (*model.Build, error) {
return FromContext(c).Builds().Get(id)
}
func GetBuildNumber(c context.Context, repo *model.Repo, num int) (*model.Build, error) {
return FromContext(c).Builds().GetNumber(repo, num)
}
func GetBuildRef(c context.Context, repo *model.Repo, ref string) (*model.Build, error) {
return FromContext(c).Builds().GetRef(repo, ref)
}
func GetBuildCommit(c context.Context, repo *model.Repo, sha, branch string) (*model.Build, error) {
return FromContext(c).Builds().GetCommit(repo, sha, branch)
}
func GetBuildLast(c context.Context, repo *model.Repo, branch string) (*model.Build, error) {
return FromContext(c).Builds().GetLast(repo, branch)
}
func GetBuildLastBefore(c context.Context, repo *model.Repo, branch string, number int64) (*model.Build, error) {
return FromContext(c).Builds().GetLastBefore(repo, branch, number)
}
func GetBuildList(c context.Context, repo *model.Repo) ([]*model.Build, error) {
return FromContext(c).Builds().GetList(repo)
}
func CreateBuild(c context.Context, build *model.Build, jobs ...*model.Job) error {
return FromContext(c).Builds().Create(build, jobs...)
}
func UpdateBuild(c context.Context, build *model.Build) error {
return FromContext(c).Builds().Update(build)
}

View file

@ -1,60 +1,55 @@
package datastore
import (
"database/sql"
"time"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type buildstore struct {
*sql.DB
}
func (db *buildstore) Get(id int64) (*model.Build, error) {
func (db *datastore) GetBuild(id int64) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.Load(db, buildTable, build, id)
return build, err
}
func (db *buildstore) GetNumber(repo *model.Repo, num int) (*model.Build, error) {
func (db *datastore) GetBuildNumber(repo *model.Repo, num int) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildNumberQuery), repo.ID, num)
return build, err
}
func (db *buildstore) GetRef(repo *model.Repo, ref string) (*model.Build, error) {
func (db *datastore) GetBuildRef(repo *model.Repo, ref string) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildRefQuery), repo.ID, ref)
return build, err
}
func (db *buildstore) GetCommit(repo *model.Repo, sha, branch string) (*model.Build, error) {
func (db *datastore) GetBuildCommit(repo *model.Repo, sha, branch string) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildCommitQuery), repo.ID, sha, branch)
return build, err
}
func (db *buildstore) GetLast(repo *model.Repo, branch string) (*model.Build, error) {
func (db *datastore) GetBuildLast(repo *model.Repo, branch string) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildLastQuery), repo.ID, branch)
return build, err
}
func (db *buildstore) GetLastBefore(repo *model.Repo, branch string, num int64) (*model.Build, error) {
func (db *datastore) GetBuildLastBefore(repo *model.Repo, branch string, num int64) (*model.Build, error) {
var build = new(model.Build)
var err = meddler.QueryRow(db, build, rebind(buildLastBeforeQuery), repo.ID, branch, num)
return build, err
}
func (db *buildstore) GetList(repo *model.Repo) ([]*model.Build, error) {
func (db *datastore) GetBuildList(repo *model.Repo) ([]*model.Build, error) {
var builds = []*model.Build{}
var err = meddler.QueryAll(db, &builds, rebind(buildListQuery), repo.ID)
return builds, err
}
func (db *buildstore) Create(build *model.Build, jobs ...*model.Job) error {
func (db *datastore) CreateBuild(build *model.Build, jobs ...*model.Job) error {
var number int
db.QueryRow(rebind(buildNumberLast), build.RepoID).Scan(&number)
build.Number = number + 1
@ -76,7 +71,7 @@ func (db *buildstore) Create(build *model.Build, jobs ...*model.Job) error {
return nil
}
func (db *buildstore) Update(build *model.Build) error {
func (db *datastore) UpdateBuild(build *model.Build) error {
return meddler.Update(db, buildTable, build)
}

View file

@ -7,7 +7,7 @@ import (
"github.com/franela/goblin"
)
func Test_buildstore(t *testing.T) {
func TestBuilds(t *testing.T) {
db := openTest()
defer db.Close()
@ -28,7 +28,7 @@ func Test_buildstore(t *testing.T) {
Status: model.StatusSuccess,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
err := s.Builds().Create(&build, []*model.Job{}...)
err := s.CreateBuild(&build, []*model.Job{}...)
g.Assert(err == nil).IsTrue()
g.Assert(build.ID != 0).IsTrue()
g.Assert(build.Number).Equal(1)
@ -42,10 +42,10 @@ func Test_buildstore(t *testing.T) {
Status: model.StatusSuccess,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
s.Builds().Create(&build, []*model.Job{}...)
s.CreateBuild(&build, []*model.Job{}...)
build.Status = model.StatusRunning
err1 := s.Builds().Update(&build)
getbuild, err2 := s.Builds().Get(build.ID)
err1 := s.UpdateBuild(&build)
getbuild, err2 := s.GetBuild(build.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(build.ID).Equal(getbuild.ID)
@ -59,8 +59,8 @@ func Test_buildstore(t *testing.T) {
RepoID: 1,
Status: model.StatusSuccess,
}
s.Builds().Create(&build, []*model.Job{}...)
getbuild, err := s.Builds().Get(build.ID)
s.CreateBuild(&build, []*model.Job{}...)
getbuild, err := s.GetBuild(build.ID)
g.Assert(err == nil).IsTrue()
g.Assert(build.ID).Equal(getbuild.ID)
g.Assert(build.RepoID).Equal(getbuild.RepoID)
@ -76,9 +76,9 @@ func Test_buildstore(t *testing.T) {
RepoID: 1,
Status: model.StatusPending,
}
err1 := s.Builds().Create(build1, []*model.Job{}...)
err2 := s.Builds().Create(build2, []*model.Job{}...)
getbuild, err3 := s.Builds().GetNumber(&model.Repo{ID: 1}, build2.Number)
err1 := s.CreateBuild(build1, []*model.Job{}...)
err2 := s.CreateBuild(build2, []*model.Job{}...)
getbuild, err3 := s.GetBuildNumber(&model.Repo{ID: 1}, build2.Number)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -98,9 +98,9 @@ func Test_buildstore(t *testing.T) {
Status: model.StatusPending,
Ref: "refs/pull/6",
}
err1 := s.Builds().Create(build1, []*model.Job{}...)
err2 := s.Builds().Create(build2, []*model.Job{}...)
getbuild, err3 := s.Builds().GetRef(&model.Repo{ID: 1}, "refs/pull/6")
err1 := s.CreateBuild(build1, []*model.Job{}...)
err2 := s.CreateBuild(build2, []*model.Job{}...)
getbuild, err3 := s.GetBuildRef(&model.Repo{ID: 1}, "refs/pull/6")
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -121,9 +121,9 @@ func Test_buildstore(t *testing.T) {
Status: model.StatusPending,
Ref: "refs/pull/6",
}
err1 := s.Builds().Create(build1, []*model.Job{}...)
err2 := s.Builds().Create(build2, []*model.Job{}...)
getbuild, err3 := s.Builds().GetRef(&model.Repo{ID: 1}, "refs/pull/6")
err1 := s.CreateBuild(build1, []*model.Job{}...)
err2 := s.CreateBuild(build2, []*model.Job{}...)
getbuild, err3 := s.GetBuildRef(&model.Repo{ID: 1}, "refs/pull/6")
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -146,9 +146,9 @@ func Test_buildstore(t *testing.T) {
Branch: "dev",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
}
err1 := s.Builds().Create(build1, []*model.Job{}...)
err2 := s.Builds().Create(build2, []*model.Job{}...)
getbuild, err3 := s.Builds().GetCommit(&model.Repo{ID: 1}, build2.Commit, build2.Branch)
err1 := s.CreateBuild(build1, []*model.Job{}...)
err2 := s.CreateBuild(build2, []*model.Job{}...)
getbuild, err3 := s.GetBuildCommit(&model.Repo{ID: 1}, build2.Commit, build2.Branch)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -174,9 +174,9 @@ func Test_buildstore(t *testing.T) {
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
Event: model.EventPush,
}
err1 := s.Builds().Create(build1, []*model.Job{}...)
err2 := s.Builds().Create(build2, []*model.Job{}...)
getbuild, err3 := s.Builds().GetLast(&model.Repo{ID: 1}, build2.Branch)
err1 := s.CreateBuild(build1, []*model.Job{}...)
err2 := s.CreateBuild(build2, []*model.Job{}...)
getbuild, err3 := s.GetBuildLast(&model.Repo{ID: 1}, build2.Branch)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -207,10 +207,10 @@ func Test_buildstore(t *testing.T) {
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
}
err1 := s.Builds().Create(build1, []*model.Job{}...)
err2 := s.Builds().Create(build2, []*model.Job{}...)
err3 := s.Builds().Create(build3, []*model.Job{}...)
getbuild, err4 := s.Builds().GetLastBefore(&model.Repo{ID: 1}, build3.Branch, build3.ID)
err1 := s.CreateBuild(build1, []*model.Job{}...)
err2 := s.CreateBuild(build2, []*model.Job{}...)
err3 := s.CreateBuild(build3, []*model.Job{}...)
getbuild, err4 := s.GetBuildLastBefore(&model.Repo{ID: 1}, build3.Branch, build3.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -232,9 +232,9 @@ func Test_buildstore(t *testing.T) {
RepoID: 1,
Status: model.StatusSuccess,
}
s.Builds().Create(build1, []*model.Job{}...)
s.Builds().Create(build2, []*model.Job{}...)
builds, err := s.Builds().GetList(&model.Repo{ID: 1})
s.CreateBuild(build1, []*model.Job{}...)
s.CreateBuild(build2, []*model.Job{}...)
builds, err := s.GetBuildList(&model.Repo{ID: 1})
g.Assert(err == nil).IsTrue()
g.Assert(len(builds)).Equal(2)
g.Assert(builds[0].ID).Equal(build2.ID)

View file

@ -0,0 +1,3 @@
package ddl
//go:generate go-bindata -pkg ddl -o ddl_gen.go sqlite3/ mysql/ postgres/

View file

@ -1,39 +1,33 @@
package datastore
import (
"database/sql"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type jobstore struct {
*sql.DB
}
func (db *jobstore) Get(id int64) (*model.Job, error) {
func (db *datastore) GetJob(id int64) (*model.Job, error) {
var job = new(model.Job)
var err = meddler.Load(db, jobTable, job, id)
return job, err
}
func (db *jobstore) GetNumber(build *model.Build, num int) (*model.Job, error) {
func (db *datastore) GetJobNumber(build *model.Build, num int) (*model.Job, error) {
var job = new(model.Job)
var err = meddler.QueryRow(db, job, rebind(jobNumberQuery), build.ID, num)
return job, err
}
func (db *jobstore) GetList(build *model.Build) ([]*model.Job, error) {
func (db *datastore) GetJobList(build *model.Build) ([]*model.Job, error) {
var jobs = []*model.Job{}
var err = meddler.QueryAll(db, &jobs, rebind(jobListQuery), build.ID)
return jobs, err
}
func (db *jobstore) Create(job *model.Job) error {
func (db *datastore) CreateJob(job *model.Job) error {
return meddler.Insert(db, jobTable, job)
}
func (db *jobstore) Update(job *model.Job) error {
func (db *datastore) UpdateJob(job *model.Job) error {
return meddler.Update(db, jobTable, job)
}

View file

@ -7,7 +7,7 @@ import (
"github.com/franela/goblin"
)
func Test_jobstore(t *testing.T) {
func TestJobs(t *testing.T) {
db := openTest()
defer db.Close()
@ -28,15 +28,15 @@ func Test_jobstore(t *testing.T) {
ExitCode: 0,
Number: 1,
}
err1 := s.Jobs().Create(job)
err1 := s.CreateJob(job)
g.Assert(err1 == nil).IsTrue()
g.Assert(job.ID != 0).IsTrue()
job.Status = "started"
err2 := s.Jobs().Update(job)
err2 := s.UpdateJob(job)
g.Assert(err2 == nil).IsTrue()
getjob, err3 := s.Jobs().Get(job.ID)
getjob, err3 := s.GetJob(job.ID)
g.Assert(err3 == nil).IsTrue()
g.Assert(getjob.Status).Equal(job.Status)
})
@ -49,11 +49,11 @@ func Test_jobstore(t *testing.T) {
Number: 1,
Environment: map[string]string{"foo": "bar"},
}
err1 := s.Jobs().Create(job)
err1 := s.CreateJob(job)
g.Assert(err1 == nil).IsTrue()
g.Assert(job.ID != 0).IsTrue()
getjob, err2 := s.Jobs().Get(job.ID)
getjob, err2 := s.GetJob(job.ID)
g.Assert(err2 == nil).IsTrue()
g.Assert(getjob.ID).Equal(job.ID)
g.Assert(getjob.Status).Equal(job.Status)
@ -69,11 +69,11 @@ func Test_jobstore(t *testing.T) {
ExitCode: 1,
Number: 1,
}
err1 := s.Jobs().Create(job)
err1 := s.CreateJob(job)
g.Assert(err1 == nil).IsTrue()
g.Assert(job.ID != 0).IsTrue()
getjob, err2 := s.Jobs().GetNumber(&model.Build{ID: 1}, 1)
getjob, err2 := s.GetJobNumber(&model.Build{ID: 1}, 1)
g.Assert(err2 == nil).IsTrue()
g.Assert(getjob.ID).Equal(job.ID)
g.Assert(getjob.Status).Equal(job.Status)
@ -106,9 +106,9 @@ func Test_jobstore(t *testing.T) {
},
}
err1 := s.Builds().Create(&build, jobs...)
err1 := s.CreateBuild(&build, jobs...)
g.Assert(err1 == nil).IsTrue()
getjobs, err2 := s.Jobs().GetList(&build)
getjobs, err2 := s.GetJobList(&build)
g.Assert(err2 == nil).IsTrue()
g.Assert(len(getjobs)).Equal(3)
g.Assert(getjobs[0].Number).Equal(1)

View file

@ -1,31 +1,25 @@
package datastore
import (
"database/sql"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type keystore struct {
*sql.DB
}
func (db *keystore) Get(repo *model.Repo) (*model.Key, error) {
func (db *datastore) GetKey(repo *model.Repo) (*model.Key, error) {
var key = new(model.Key)
var err = meddler.QueryRow(db, key, rebind(keyQuery), repo.ID)
return key, err
}
func (db *keystore) Create(key *model.Key) error {
func (db *datastore) CreateKey(key *model.Key) error {
return meddler.Save(db, keyTable, key)
}
func (db *keystore) Update(key *model.Key) error {
func (db *datastore) UpdateKey(key *model.Key) error {
return meddler.Save(db, keyTable, key)
}
func (db *keystore) Delete(key *model.Key) error {
func (db *datastore) DeleteKey(key *model.Key) error {
var _, err = db.Exec(rebind(keyDeleteStmt), key.ID)
return err
}

View file

@ -7,7 +7,7 @@ import (
"github.com/franela/goblin"
)
func Test_keystore(t *testing.T) {
func TestKeys(t *testing.T) {
db := openTest()
defer db.Close()
@ -27,7 +27,7 @@ func Test_keystore(t *testing.T) {
Public: fakePublicKey,
Private: fakePrivateKey,
}
err := s.Keys().Create(&key)
err := s.CreateKey(&key)
g.Assert(err == nil).IsTrue()
g.Assert(key.ID != 0).IsTrue()
})
@ -38,15 +38,15 @@ func Test_keystore(t *testing.T) {
Public: fakePublicKey,
Private: fakePrivateKey,
}
err := s.Keys().Create(&key)
err := s.CreateKey(&key)
g.Assert(err == nil).IsTrue()
g.Assert(key.ID != 0).IsTrue()
key.Private = ""
key.Public = ""
err1 := s.Keys().Update(&key)
getkey, err2 := s.Keys().Get(&model.Repo{ID: 1})
err1 := s.UpdateKey(&key)
getkey, err2 := s.GetKey(&model.Repo{ID: 1})
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(key.ID).Equal(getkey.ID)
@ -60,11 +60,11 @@ func Test_keystore(t *testing.T) {
Public: fakePublicKey,
Private: fakePrivateKey,
}
err := s.Keys().Create(&key)
err := s.CreateKey(&key)
g.Assert(err == nil).IsTrue()
g.Assert(key.ID != 0).IsTrue()
getkey, err := s.Keys().Get(&model.Repo{ID: 1})
getkey, err := s.GetKey(&model.Repo{ID: 1})
g.Assert(err == nil).IsTrue()
g.Assert(key.ID).Equal(getkey.ID)
g.Assert(key.Public).Equal(getkey.Public)
@ -77,12 +77,12 @@ func Test_keystore(t *testing.T) {
Public: fakePublicKey,
Private: fakePrivateKey,
}
err1 := s.Keys().Create(&key)
err2 := s.Keys().Delete(&key)
err1 := s.CreateKey(&key)
err2 := s.DeleteKey(&key)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
_, err := s.Keys().Get(&model.Repo{ID: 1})
_, err := s.GetKey(&model.Repo{ID: 1})
g.Assert(err == nil).IsFalse()
})
})

View file

@ -2,7 +2,6 @@ package datastore
import (
"bytes"
"database/sql"
"io"
"io/ioutil"
@ -10,18 +9,14 @@ import (
"github.com/russross/meddler"
)
type logstore struct {
*sql.DB
}
func (db *logstore) Read(job *model.Job) (io.ReadCloser, error) {
func (db *datastore) ReadLog(job *model.Job) (io.ReadCloser, error) {
var log = new(model.Log)
var err = meddler.QueryRow(db, log, rebind(logQuery), job.ID)
var buf = bytes.NewBuffer(log.Data)
return ioutil.NopCloser(buf), err
}
func (db *logstore) Write(job *model.Job, r io.Reader) error {
func (db *datastore) WriteLog(job *model.Job, r io.Reader) error {
var log = new(model.Log)
var err = meddler.QueryRow(db, log, rebind(logQuery), job.ID)
if err != nil {

View file

@ -9,7 +9,7 @@ import (
"github.com/franela/goblin"
)
func Test_logstore(t *testing.T) {
func TestLogs(t *testing.T) {
db := openTest()
defer db.Close()
@ -28,10 +28,10 @@ func Test_logstore(t *testing.T) {
ID: 1,
}
buf := bytes.NewBufferString("echo hi")
err := s.Logs().Write(&job, buf)
err := s.WriteLog(&job, buf)
g.Assert(err == nil).IsTrue()
rc, err := s.Logs().Read(&job)
rc, err := s.ReadLog(&job)
g.Assert(err == nil).IsTrue()
defer rc.Close()
out, _ := ioutil.ReadAll(rc)
@ -44,12 +44,12 @@ func Test_logstore(t *testing.T) {
}
buf1 := bytes.NewBufferString("echo hi")
buf2 := bytes.NewBufferString("echo allo?")
err1 := s.Logs().Write(&job, buf1)
err2 := s.Logs().Write(&job, buf2)
err1 := s.WriteLog(&job, buf1)
err2 := s.WriteLog(&job, buf2)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
rc, err := s.Logs().Read(&job)
rc, err := s.ReadLog(&job)
g.Assert(err == nil).IsTrue()
defer rc.Close()
out, _ := ioutil.ReadAll(rc)

View file

@ -1,43 +1,31 @@
package datastore
import (
"database/sql"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type nodestore struct {
*sql.DB
}
func (db *nodestore) Get(id int64) (*model.Node, error) {
func (db *datastore) GetNode(id int64) (*model.Node, error) {
var node = new(model.Node)
var err = meddler.Load(db, nodeTable, node, id)
return node, err
}
func (db *nodestore) GetList() ([]*model.Node, error) {
func (db *datastore) GetNodeList() ([]*model.Node, error) {
var nodes = []*model.Node{}
var err = meddler.QueryAll(db, &nodes, rebind(nodeListQuery))
return nodes, err
}
func (db *nodestore) Count() (int, error) {
var count int
var err = db.QueryRow(rebind(nodeCountQuery)).Scan(&count)
return count, err
}
func (db *nodestore) Create(node *model.Node) error {
func (db *datastore) CreateNode(node *model.Node) error {
return meddler.Insert(db, nodeTable, node)
}
func (db *nodestore) Update(node *model.Node) error {
func (db *datastore) UpdateNode(node *model.Node) error {
return meddler.Update(db, nodeTable, node)
}
func (db *nodestore) Delete(node *model.Node) error {
func (db *datastore) DeleteNode(node *model.Node) error {
var _, err = db.Exec(rebind(nodeDeleteStmt), node.ID)
return err
}

View file

@ -7,11 +7,11 @@ import (
"github.com/franela/goblin"
)
func Test_nodestore(t *testing.T) {
func TestNodes(t *testing.T) {
db := openTest()
defer db.Close()
s := From(db)
s := From(db)
g := goblin.Goblin(t)
g.Describe("Nodes", func() {
@ -26,7 +26,7 @@ func Test_nodestore(t *testing.T) {
Addr: "unix:///var/run/docker/docker.sock",
Arch: "linux_amd64",
}
err := s.Nodes().Create(&node)
err := s.CreateNode(&node)
g.Assert(err == nil).IsTrue()
g.Assert(node.ID != 0).IsTrue()
})
@ -36,14 +36,14 @@ func Test_nodestore(t *testing.T) {
Addr: "unix:///var/run/docker/docker.sock",
Arch: "linux_amd64",
}
err := s.Nodes().Create(&node)
err := s.CreateNode(&node)
g.Assert(err == nil).IsTrue()
g.Assert(node.ID != 0).IsTrue()
node.Addr = "unix:///var/run/docker.sock"
err1 := s.Nodes().Update(&node)
getnode, err2 := s.Nodes().Get(node.ID)
err1 := s.UpdateNode(&node)
getnode, err2 := s.GetNode(node.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(node.ID).Equal(getnode.ID)
@ -56,11 +56,11 @@ func Test_nodestore(t *testing.T) {
Addr: "unix:///var/run/docker/docker.sock",
Arch: "linux_amd64",
}
err := s.Nodes().Create(&node)
err := s.CreateNode(&node)
g.Assert(err == nil).IsTrue()
g.Assert(node.ID != 0).IsTrue()
getnode, err := s.Nodes().Get(node.ID)
getnode, err := s.GetNode(node.ID)
g.Assert(err == nil).IsTrue()
g.Assert(node.ID).Equal(getnode.ID)
g.Assert(node.Addr).Equal(getnode.Addr)
@ -76,42 +76,25 @@ func Test_nodestore(t *testing.T) {
Addr: "unix:///var/run/docker.sock",
Arch: "linux_386",
}
s.Nodes().Create(&node1)
s.Nodes().Create(&node2)
s.CreateNode(&node1)
s.CreateNode(&node2)
nodes, err := s.Nodes().GetList()
nodes, err := s.GetNodeList()
g.Assert(err == nil).IsTrue()
g.Assert(len(nodes)).Equal(2)
})
g.It("Should count nodes", func() {
node1 := model.Node{
Addr: "unix:///var/run/docker/docker.sock",
Arch: "linux_amd64",
}
node2 := model.Node{
Addr: "unix:///var/run/docker.sock",
Arch: "linux_386",
}
s.Nodes().Create(&node1)
s.Nodes().Create(&node2)
count, err := s.Nodes().Count()
g.Assert(err == nil).IsTrue()
g.Assert(count).Equal(2)
})
g.It("Should delete a node", func() {
node := model.Node{
Addr: "unix:///var/run/docker/docker.sock",
Arch: "linux_amd64",
}
err1 := s.Nodes().Create(&node)
err2 := s.Nodes().Delete(&node)
err1 := s.CreateNode(&node)
err2 := s.DeleteNode(&node)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
_, err := s.Nodes().Get(node.ID)
_, err := s.GetNode(node.ID)
g.Assert(err == nil).IsFalse()
})
})

View file

@ -1,30 +1,25 @@
package datastore
import (
"database/sql"
"fmt"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type repostore struct {
*sql.DB
}
func (db *repostore) Get(id int64) (*model.Repo, error) {
func (db *datastore) GetRepo(id int64) (*model.Repo, error) {
var repo = new(model.Repo)
var err = meddler.Load(db, repoTable, repo, id)
return repo, err
}
func (db *repostore) GetName(name string) (*model.Repo, error) {
func (db *datastore) GetRepoName(name string) (*model.Repo, error) {
var repo = new(model.Repo)
var err = meddler.QueryRow(db, repo, rebind(repoNameQuery), name)
return repo, err
}
func (db *repostore) GetListOf(listof []*model.RepoLite) ([]*model.Repo, error) {
func (db *datastore) GetRepoListOf(listof []*model.RepoLite) ([]*model.Repo, error) {
var (
repos []*model.Repo
args []interface{}
@ -40,21 +35,21 @@ func (db *repostore) GetListOf(listof []*model.RepoLite) ([]*model.Repo, error)
return repos, err
}
func (db *repostore) Count() (int, error) {
func (db *datastore) GetRepoCount() (int, error) {
var count int
var err = db.QueryRow(rebind(repoCountQuery)).Scan(&count)
return count, err
}
func (db *repostore) Create(repo *model.Repo) error {
func (db *datastore) CreateRepo(repo *model.Repo) error {
return meddler.Insert(db, repoTable, repo)
}
func (db *repostore) Update(repo *model.Repo) error {
func (db *datastore) UpdateRepo(repo *model.Repo) error {
return meddler.Update(db, repoTable, repo)
}
func (db *repostore) Delete(repo *model.Repo) error {
func (db *datastore) DeleteRepo(repo *model.Repo) error {
var _, err = db.Exec(rebind(repoDeleteStmt), repo.ID)
return err
}

View file

@ -7,7 +7,7 @@ import (
"github.com/franela/goblin"
)
func Test_repostore(t *testing.T) {
func TestRepos(t *testing.T) {
db := openTest()
defer db.Close()
@ -30,9 +30,9 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
err1 := s.Repos().Create(&repo)
err2 := s.Repos().Update(&repo)
getrepo, err3 := s.Repos().Get(repo.ID)
err1 := s.CreateRepo(&repo)
err2 := s.UpdateRepo(&repo)
getrepo, err3 := s.GetRepo(repo.ID)
if err3 != nil {
println("Get Repo Error")
println(err3.Error())
@ -50,7 +50,7 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
err := s.Repos().Create(&repo)
err := s.CreateRepo(&repo)
g.Assert(err == nil).IsTrue()
g.Assert(repo.ID != 0).IsTrue()
})
@ -62,8 +62,8 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
s.Repos().Create(&repo)
getrepo, err := s.Repos().Get(repo.ID)
s.CreateRepo(&repo)
getrepo, err := s.GetRepo(repo.ID)
g.Assert(err == nil).IsTrue()
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
@ -78,8 +78,8 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
s.Repos().Create(&repo)
getrepo, err := s.Repos().GetName(repo.FullName)
s.CreateRepo(&repo)
getrepo, err := s.GetRepoName(repo.FullName)
g.Assert(err == nil).IsTrue()
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
@ -106,11 +106,11 @@ func Test_repostore(t *testing.T) {
Name: "hello-world",
FullName: "octocat/hello-world",
}
s.Repos().Create(repo1)
s.Repos().Create(repo2)
s.Repos().Create(repo3)
s.CreateRepo(repo1)
s.CreateRepo(repo2)
s.CreateRepo(repo3)
repos, err := s.Repos().GetListOf([]*model.RepoLite{
repos, err := s.GetRepoListOf([]*model.RepoLite{
{FullName: "bradrydzewski/drone"},
{FullName: "drone/drone"},
})
@ -133,10 +133,10 @@ func Test_repostore(t *testing.T) {
Name: "drone",
FullName: "drone/drone",
}
s.Repos().Create(repo1)
s.Repos().Create(repo2)
s.CreateRepo(repo1)
s.CreateRepo(repo2)
count, err := s.Repos().Count()
count, err := s.GetRepoCount()
g.Assert(err == nil).IsTrue()
g.Assert(count).Equal(2)
})
@ -148,10 +148,10 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
s.Repos().Create(&repo)
_, err1 := s.Repos().Get(repo.ID)
err2 := s.Repos().Delete(&repo)
_, err3 := s.Repos().Get(repo.ID)
s.CreateRepo(&repo)
_, err1 := s.GetRepo(repo.ID)
err2 := s.DeleteRepo(&repo)
_, err3 := s.GetRepo(repo.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsFalse()
@ -170,8 +170,8 @@ func Test_repostore(t *testing.T) {
Owner: "bradrydzewski",
Name: "drone",
}
err1 := s.Repos().Create(&repo1)
err2 := s.Repos().Create(&repo2)
err1 := s.CreateRepo(&repo1)
err2 := s.CreateRepo(&repo2)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsFalse()
})

View file

@ -7,16 +7,22 @@ import (
"github.com/drone/drone/shared/envconfig"
"github.com/drone/drone/store"
"github.com/drone/drone/store/migration"
"github.com/drone/drone/store/datastore/ddl"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/rubenv/sql-migrate"
"github.com/russross/meddler"
log "github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus"
)
// datastore is an implementation of a model.Store built on top
// of the sql/database driver with a relational database backend.
type datastore struct {
*sql.DB
}
// Load opens a new database connection with the specified driver
// and connection string specified in the environment variables.
func Load(env envconfig.Env) store.Store {
@ -25,47 +31,32 @@ func Load(env envconfig.Env) store.Store {
config = env.String("DATABASE_CONFIG", "drone.sqlite")
)
log.Infof("using database driver %s", driver)
log.Infof("using database config %s", config)
logrus.Infof("using database driver %s", driver)
logrus.Infof("using database config %s", config)
return New(driver, config)
}
// New creates a database connection for the given driver and datasource
// and returns a new Store.
func New(driver, config string) store.Store {
db := Open(driver, config)
return store.New(
driver,
&nodestore{db},
&userstore{db},
&repostore{db},
&keystore{db},
&buildstore{db},
&jobstore{db},
&logstore{db},
return From(
open(driver, config),
)
}
// From returns a Store using an existing database connection.
func From(db *sql.DB) store.Store {
var driver string
return store.New(
driver,
&nodestore{db},
&userstore{db},
&repostore{db},
&keystore{db},
&buildstore{db},
&jobstore{db},
&logstore{db},
)
return &datastore{db}
}
// Open opens a new database connection with the specified
// open opens a new database connection with the specified
// driver and connection string and returns a store.
func Open(driver, config string) *sql.DB {
func open(driver, config string) *sql.DB {
db, err := sql.Open(driver, config)
if err != nil {
log.Errorln(err)
log.Fatalln("database connection failed")
logrus.Errorln(err)
logrus.Fatalln("database connection failed")
}
if driver == "mysql" {
// per issue https://github.com/go-sql-driver/mysql/issues/257
@ -75,13 +66,13 @@ func Open(driver, config string) *sql.DB {
setupMeddler(driver)
if err := pingDatabase(db); err != nil {
log.Errorln(err)
log.Fatalln("database ping attempts failed")
logrus.Errorln(err)
logrus.Fatalln("database ping attempts failed")
}
if err := setupDatabase(driver, db); err != nil {
log.Errorln(err)
log.Fatalln("migration failed")
logrus.Errorln(err)
logrus.Fatalln("migration failed")
}
cleanupDatabase(db)
return db
@ -99,7 +90,7 @@ func openTest() *sql.DB {
driver = os.Getenv("DATABASE_DRIVER")
config = os.Getenv("DATABASE_CONFIG")
}
return Open(driver, config)
return open(driver, config)
}
// helper function to ping the database with backoff to ensure
@ -111,7 +102,7 @@ func pingDatabase(db *sql.DB) (err error) {
if err == nil {
return
}
log.Infof("database ping failed. retry in 1s")
logrus.Infof("database ping failed. retry in 1s")
time.Sleep(time.Second)
}
return
@ -121,8 +112,8 @@ func pingDatabase(db *sql.DB) (err error) {
// automated database migration steps.
func setupDatabase(driver string, db *sql.DB) error {
var migrations = &migrate.AssetMigrationSource{
Asset: migration.Asset,
AssetDir: migration.AssetDir,
Asset: ddl.Asset,
AssetDir: ddl.AssetDir,
Dir: driver,
}
_, err := migrate.Exec(db, driver, migrations, migrate.Up)

View file

@ -1,36 +1,31 @@
package datastore
import (
"database/sql"
"fmt"
"github.com/drone/drone/model"
"github.com/russross/meddler"
)
type userstore struct {
*sql.DB
}
func (db *userstore) Get(id int64) (*model.User, error) {
func (db *datastore) GetUser(id int64) (*model.User, error) {
var usr = new(model.User)
var err = meddler.Load(db, userTable, usr, id)
return usr, err
}
func (db *userstore) GetLogin(login string) (*model.User, error) {
func (db *datastore) GetUserLogin(login string) (*model.User, error) {
var usr = new(model.User)
var err = meddler.QueryRow(db, usr, rebind(userLoginQuery), login)
return usr, err
}
func (db *userstore) GetList() ([]*model.User, error) {
func (db *datastore) GetUserList() ([]*model.User, error) {
var users = []*model.User{}
var err = meddler.QueryAll(db, &users, rebind(userListQuery))
return users, err
}
func (db *userstore) GetFeed(listof []*model.RepoLite) ([]*model.Feed, error) {
func (db *datastore) GetUserFeed(listof []*model.RepoLite) ([]*model.Feed, error) {
var (
feed []*model.Feed
args []interface{}
@ -46,21 +41,21 @@ func (db *userstore) GetFeed(listof []*model.RepoLite) ([]*model.Feed, error) {
return feed, err
}
func (db *userstore) Count() (int, error) {
func (db *datastore) GetUserCount() (int, error) {
var count int
var err = db.QueryRow(rebind(userCountQuery)).Scan(&count)
return count, err
}
func (db *userstore) Create(user *model.User) error {
func (db *datastore) CreateUser(user *model.User) error {
return meddler.Insert(db, userTable, user)
}
func (db *userstore) Update(user *model.User) error {
func (db *datastore) UpdateUser(user *model.User) error {
return meddler.Update(db, userTable, user)
}
func (db *userstore) Delete(user *model.User) error {
func (db *datastore) DeleteUser(user *model.User) error {
var _, err = db.Exec(rebind(userDeleteStmt), user.ID)
return err
}

View file

@ -7,7 +7,7 @@ import (
"github.com/franela/goblin"
)
func Test_userstore(t *testing.T) {
func TestUsers(t *testing.T) {
db := openTest()
defer db.Close()
s := From(db)
@ -30,9 +30,9 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
err1 := s.Users().Create(&user)
err2 := s.Users().Update(&user)
getuser, err3 := s.Users().Get(user.ID)
err1 := s.CreateUser(&user)
err2 := s.UpdateUser(&user)
getuser, err3 := s.GetUser(user.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsTrue()
@ -45,7 +45,7 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
err := s.Users().Create(&user)
err := s.CreateUser(&user)
g.Assert(err == nil).IsTrue()
g.Assert(user.ID != 0).IsTrue()
})
@ -61,8 +61,8 @@ func Test_userstore(t *testing.T) {
Admin: true,
}
s.Users().Create(&user)
getuser, err := s.Users().Get(user.ID)
s.CreateUser(&user)
getuser, err := s.GetUser(user.ID)
g.Assert(err == nil).IsTrue()
g.Assert(user.ID).Equal(getuser.ID)
g.Assert(user.Login).Equal(getuser.Login)
@ -80,8 +80,8 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
s.Users().Create(&user)
getuser, err := s.Users().GetLogin(user.Login)
s.CreateUser(&user)
getuser, err := s.GetUserLogin(user.Login)
g.Assert(err == nil).IsTrue()
g.Assert(user.ID).Equal(getuser.ID)
g.Assert(user.Login).Equal(getuser.Login)
@ -98,8 +98,8 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
}
err1 := s.Users().Create(&user1)
err2 := s.Users().Create(&user2)
err1 := s.CreateUser(&user1)
err2 := s.CreateUser(&user2)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsFalse()
})
@ -115,9 +115,9 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
s.Users().Create(&user1)
s.Users().Create(&user2)
users, err := s.Users().GetList()
s.CreateUser(&user1)
s.CreateUser(&user2)
users, err := s.GetUserList()
g.Assert(err == nil).IsTrue()
g.Assert(len(users)).Equal(2)
g.Assert(users[0].Login).Equal(user1.Login)
@ -136,15 +136,15 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
s.Users().Create(&user1)
s.Users().Create(&user2)
count, err := s.Users().Count()
s.CreateUser(&user1)
s.CreateUser(&user2)
count, err := s.GetUserCount()
g.Assert(err == nil).IsTrue()
g.Assert(count).Equal(2)
})
g.It("Should Get a User Count Zero", func() {
count, err := s.Users().Count()
count, err := s.GetUserCount()
g.Assert(err == nil).IsTrue()
g.Assert(count).Equal(0)
})
@ -155,10 +155,10 @@ func Test_userstore(t *testing.T) {
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
s.Users().Create(&user)
_, err1 := s.Users().Get(user.ID)
err2 := s.Users().Delete(&user)
_, err3 := s.Users().Get(user.ID)
s.CreateUser(&user)
_, err1 := s.GetUser(user.ID)
err2 := s.DeleteUser(&user)
_, err3 := s.GetUser(user.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsFalse()
@ -183,9 +183,9 @@ func Test_userstore(t *testing.T) {
Name: "hello-world",
FullName: "octocat/hello-world",
}
s.Repos().Create(repo1)
s.Repos().Create(repo2)
s.Repos().Create(repo3)
s.CreateRepo(repo1)
s.CreateRepo(repo2)
s.CreateRepo(repo3)
build1 := &model.Build{
RepoID: repo1.ID,
@ -203,12 +203,12 @@ func Test_userstore(t *testing.T) {
RepoID: repo3.ID,
Status: model.StatusSuccess,
}
s.Builds().Create(build1)
s.Builds().Create(build2)
s.Builds().Create(build3)
s.Builds().Create(build4)
s.CreateBuild(build1)
s.CreateBuild(build2)
s.CreateBuild(build3)
s.CreateBuild(build4)
builds, err := s.Users().GetFeed([]*model.RepoLite{
builds, err := s.GetUserFeed([]*model.RepoLite{
{FullName: "bradrydzewski/drone"},
{FullName: "drone/drone"},
})

View file

@ -1,43 +0,0 @@
package store
import (
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type JobStore interface {
// Get gets a job by unique ID.
Get(int64) (*model.Job, error)
// GetNumber gets a job by number.
GetNumber(*model.Build, int) (*model.Job, error)
// GetList gets a list of all users in the system.
GetList(*model.Build) ([]*model.Job, error)
// Create creates a job.
Create(*model.Job) error
// Update updates a job.
Update(*model.Job) error
}
func GetJob(c context.Context, id int64) (*model.Job, error) {
return FromContext(c).Jobs().Get(id)
}
func GetJobNumber(c context.Context, build *model.Build, num int) (*model.Job, error) {
return FromContext(c).Jobs().GetNumber(build, num)
}
func GetJobList(c context.Context, build *model.Build) ([]*model.Job, error) {
return FromContext(c).Jobs().GetList(build)
}
func CreateJob(c context.Context, job *model.Job) error {
return FromContext(c).Jobs().Create(job)
}
func UpdateJob(c context.Context, job *model.Job) error {
return FromContext(c).Jobs().Update(job)
}

View file

@ -1,36 +0,0 @@
package store
import (
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type KeyStore interface {
// Get gets a key by unique repository ID.
Get(*model.Repo) (*model.Key, error)
// Create creates a new key.
Create(*model.Key) error
// Update updates a user key.
Update(*model.Key) error
// Delete deletes a user key.
Delete(*model.Key) error
}
func GetKey(c context.Context, repo *model.Repo) (*model.Key, error) {
return FromContext(c).Keys().Get(repo)
}
func CreateKey(c context.Context, key *model.Key) error {
return FromContext(c).Keys().Create(key)
}
func UpdateKey(c context.Context, key *model.Key) error {
return FromContext(c).Keys().Update(key)
}
func DeleteKey(c context.Context, key *model.Key) error {
return FromContext(c).Keys().Delete(key)
}

View file

@ -1,24 +0,0 @@
package store
import (
"io"
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type LogStore interface {
// Read reads the Job logs from the datastore.
Read(*model.Job) (io.ReadCloser, error)
// Write writes the job logs to the datastore.
Write(*model.Job, io.Reader) error
}
func ReadLog(c context.Context, job *model.Job) (io.ReadCloser, error) {
return FromContext(c).Logs().Read(job)
}
func WriteLog(c context.Context, job *model.Job, r io.Reader) error {
return FromContext(c).Logs().Write(job, r)
}

View file

@ -1,3 +0,0 @@
package migration
//go:generate go-bindata -pkg migration -o migration_gen.go sqlite3/ mysql/ postgres/

View file

@ -1,50 +0,0 @@
package store
import (
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type NodeStore interface {
// Get gets a user by unique ID.
Get(int64) (*model.Node, error)
// GetList gets a list of all nodes in the system.
GetList() ([]*model.Node, error)
// Count gets a count of all nodes in the system.
Count() (int, error)
// Create creates a node.
Create(*model.Node) error
// Update updates a node.
Update(*model.Node) error
// Delete deletes a node.
Delete(*model.Node) error
}
func GetNode(c context.Context, id int64) (*model.Node, error) {
return FromContext(c).Nodes().Get(id)
}
func GetNodeList(c context.Context) ([]*model.Node, error) {
return FromContext(c).Nodes().GetList()
}
func CountNodes(c context.Context) (int, error) {
return FromContext(c).Nodes().Count()
}
func CreateNode(c context.Context, node *model.Node) error {
return FromContext(c).Nodes().Create(node)
}
func UpdateNode(c context.Context, node *model.Node) error {
return FromContext(c).Nodes().Update(node)
}
func DeleteNode(c context.Context, node *model.Node) error {
return FromContext(c).Nodes().Delete(node)
}

View file

@ -1,61 +0,0 @@
package store
import (
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type RepoStore interface {
// Get gets a repo by unique ID.
Get(int64) (*model.Repo, error)
// GetLogin gets a repo by its full name.
GetName(string) (*model.Repo, error)
// GetListOf gets the list of enumerated repos in the system.
GetListOf([]*model.RepoLite) ([]*model.Repo, error)
// Count gets a count of all repos in the system.
Count() (int, error)
// Create creates a new repository.
Create(*model.Repo) error
// Update updates a user repository.
Update(*model.Repo) error
// Delete deletes a user repository.
Delete(*model.Repo) error
}
func GetRepo(c context.Context, id int64) (*model.Repo, error) {
return FromContext(c).Repos().Get(id)
}
func GetRepoName(c context.Context, name string) (*model.Repo, error) {
return FromContext(c).Repos().GetName(name)
}
func GetRepoOwnerName(c context.Context, owner, name string) (*model.Repo, error) {
return FromContext(c).Repos().GetName(owner + "/" + name)
}
func GetRepoListOf(c context.Context, listof []*model.RepoLite) ([]*model.Repo, error) {
return FromContext(c).Repos().GetListOf(listof)
}
func CountRepos(c context.Context) (int, error) {
return FromContext(c).Repos().Count()
}
func CreateRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).Repos().Create(repo)
}
func UpdateRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).Repos().Update(repo)
}
func DeleteRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).Repos().Delete(repo)
}

View file

@ -1,53 +1,296 @@
package store
import (
"io"
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type Store interface {
Nodes() NodeStore
Users() UserStore
Repos() RepoStore
Keys() KeyStore
Builds() BuildStore
Jobs() JobStore
Logs() LogStore
// GetUser gets a user by unique ID.
GetUser(int64) (*model.User, error)
// GetUserLogin gets a user by unique Login name.
GetUserLogin(string) (*model.User, error)
// GetUserList gets a list of all users in the system.
GetUserList() ([]*model.User, error)
// GetUserFeed gets a user activity feed.
GetUserFeed([]*model.RepoLite) ([]*model.Feed, error)
// GetUserCount gets a count of all users in the system.
GetUserCount() (int, error)
// CreateUser creates a new user account.
CreateUser(*model.User) error
// UpdateUser updates a user account.
UpdateUser(*model.User) error
// DeleteUser deletes a user account.
DeleteUser(*model.User) error
// GetRepo gets a repo by unique ID.
GetRepo(int64) (*model.Repo, error)
// GetRepoName gets a repo by its full name.
GetRepoName(string) (*model.Repo, error)
// GetRepoListOf gets the list of enumerated repos in the system.
GetRepoListOf([]*model.RepoLite) ([]*model.Repo, error)
// GetRepoCount gets a count of all repositories in the system.
GetRepoCount() (int, error)
// CreateRepo creates a new repository.
CreateRepo(*model.Repo) error
// UpdateRepo updates a user repository.
UpdateRepo(*model.Repo) error
// DeleteRepo deletes a user repository.
DeleteRepo(*model.Repo) error
// GetKey gets a key by unique repository ID.
GetKey(*model.Repo) (*model.Key, error)
// CreateKey creates a new key.
CreateKey(*model.Key) error
// UpdateKey updates a user key.
UpdateKey(*model.Key) error
// DeleteKey deletes a user key.
DeleteKey(*model.Key) error
// GetBuild gets a build by unique ID.
GetBuild(int64) (*model.Build, error)
// GetBuildNumber gets a build by number.
GetBuildNumber(*model.Repo, int) (*model.Build, error)
// GetBuildRef gets a build by its ref.
GetBuildRef(*model.Repo, string) (*model.Build, error)
// GetBuildCommit gets a build by its commit sha.
GetBuildCommit(*model.Repo, string, string) (*model.Build, error)
// GetBuildLast gets the last build for the branch.
GetBuildLast(*model.Repo, string) (*model.Build, error)
// GetBuildLastBefore gets the last build before build number N.
GetBuildLastBefore(*model.Repo, string, int64) (*model.Build, error)
// GetBuildList gets a list of builds for the repository
GetBuildList(*model.Repo) ([]*model.Build, error)
// CreateBuild creates a new build and jobs.
CreateBuild(*model.Build, ...*model.Job) error
// UpdateBuild updates a build.
UpdateBuild(*model.Build) error
// GetJob gets a job by unique ID.
GetJob(int64) (*model.Job, error)
// GetJobNumber gets a job by number.
GetJobNumber(*model.Build, int) (*model.Job, error)
// GetJobList gets a list of all users in the system.
GetJobList(*model.Build) ([]*model.Job, error)
// CreateJob creates a job.
CreateJob(*model.Job) error
// UpdateJob updates a job.
UpdateJob(*model.Job) error
// ReadLog reads the Job logs from the datastore.
ReadLog(*model.Job) (io.ReadCloser, error)
// WriteLog writes the job logs to the datastore.
WriteLog(*model.Job, io.Reader) error
// GetNode gets a build node from the datastore.
GetNode(id int64) (*model.Node, error)
// GetNodeList gets a build node list from the datastore.
GetNodeList() ([]*model.Node, error)
// CreateNode add a new build node to the datastore.
CreateNode(*model.Node) error
// UpdateNode updates a build node in the datastore.
UpdateNode(*model.Node) error
// DeleteNode removes a build node from the datastore.
DeleteNode(*model.Node) error
}
type store struct {
name string
nodes NodeStore
users UserStore
repos RepoStore
keys KeyStore
builds BuildStore
jobs JobStore
logs LogStore
// GetUser gets a user by unique ID.
func GetUser(c context.Context, id int64) (*model.User, error) {
return FromContext(c).GetUser(id)
}
func (s *store) Nodes() NodeStore { return s.nodes }
func (s *store) Users() UserStore { return s.users }
func (s *store) Repos() RepoStore { return s.repos }
func (s *store) Keys() KeyStore { return s.keys }
func (s *store) Builds() BuildStore { return s.builds }
func (s *store) Jobs() JobStore { return s.jobs }
func (s *store) Logs() LogStore { return s.logs }
func (s *store) String() string { return s.name }
func New(
name string,
nodes NodeStore,
users UserStore,
repos RepoStore,
keys KeyStore,
builds BuildStore,
jobs JobStore,
logs LogStore,
) Store {
return &store{
name,
nodes,
users,
repos,
keys,
builds,
jobs,
logs,
}
// GetUserLogin gets a user by unique Login name.
func GetUserLogin(c context.Context, login string) (*model.User, error) {
return FromContext(c).GetUserLogin(login)
}
// GetUserList gets a list of all users in the system.
func GetUserList(c context.Context) ([]*model.User, error) {
return FromContext(c).GetUserList()
}
// GetUserFeed gets a user activity feed.
func GetUserFeed(c context.Context, listof []*model.RepoLite) ([]*model.Feed, error) {
return FromContext(c).GetUserFeed(listof)
}
// GetUserCount gets a count of all users in the system.
func GetUserCount(c context.Context) (int, error) {
return FromContext(c).GetUserCount()
}
func CreateUser(c context.Context, user *model.User) error {
return FromContext(c).CreateUser(user)
}
func UpdateUser(c context.Context, user *model.User) error {
return FromContext(c).UpdateUser(user)
}
func DeleteUser(c context.Context, user *model.User) error {
return FromContext(c).DeleteUser(user)
}
func GetRepo(c context.Context, id int64) (*model.Repo, error) {
return FromContext(c).GetRepo(id)
}
func GetRepoName(c context.Context, name string) (*model.Repo, error) {
return FromContext(c).GetRepoName(name)
}
func GetRepoOwnerName(c context.Context, owner, name string) (*model.Repo, error) {
return FromContext(c).GetRepoName(owner + "/" + name)
}
func GetRepoListOf(c context.Context, listof []*model.RepoLite) ([]*model.Repo, error) {
return FromContext(c).GetRepoListOf(listof)
}
func CreateRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).CreateRepo(repo)
}
func UpdateRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).UpdateRepo(repo)
}
func DeleteRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).DeleteRepo(repo)
}
func GetKey(c context.Context, repo *model.Repo) (*model.Key, error) {
return FromContext(c).GetKey(repo)
}
func CreateKey(c context.Context, key *model.Key) error {
return FromContext(c).CreateKey(key)
}
func UpdateKey(c context.Context, key *model.Key) error {
return FromContext(c).UpdateKey(key)
}
func DeleteKey(c context.Context, key *model.Key) error {
return FromContext(c).DeleteKey(key)
}
func GetBuild(c context.Context, id int64) (*model.Build, error) {
return FromContext(c).GetBuild(id)
}
func GetBuildNumber(c context.Context, repo *model.Repo, num int) (*model.Build, error) {
return FromContext(c).GetBuildNumber(repo, num)
}
func GetBuildRef(c context.Context, repo *model.Repo, ref string) (*model.Build, error) {
return FromContext(c).GetBuildRef(repo, ref)
}
func GetBuildCommit(c context.Context, repo *model.Repo, sha, branch string) (*model.Build, error) {
return FromContext(c).GetBuildCommit(repo, sha, branch)
}
func GetBuildLast(c context.Context, repo *model.Repo, branch string) (*model.Build, error) {
return FromContext(c).GetBuildLast(repo, branch)
}
func GetBuildLastBefore(c context.Context, repo *model.Repo, branch string, number int64) (*model.Build, error) {
return FromContext(c).GetBuildLastBefore(repo, branch, number)
}
func GetBuildList(c context.Context, repo *model.Repo) ([]*model.Build, error) {
return FromContext(c).GetBuildList(repo)
}
func CreateBuild(c context.Context, build *model.Build, jobs ...*model.Job) error {
return FromContext(c).CreateBuild(build, jobs...)
}
func UpdateBuild(c context.Context, build *model.Build) error {
return FromContext(c).UpdateBuild(build)
}
func GetJob(c context.Context, id int64) (*model.Job, error) {
return FromContext(c).GetJob(id)
}
func GetJobNumber(c context.Context, build *model.Build, num int) (*model.Job, error) {
return FromContext(c).GetJobNumber(build, num)
}
func GetJobList(c context.Context, build *model.Build) ([]*model.Job, error) {
return FromContext(c).GetJobList(build)
}
func CreateJob(c context.Context, job *model.Job) error {
return FromContext(c).CreateJob(job)
}
func UpdateJob(c context.Context, job *model.Job) error {
return FromContext(c).UpdateJob(job)
}
func ReadLog(c context.Context, job *model.Job) (io.ReadCloser, error) {
return FromContext(c).ReadLog(job)
}
func WriteLog(c context.Context, job *model.Job, r io.Reader) error {
return FromContext(c).WriteLog(job, r)
}
func GetNode(c context.Context, id int64) (*model.Node, error) {
return FromContext(c).GetNode(id)
}
func GetNodeList(c context.Context) ([]*model.Node, error) {
return FromContext(c).GetNodeList()
}
func CreateNode(c context.Context, node *model.Node) error {
return FromContext(c).CreateNode(node)
}
func UpdateNode(c context.Context, node *model.Node) error {
return FromContext(c).UpdateNode(node)
}
func DeleteNode(c context.Context, node *model.Node) error {
return FromContext(c).DeleteNode(node)
}

View file

@ -1,64 +0,0 @@
package store
import (
"github.com/drone/drone/model"
"golang.org/x/net/context"
)
type UserStore interface {
// Get gets a user by unique ID.
Get(int64) (*model.User, error)
// GetLogin gets a user by unique Login name.
GetLogin(string) (*model.User, error)
// GetList gets a list of all users in the system.
GetList() ([]*model.User, error)
// GetFeed gets a user activity feed.
GetFeed([]*model.RepoLite) ([]*model.Feed, error)
// Count gets a count of all users in the system.
Count() (int, error)
// Create creates a new user account.
Create(*model.User) error
// Update updates a user account.
Update(*model.User) error
// Delete deletes a user account.
Delete(*model.User) error
}
func GetUser(c context.Context, id int64) (*model.User, error) {
return FromContext(c).Users().Get(id)
}
func GetUserLogin(c context.Context, login string) (*model.User, error) {
return FromContext(c).Users().GetLogin(login)
}
func GetUserList(c context.Context) ([]*model.User, error) {
return FromContext(c).Users().GetList()
}
func GetUserFeed(c context.Context, listof []*model.RepoLite) ([]*model.Feed, error) {
return FromContext(c).Users().GetFeed(listof)
}
func CountUsers(c context.Context) (int, error) {
return FromContext(c).Users().Count()
}
func CreateUser(c context.Context, user *model.User) error {
return FromContext(c).Users().Create(user)
}
func UpdateUser(c context.Context, user *model.User) error {
return FromContext(c).Users().Update(user)
}
func DeleteUser(c context.Context, user *model.User) error {
return FromContext(c).Users().Delete(user)
}