add store repository for template queries
This commit is contained in:
parent
58824af15e
commit
91076dc1d0
3 changed files with 420 additions and 0 deletions
58
store/template/scan.go
Normal file
58
store/template/scan.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/store/shared/db"
|
||||
)
|
||||
|
||||
// helper function converts the Template structure to a set
|
||||
// of named query parameters.
|
||||
func toParams(template *core.Template) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"template_id": template.Id,
|
||||
"template_name": template.Name,
|
||||
"template_data": template.Data,
|
||||
"template_created": template.Created,
|
||||
"template_updated": template.Updated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// helper function scans the sql.Row and copies the column
|
||||
// values to the destination object.
|
||||
func scanRow(scanner db.Scanner, dst *core.Template) error {
|
||||
err := scanner.Scan(
|
||||
&dst.Id,
|
||||
&dst.Name,
|
||||
&dst.Data,
|
||||
&dst.Created,
|
||||
&dst.Updated,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// helper function scans the sql.Row and copies the column
|
||||
// values to the destination object.
|
||||
func scanRows(rows *sql.Rows) ([]*core.Template, error) {
|
||||
defer rows.Close()
|
||||
|
||||
var template []*core.Template
|
||||
for rows.Next() {
|
||||
tem := new(core.Template)
|
||||
err := scanRow(rows, tem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
template = append(template, tem)
|
||||
}
|
||||
return template, nil
|
||||
}
|
206
store/template/template.go
Normal file
206
store/template/template.go
Normal file
|
@ -0,0 +1,206 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/store/shared/db"
|
||||
)
|
||||
|
||||
// New returns a new Template database store.
|
||||
func New(db *db.DB) core.TemplateStore {
|
||||
return &templateStore{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
type templateStore struct {
|
||||
db *db.DB
|
||||
}
|
||||
|
||||
func (s *templateStore) List(ctx context.Context, id int64) ([]*core.Template, error) {
|
||||
var out []*core.Template
|
||||
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
||||
params := map[string]interface{}{"template_id": id}
|
||||
stmt, args, err := binder.BindNamed(queryRepo, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := queryer.Query(stmt, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out, err = scanRows(rows)
|
||||
return err
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (s *templateStore) Find(ctx context.Context, id int64) (*core.Template, error) {
|
||||
out := &core.Template{Id: id}
|
||||
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
||||
params, err := toParams(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query, args, err := binder.BindNamed(queryKey, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
row := queryer.QueryRow(query, args...)
|
||||
return scanRow(row, out)
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (s *templateStore) FindName(ctx context.Context, id int64, name string) (*core.Template, error) {
|
||||
out := &core.Template{Name: name, Id: id}
|
||||
err := s.db.View(func(queryer db.Queryer, binder db.Binder) error {
|
||||
params, err := toParams(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query, args, err := binder.BindNamed(queryName, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
row := queryer.QueryRow(query, args...)
|
||||
return scanRow(row, out)
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (s *templateStore) Create(ctx context.Context, template *core.Template) error {
|
||||
if s.db.Driver() == db.Postgres {
|
||||
return s.createPostgres(ctx, template)
|
||||
}
|
||||
return s.create(ctx, template)
|
||||
}
|
||||
|
||||
func (s *templateStore) create(ctx context.Context, template *core.Template) error {
|
||||
return s.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
||||
params, err := toParams(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt, args, err := binder.BindNamed(stmtInsert, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := execer.Exec(stmt, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
template.Id, err = res.LastInsertId()
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *templateStore) createPostgres(ctx context.Context, template *core.Template) error {
|
||||
return s.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
||||
params, err := toParams(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt, args, err := binder.BindNamed(stmtInsertPostgres, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return execer.QueryRow(stmt, args...).Scan(&template.Id)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *templateStore) Update(ctx context.Context, template *core.Template) error {
|
||||
return s.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
||||
params, err := toParams(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt, args, err := binder.BindNamed(stmtUpdate, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = execer.Exec(stmt, args...)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *templateStore) Delete(ctx context.Context, template *core.Template) error {
|
||||
return s.db.Lock(func(execer db.Execer, binder db.Binder) error {
|
||||
params, err := toParams(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt, args, err := binder.BindNamed(stmtDelete, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = execer.Exec(stmt, args...)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
const queryKey = queryBase + `
|
||||
FROM template
|
||||
WHERE template_id = :template_id
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
const queryBase = `
|
||||
SELECT
|
||||
template_id
|
||||
,template_name
|
||||
,template_data
|
||||
,template_created
|
||||
,template_updated
|
||||
`
|
||||
|
||||
const queryRepo = queryBase + `
|
||||
FROM template
|
||||
WHERE template_id = :template_id
|
||||
ORDER BY template_name
|
||||
`
|
||||
const stmtInsert = `
|
||||
INSERT INTO template (
|
||||
template_id
|
||||
,template_name
|
||||
,template_data
|
||||
,template_created
|
||||
,template_updated
|
||||
) VALUES (
|
||||
:template_id
|
||||
,:template_name
|
||||
,:template_data
|
||||
,:template_created
|
||||
,:template_updated
|
||||
)
|
||||
`
|
||||
|
||||
const stmtUpdate = `
|
||||
UPDATE template SET
|
||||
template_name = :template_name
|
||||
,template_data = :template_data
|
||||
,template_updated = :template_updated
|
||||
WHERE template_id = :template_id
|
||||
`
|
||||
|
||||
const stmtDelete = `
|
||||
DELETE FROM template
|
||||
WHERE template_id = :template_id
|
||||
`
|
||||
const queryName = queryBase + `
|
||||
FROM template
|
||||
WHERE template_name = :template_name
|
||||
AND template_id = :template_id
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
const stmtInsertPostgres = stmtInsert + `
|
||||
RETURNING template_id
|
||||
`
|
156
store/template/template_test.go
Normal file
156
store/template/template_test.go
Normal file
|
@ -0,0 +1,156 @@
|
|||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Drone Non-Commercial License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !oss
|
||||
|
||||
package template
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/store/repos"
|
||||
"github.com/drone/drone/store/shared/db/dbtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var noContext = context.TODO()
|
||||
|
||||
func TestTemplate(t *testing.T) {
|
||||
conn, err := dbtest.Connect()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
dbtest.Reset(conn)
|
||||
dbtest.Disconnect(conn)
|
||||
}()
|
||||
|
||||
// seeds the database with a dummy repository.
|
||||
repo := &core.Repository{UID: "1", Slug: "octocat/hello-world"}
|
||||
repos := repos.New(conn)
|
||||
if err := repos.Create(noContext, repo); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
store := New(conn).(*templateStore)
|
||||
t.Run("Create", testTemplateCreate(store, repos, repo))
|
||||
}
|
||||
|
||||
func testTemplateCreate(store *templateStore, repos core.RepositoryStore, repo *core.Repository) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
item := &core.Template{
|
||||
Id: repo.ID,
|
||||
Name: "my_template",
|
||||
Data: "some_template_data",
|
||||
Created: 1,
|
||||
Updated: 2,
|
||||
}
|
||||
err := store.Create(noContext, item)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if item.Id == 0 {
|
||||
t.Errorf("Want template Id assigned, got %d", item.Id)
|
||||
}
|
||||
|
||||
t.Run("Find", testTemplateFind(store, item))
|
||||
t.Run("FindName", testTemplateFindName(store, repo))
|
||||
t.Run("List", testTemplateList(store, repo))
|
||||
t.Run("Update", testTemplateUpdate(store, repo))
|
||||
t.Run("Delete", testTemplateDelete(store, repo))
|
||||
}
|
||||
}
|
||||
|
||||
func testTemplateFind(store *templateStore, template *core.Template) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
item, err := store.Find(noContext, template.Id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Run("Fields", testTemplate(item))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTemplateFindName(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
item, err := store.FindName(noContext, repo.ID, "my_template")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Run("Fields", testTemplate(item))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTemplate(item *core.Template) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
if got, want := item.Name, "my_template"; got != want {
|
||||
t.Errorf("Want template name %q, got %q", want, got)
|
||||
}
|
||||
if got, want := item.Data, "some_template_data"; got != want {
|
||||
t.Errorf("Want template data %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTemplateList(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
list, err := store.List(noContext, repo.ID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if got, want := len(list), 1; got != want {
|
||||
t.Errorf("Want count %d, got %d", want, got)
|
||||
} else {
|
||||
t.Run("Fields", testTemplate(list[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTemplateUpdate(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
before, err := store.FindName(noContext, repo.ID, "my_template")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
err = store.Update(noContext, before)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
after, err := store.Find(noContext, before.Id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if after == nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTemplateDelete(store *templateStore, repo *core.Repository) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
secret, err := store.FindName(noContext, repo.ID, "my_template")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
err = store.Delete(noContext, secret)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
_, err = store.Find(noContext, secret.Id)
|
||||
if got, want := sql.ErrNoRows, err; got != want {
|
||||
t.Errorf("Want sql.ErrNoRows, got %v", got)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue