refactoring to use datastore

This commit is contained in:
Brad Rydzewski 2014-09-25 01:07:40 -07:00
parent ed0024efa1
commit 557c730b52
17 changed files with 730 additions and 3 deletions

View file

@ -28,7 +28,7 @@ type CommitManager interface {
ListBranch(repo int64, branch string) ([]*model.Commit, error)
// ListBranches finds most recent commit for each branch.
ListBranches(repo int64) ([]*model.Commit, error)
//ListBranches(repo int64) ([]*model.Commit, error)
// ListUser finds most recent commits for a user.
ListUser(repo int64) ([]*model.CommitRepo, error)

View file

@ -25,7 +25,7 @@ type PermManager interface {
// Write returns true if the specified user has write
// access to the repository.
Write(u *model.User, r *model.Repo) (bool, error)
//Write(u *model.User, r *model.Repo) (bool, error)
// Admin returns true if the specified user is an
// administrator of the repository.
@ -33,7 +33,7 @@ type PermManager interface {
// Member returns true if the specified user is a
// collaborator on the repository.
Member(u *model.User, r *model.Repo) (bool, error)
//Member(u *model.User, r *model.Repo) (bool, error)
}
// permManager manages user permissions to access repositories.

View file

@ -0,0 +1,94 @@
package datastore
import (
"code.google.com/p/go.net/context"
"github.com/drone/drone/shared/model"
)
type Commitstore interface {
// GetCommit retrieves a commit from the
// datastore for the given ID.
GetCommit(id int64) (*model.Commit, error)
// GetCommitSha retrieves a commit from the
// datastore for the specified repo and sha
GetCommitSha(repo *model.Repo, branch, sha string) (*model.Commit, error)
// GetCommitLast retrieves the latest commit
// from the datastore for the specified repository
// and branch.
GetCommitLast(repo *model.Repo, branch string) (*model.Commit, error)
// GetCommitList retrieves a list of latest commits
// from the datastore for the specified repository.
GetCommitList(repo *model.Repo) ([]*model.Commit, error)
// GetCommitListUser retrieves a list of latest commits
// from the datastore accessible to the specified user.
GetCommitListUser(user *model.User) ([]*model.Commit, error)
// PostCommit saves a commit in the datastore.
PostCommit(commit *model.Commit) error
// PutCommit saves a commit in the datastore.
PutCommit(commit *model.Commit) error
// DelCommit removes the commit from the datastore.
DelCommit(commit *model.Commit) error
// KillCommits updates all pending or started commits
// in the datastore settings the status to killed.
KillCommits() error
}
// GetCommit retrieves a commit from the
// datastore for the given ID.
func GetCommit(c context.Context, id int64) (*model.Commit, error) {
return FromContext(c).GetCommit(id)
}
// GetCommitSha retrieves a commit from the
// datastore for the specified repo and sha
func GetCommitSha(c context.Context, repo *model.Repo, branch, sha string) (*model.Commit, error) {
return FromContext(c).GetCommitSha(repo, branch, sha)
}
// GetCommitLast retrieves the latest commit
// from the datastore for the specified repository
// and branch.
func GetCommitLast(c context.Context, repo *model.Repo, branch string) (*model.Commit, error) {
return FromContext(c).GetCommitLast(repo, branch)
}
// GetCommitList retrieves a list of latest commits
// from the datastore for the specified repository.
func GetCommitList(c context.Context, repo *model.Repo) ([]*model.Commit, error) {
return FromContext(c).GetCommitList(repo)
}
// GetCommitListUser retrieves a list of latest commits
// from the datastore accessible to the specified user.
func GetCommitListUser(c context.Context, user *model.User) ([]*model.Commit, error) {
return FromContext(c).GetCommitListUser(user)
}
// PostCommit saves a commit in the datastore.
func PostCommit(c context.Context, commit *model.Commit) error {
return FromContext(c).PostCommit(commit)
}
// PutCommit saves a commit in the datastore.
func PutCommit(c context.Context, commit *model.Commit) error {
return FromContext(c).PutCommit(commit)
}
// DelCommit removes the commit from the datastore.
func DelCommit(c context.Context, commit *model.Commit) error {
return FromContext(c).DelCommit(commit)
}
// KillCommits updates all pending or started commits
// in the datastore settings the status to killed.
func KillCommits(c context.Context) error {
return FromContext(c).KillCommits()
}

View file

@ -0,0 +1,31 @@
package datastore
import (
"code.google.com/p/go.net/context"
)
const reqkey = "datastore"
// NewContext returns a Context whose Value method returns the
// application's data storage objects.
func NewContext(parent context.Context, ds Datastore) context.Context {
return &wrapper{parent, ds}
}
type wrapper struct {
context.Context
ds Datastore
}
// Value returns the named key from the context.
func (c *wrapper) Value(key interface{}) interface{} {
if key == reqkey {
return c.ds
}
return c.Context.Value(key)
}
// FromContext returns the sql.DB associated with this context.
func FromContext(c context.Context) Datastore {
return c.Value(reqkey).(Datastore)
}

View file

@ -0,0 +1,123 @@
package datasql
import (
"github.com/drone/drone/shared/model"
"github.com/russross/meddler"
)
type Commitstore struct {
meddler.DB
}
func NewCommitstore(db meddler.DB) *Commitstore {
return &Commitstore{db}
}
// GetCommit retrieves a commit from the
// datastore for the given ID.
func (db *Commitstore) GetCommit(id int64) (*model.Commit, error) {
var commit = new(model.Commit)
var err = meddler.Load(db, commitTable, commit, id)
return commit, err
}
// GetCommitSha retrieves a commit from the
// datastore for the specified repo and sha
func (db *Commitstore) GetCommitSha(repo *model.Repo, branch, sha string) (*model.Commit, error) {
var commit = new(model.Commit)
var err = meddler.QueryRow(db, commit, commitShaQuery, repo.ID, branch, sha)
return commit, err
}
// GetCommitLast retrieves the latest commit
// from the datastore for the specified repository
// and branch.
func (db *Commitstore) GetCommitLast(repo *model.Repo, branch string) (*model.Commit, error) {
var commit = new(model.Commit)
var err = meddler.QueryRow(db, commit, commitLastQuery, repo.ID, branch)
return commit, err
}
// GetCommitList retrieves a list of latest commits
// from the datastore for the specified repository.
func (db *Commitstore) GetCommitList(repo *model.Repo) ([]*model.Commit, error) {
var commits []*model.Commit
var err = meddler.QueryAll(db, &commits, commitListQuery)
return commits, err
}
// GetCommitListUser retrieves a list of latest commits
// from the datastore accessible to the specified user.
func (db *Commitstore) GetCommitListUser(user *model.User) ([]*model.Commit, error) {
return nil, nil
}
// PostCommit saves a commit in the datastore.
func (db *Commitstore) PostCommit(commit *model.Commit) error {
return meddler.Save(db, commitTable, commit)
}
// PutCommit saves a commit in the datastore.
func (db *Commitstore) PutCommit(commit *model.Commit) error {
return meddler.Save(db, commitTable, commit)
}
// DelCommit removes the commit from the datastore.
func (db *Commitstore) DelCommit(commit *model.Commit) error {
var _, err = db.Exec(commitDeleteStmt, commit.ID)
return err
}
// KillCommits updates all pending or started commits
// in the datastore settings the status to killed.
func (db *Commitstore) KillCommits() error {
var _, err = db.Exec(commitKillStmt)
return err
}
// Commit table name in database.
const commitTable = "commits"
// SQL statement to delete a Commit by ID.
const commitDeleteStmt = `
DELETE FROM commits
WHERE commit_id = ?
`
// SQL query to retrieve the latest Commits across all branches.
const commitListQuery = `
SELECT *
FROM commits
WHERE repo_id = ?
ORDER BY commit_id DESC
LIMIT 20
`
// SQL query to retrieve a Commit by branch and sha.
const commitShaQuery = `
SELECT *
FROM commits
WHERE repo_id = ?
AND commit_branch = ?
AND commit_sha = ?
LIMIT 1
`
// SQL query to retrieve the most recent Commit for a branch.
const commitLastQuery = `
SELECT *
FROM commits
WHERE repo_id = ?
AND commit_branch = ?
ORDER BY commit_id DESC
LIMIT 1
`
// SQL statement to cancel all running Commits.
const commitKillStmt = `
UPDATE commits SET
commit_status = ?,
commit_started = ?,
commit_finished = ?
WHERE commit_status IN ('Started', 'Pending');
`

View file

@ -0,0 +1 @@
package datasql

View file

@ -0,0 +1,54 @@
package datasql
import (
"database/sql"
"github.com/drone/drone/server/blobstore/blobsql"
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/shared/model"
"github.com/astaxie/beego/orm"
_ "github.com/mattn/go-sqlite3"
)
const (
driverPostgres = "postgres"
driverSqlite = "sqlite3"
driverMysql = "mysql"
databaseName = "default"
)
// Connect is a helper function that establishes a new
// database connection and auto-generates the database
// schema. If the database already exists, it will perform
// and update as needed.
func Connect(driver, datasource string) (*sql.DB, error) {
defer orm.ResetModelCache()
orm.RegisterDriver(driverSqlite, orm.DR_Sqlite)
orm.RegisterDataBase(databaseName, driver, datasource)
orm.RegisterModel(new(model.User))
orm.RegisterModel(new(model.Perm))
orm.RegisterModel(new(model.Repo))
orm.RegisterModel(new(model.Commit))
orm.RegisterModel(new(blobsql.Blob))
var err = orm.RunSyncdb(databaseName, true, true)
if err != nil {
return nil, err
}
return orm.GetDB(databaseName)
}
// New returns a new DataStore
func New(db *sql.DB) datastore.Datastore {
return struct {
*Userstore
*Permstore
*Repostore
*Commitstore
}{
NewUserstore(db),
NewPermstore(db),
NewRepostore(db),
NewCommitstore(db),
}
}

View file

@ -0,0 +1,57 @@
package datasql
import (
"github.com/drone/drone/shared/model"
"github.com/russross/meddler"
)
type Permstore struct {
meddler.DB
}
func NewPermstore(db meddler.DB) *Permstore {
return &Permstore{db}
}
// GetPerm retrieves the User's permission from
// the datastore for the given repository.
func (db *Repostore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error) {
var perm = new(model.Perm)
var err = meddler.QueryRow(db, perm, permQuery, user.ID, repo.ID)
return perm, err
}
// PostPerm saves permission in the datastore.
func (db *Repostore) PostPerm(perm *model.Perm) error {
return meddler.Save(db, permTable, perm)
}
// PutPerm saves permission in the datastore.
func (db *Repostore) PutPerm(perm *model.Perm) error {
return meddler.Save(db, permTable, perm)
}
// DelPerm removes permission from the datastore.
func (db *Repostore) DelPerm(perm *model.Perm) error {
var _, err = db.Exec(permDeleteStmt, perm.ID)
return err
}
// Permission table name in database.
const permTable = "perms"
// SQL query to retrieve a user's permission to
// access a repository.
const permQuery = `
SELECT *
FROM perms
WHERE user_id=?
AND repo_id=?
LIMIT 1
`
// SQL statement to delete a User by ID.
const permDeleteStmt = `
DELETE FROM perms
WHERE perm_id=?
`

View file

@ -0,0 +1 @@
package datasql

View file

@ -0,0 +1,85 @@
package datasql
import (
"github.com/drone/drone/shared/model"
"github.com/russross/meddler"
)
type Repostore struct {
meddler.DB
}
func NewRepostore(db meddler.DB) *Repostore {
return &Repostore{db}
}
// GetRepo retrieves a specific repo from the
// datastore for the given ID.
func (db *Repostore) GetRepo(id int64) (*model.Repo, error) {
var repo = new(model.Repo)
var err = meddler.Load(db, repoTable, repo, id)
return repo, err
}
// GetRepoName retrieves a repo from the datastore
// for the specified remote, owner and name.
func (db *Repostore) GetRepoName(remote, owner, name string) (*model.Repo, error) {
var repo = new(model.Repo)
var err = meddler.QueryRow(db, repo, repoNameQuery, remote, owner, name)
return repo, err
}
// GetRepoList retrieves a list of all repos from
// the datastore accessible by the given user ID.
func (db *Repostore) GetRepoList(user *model.User) ([]*model.Repo, error) {
var repos []*model.Repo
var err = meddler.QueryAll(db, &repos, repoListQuery)
return repos, err
}
// PostRepo saves a repo in the datastore.
func (db *Repostore) PostRepo(repo *model.Repo) error {
return meddler.Save(db, repoTable, repo)
}
// PutRepo saves a repo in the datastore.
func (db *Repostore) PutRepo(repo *model.Repo) error {
return meddler.Save(db, repoTable, repo)
}
// DelRepo removes the repo from the datastore.
func (db *Repostore) DelRepo(repo *model.Repo) error {
var _, err = db.Exec(repoDeleteStmt, repo.ID)
return err
}
// Repo table name in database.
const repoTable = "repos"
// SQL statement to retrieve a Repo by name.
const repoNameQuery = `
SELECT *
FROM repos
WHERE repo_host = ?
AND repo_owner = ?
AND repo_name = ?
LIMIT 1;
`
// SQL statement to retrieve a list of Repos
// with permissions for the given User ID.
const repoListQuery = `
SELECT *
FROM repos
WHERE repo_id IN (
SELECT repo_id
FROM perms
WHERE user_id = ?
);
`
// SQL statement to delete a User by ID.
const repoDeleteStmt = `
DELETE FROM users
WHERE user_id = ?
`

View file

@ -0,0 +1 @@
package datasql

View file

@ -0,0 +1,95 @@
package datasql
import (
"github.com/drone/drone/shared/model"
"github.com/russross/meddler"
)
type Userstore struct {
meddler.DB
}
func NewUserstore(db meddler.DB) *Userstore {
return &Userstore{db}
}
// GetUser retrieves a specific user from the
// datastore for the given ID.
func (db *Repostore) GetUser(id int64) (*model.User, error) {
var usr = new(model.User)
var err = meddler.Load(db, userTable, usr, id)
return usr, err
}
// GetUserLogin retrieves a user from the datastore
// for the specified remote and login name.
func (db *Repostore) GetUserLogin(remote, login string) (*model.User, error) {
var usr = new(model.User)
var err = meddler.QueryRow(db, usr, userLoginQuery)
return usr, err
}
// GetUserToken retrieves a user from the datastore
// with the specified token.
func (db *Repostore) GetUserToken(token string) (*model.User, error) {
var usr = new(model.User)
var err = meddler.QueryRow(db, usr, userTokenQuery)
return usr, err
}
// GetUserList retrieves a list of all users from
// the datastore that are registered in the system.
func (db *Repostore) GetUserList() ([]*model.User, error) {
var users []*model.User
var err = meddler.QueryAll(db, &users, userListQuery)
return users, err
}
// PostUser saves a User in the datastore.
func (db *Repostore) PostUser(user *model.User) error {
return meddler.Save(db, userTable, user)
}
// PutUser saves a user in the datastore.
func (db *Repostore) PutUser(user *model.User) error {
return meddler.Save(db, userTable, user)
}
// DelUser removes the user from the datastore.
func (db *Repostore) DelUser(user *model.User) error {
var _, err = db.Exec(userDeleteStmt, user.ID)
return err
}
// User table name in database.
const userTable = "users"
// SQL query to retrieve a User by remote login.
const userLoginQuery = `
SELECT *
FROM users
WHERE user_remote=?
AND user_login=?
LIMIT 1
`
// SQL query to retrieve a User by remote login.
const userTokenQuery = `
SELECT *
FROM users
WHERE user_token=?
LIMIT 1
`
// SQL query to retrieve a list of all users.
const userListQuery = `
SELECT *
FROM users
ORDER BY user_name ASC
`
// SQL statement to delete a User by ID.
const userDeleteStmt = `
DELETE FROM users
WHERE user_id=?
`

View file

@ -0,0 +1 @@
package datasql

View file

@ -0,0 +1,8 @@
package datastore
type Datastore interface {
Userstore
Permstore
Repostore
Commitstore
}

42
server/datastore/perm.go Normal file
View file

@ -0,0 +1,42 @@
package datastore
import (
"code.google.com/p/go.net/context"
"github.com/drone/drone/shared/model"
)
type Permstore interface {
// GetPerm retrieves the User's permission from
// the datastore for the given repository.
GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error)
// PostPerm saves permission in the datastore.
PostPerm(perm *model.Perm) error
// PutPerm saves permission in the datastore.
PutPerm(perm *model.Perm) error
// DelPerm removes permission from the datastore.
DelPerm(perm *model.Perm) error
}
// GetPerm retrieves the User's permission from
// the datastore for the given repository.
func GetPerm(c context.Context, user *model.User, repo *model.Repo) (*model.Perm, error) {
return FromContext(c).GetPerm(user, repo)
}
// PostPerm saves permission in the datastore.
func PostPerm(c context.Context, perm *model.Perm) error {
return FromContext(c).PostPerm(perm)
}
// PutPerm saves permission in the datastore.
func PutPerm(c context.Context, perm *model.Perm) error {
return FromContext(c).PutPerm(perm)
}
// DelPerm removes permission from the datastore.
func DelPerm(c context.Context, perm *model.Perm) error {
return FromContext(c).DelPerm(perm)
}

62
server/datastore/repo.go Normal file
View file

@ -0,0 +1,62 @@
package datastore
import (
"code.google.com/p/go.net/context"
"github.com/drone/drone/shared/model"
)
type Repostore interface {
// GetRepo retrieves a specific repo from the
// datastore for the given ID.
GetRepo(id int64) (*model.Repo, error)
// GetRepoName retrieves a repo from the datastore
// for the specified remote, owner and name.
GetRepoName(remote, owner, name string) (*model.Repo, error)
// GetRepoList retrieves a list of all repos from
// the datastore accessible by the given user ID.
GetRepoList(user *model.User) ([]*model.Repo, error)
// PostRepo saves a repo in the datastore.
PostRepo(repo *model.Repo) error
// PutRepo saves a repo in the datastore.
PutRepo(repo *model.Repo) error
// DelRepo removes the repo from the datastore.
DelRepo(repo *model.Repo) error
}
// GetRepo retrieves a specific repo from the
// datastore for the given ID.
func GetRepo(c context.Context, id int64) (*model.Repo, error) {
return FromContext(c).GetRepo(id)
}
// GetRepoName retrieves a repo from the datastore
// for the specified remote, owner and name.
func GetRepoName(c context.Context, remote, owner, name string) (*model.Repo, error) {
return FromContext(c).GetRepoName(remote, owner, name)
}
// GetRepoList retrieves a list of all repos from
// the datastore accessible by the given user ID.
func GetRepoList(c context.Context, user *model.User) ([]*model.Repo, error) {
return FromContext(c).GetRepoList(user)
}
// PostRepo saves a repo in the datastore.
func PostRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).PostRepo(repo)
}
// PutRepo saves a repo in the datastore.
func PutRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).PutRepo(repo)
}
// DelRepo removes the repo from the datastore.
func DelRepo(c context.Context, repo *model.Repo) error {
return FromContext(c).DelRepo(repo)
}

72
server/datastore/user.go Normal file
View file

@ -0,0 +1,72 @@
package datastore
import (
"code.google.com/p/go.net/context"
"github.com/drone/drone/shared/model"
)
type Userstore interface {
// GetUser retrieves a specific user from the
// datastore for the given ID.
GetUser(id int64) (*model.User, error)
// GetUserLogin retrieves a user from the datastore
// for the specified remote and login name.
GetUserLogin(remote, login string) (*model.User, error)
// GetUserToken retrieves a user from the datastore
// with the specified token.
GetUserToken(token string) (*model.User, error)
// GetUserList retrieves a list of all users from
// the datastore that are registered in the system.
GetUserList() ([]*model.User, error)
// PostUser saves a User in the datastore.
PostUser(user *model.User) error
// PutUser saves a user in the datastore.
PutUser(user *model.User) error
// DelUser removes the user from the datastore.
DelUser(user *model.User) error
}
// GetUser retrieves a specific user from the
// datastore for the given ID.
func GetUser(c context.Context, id int64) (*model.User, error) {
return FromContext(c).GetUser(id)
}
// GetUserLogin retrieves a user from the datastore
// for the specified remote and login name.
func GetUserLogin(c context.Context, remote, login string) (*model.User, error) {
return FromContext(c).GetUserLogin(remote, login)
}
// GetUserToken retrieves a user from the datastore
// with the specified token.
func GetUserToken(c context.Context, token string) (*model.User, error) {
return FromContext(c).GetUserToken(token)
}
// GetUserList retrieves a list of all users from
// the datastore that are registered in the system.
func GetUserList(c context.Context) ([]*model.User, error) {
return FromContext(c).GetUserList()
}
// PostUser saves a User in the datastore.
func PostUser(c context.Context, user *model.User) error {
return FromContext(c).PostUser(user)
}
// PutUser saves a user in the datastore.
func PutUser(c context.Context, user *model.User) error {
return FromContext(c).PutUser(user)
}
// DelUser removes the user from the datastore.
func DelUser(c context.Context, user *model.User) error {
return FromContext(c).DelUser(user)
}