mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
feat(be): implemented all Store interface methods by BoltDb implementation
This commit is contained in:
parent
6a652be1f4
commit
dade5a3a7b
@ -189,3 +189,12 @@ var SessionObject = ObjectProperties{
|
||||
var TokenObject = ObjectProperties{
|
||||
TableName: "user__token",
|
||||
}
|
||||
|
||||
var TaskObject = ObjectProperties{
|
||||
TableName: "task",
|
||||
IsGlobal: true,
|
||||
}
|
||||
|
||||
var TaskOutputObject = ObjectProperties{
|
||||
TableName: "task__output",
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import "time"
|
||||
type Task struct {
|
||||
ID int `db:"id" json:"id"`
|
||||
TemplateID int `db:"template_id" json:"template_id" binding:"required"`
|
||||
ProjectID int `db:"project_id" json:"project_id"`
|
||||
|
||||
Status string `db:"status" json:"status"`
|
||||
Debug bool `db:"debug" json:"debug"`
|
||||
|
@ -156,7 +156,7 @@ func sortObjects(objects interface{}, sortBy string, sortInverted bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.RetrieveQueryParams, objects interface{}) (err error) {
|
||||
func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.RetrieveQueryParams, filter func(interface{}) bool, objects interface{}) (err error) {
|
||||
objectsValue := reflect.ValueOf(objects).Elem()
|
||||
objType := objectsValue.Type().Elem()
|
||||
|
||||
@ -175,6 +175,12 @@ func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.R
|
||||
return err
|
||||
}
|
||||
|
||||
if filter != nil {
|
||||
if !filter(obj) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
objectsValue.Set(reflect.Append(objectsValue, obj))
|
||||
|
||||
n++
|
||||
@ -206,13 +212,13 @@ func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.R
|
||||
return
|
||||
}
|
||||
|
||||
func (d *BoltDb) getObjects(bucketID int, props db.ObjectProperties, params db.RetrieveQueryParams, objects interface{}) error {
|
||||
func (d *BoltDb) getObjects(bucketID int, props db.ObjectProperties, params db.RetrieveQueryParams, filter func(interface{}) bool, objects interface{}) error {
|
||||
return d.db.View(func(tx *bbolt.Tx) error {
|
||||
|
||||
b := tx.Bucket(makeBucketId(props, bucketID))
|
||||
c := b.Cursor()
|
||||
|
||||
return unmarshalObjects(c, props, params, objects)
|
||||
return unmarshalObjects(c, props, params, filter, objects)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ func (d *BoltDb) GetAccessKey(projectID int, accessKeyID int) (db.AccessKey, err
|
||||
|
||||
func (d *BoltDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) {
|
||||
var keys []db.AccessKey
|
||||
err := d.getObjects(projectID, db.AccessKeyObject, params, &keys)
|
||||
err := d.getObjects(projectID, db.AccessKeyObject, params, nil, &keys)
|
||||
return keys, err
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ func (d *BoltDb) GetGlobalAccessKey(accessKeyID int) (key db.AccessKey, err erro
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetGlobalAccessKeys(params db.RetrieveQueryParams) (keys []db.AccessKey, err error) {
|
||||
err = d.getObjects(0, db.GlobalAccessKeyObject, params, &keys)
|
||||
err = d.getObjects(0, db.GlobalAccessKeyObject, params, nil, &keys)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ func (d *BoltDb) GetEnvironment(projectID int, environmentID int) (environment d
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) (environment []db.Environment, err error) {
|
||||
err = d.getObjects(projectID, db.EnvironmentObject, params, &environment)
|
||||
err = d.getObjects(projectID, db.EnvironmentObject, params, nil, &environment)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inve
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetInventories(projectID int, params db.RetrieveQueryParams) (inventories []db.Inventory, err error) {
|
||||
err = d.getObjects(projectID, db.AccessKeyObject, params, &inventories)
|
||||
err = d.getObjects(projectID, db.AccessKeyObject, params, nil, &inventories)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ func (d *BoltDb) CreateProject(project db.Project) (db.Project, error) {
|
||||
func (d *BoltDb) GetProjects(userID int) (projects []db.Project, err error) {
|
||||
var allProjects []db.Project
|
||||
|
||||
err = d.getObjects(0, db.ProjectObject, db.RetrieveQueryParams{}, &allProjects)
|
||||
err = d.getObjects(0, db.ProjectObject, db.RetrieveQueryParams{}, nil, &allProjects)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -10,7 +10,7 @@ func (d *BoltDb) GetRepository(projectID int, repositoryID int) (repository db.R
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetRepositories(projectID int, params db.RetrieveQueryParams) (repositories []db.Repository, err error) {
|
||||
err = d.getObjects(projectID, db.RepositoryObject, params, &repositories)
|
||||
err = d.getObjects(projectID, db.RepositoryObject, params, nil, &repositories)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ func (d *BoltDb) TouchSession(userID int, sessionID int) (err error) {
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) {
|
||||
err = d.getObjects(userID, db.SessionObject, db.RetrieveQueryParams{}, &tokens)
|
||||
err = d.getObjects(userID, db.SessionObject, db.RetrieveQueryParams{}, nil, &tokens)
|
||||
return
|
||||
}
|
||||
|
||||
|
108
db/bolt/task.go
108
db/bolt/task.go
@ -1,78 +1,83 @@
|
||||
package bolt
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/masterminds/squirrel"
|
||||
"go.etcd.io/bbolt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *BoltDb) CreateTask(task db.Task) (db.Task, error) {
|
||||
err := d.sql.Insert(&task)
|
||||
return task, err
|
||||
func (d *BoltDb) CreateTask(task db.Task) (newTask db.Task, err error) {
|
||||
task.Created = time.Now()
|
||||
err = d.getObject(0, db.TaskObject, intObjectID(task.ID), &newTask)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *BoltDb) UpdateTask(task db.Task) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update task set status=?, start=?, end=? where id=?",
|
||||
task.Status,
|
||||
task.Start,
|
||||
task.End,
|
||||
task.ID)
|
||||
|
||||
return err
|
||||
return d.updateObject(0, db.TaskObject, task)
|
||||
}
|
||||
|
||||
func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
|
||||
_, err := d.sql.Exec(
|
||||
"insert into task__output (task_id, task, output, time) VALUES (?, '', ?, ?)",
|
||||
output.TaskID,
|
||||
output.Output,
|
||||
output.Time)
|
||||
return output, err
|
||||
newOutput, err := d.createObject(output.TaskID, db.TaskOutputObject, output)
|
||||
if err != nil {
|
||||
return db.TaskOutput{}, err
|
||||
}
|
||||
return newOutput.(db.TaskOutput), nil
|
||||
}
|
||||
|
||||
func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
|
||||
q := squirrel.Select("task.*, tpl.playbook as tpl_playbook, user.name as user_name, tpl.alias as tpl_alias").
|
||||
From("task").
|
||||
Join("project__template as tpl on task.template_id=tpl.id").
|
||||
LeftJoin("user on task.user_id=user.id").
|
||||
OrderBy("task.created desc, id desc")
|
||||
err = d.getObjects(0, db.TaskObject, params, func (tsk interface{}) bool {
|
||||
task := tsk.(db.TaskWithTpl)
|
||||
|
||||
if templateID == nil {
|
||||
q = q.Where("tpl.project_id=?", projectID)
|
||||
} else {
|
||||
q = q.Where("tpl.project_id=? AND task.template_id=?", projectID, templateID)
|
||||
if task.ProjectID != projectID {
|
||||
return false
|
||||
}
|
||||
|
||||
if params.Count > 0 {
|
||||
q = q.Limit(uint64(params.Count))
|
||||
if templateID != nil && task.TemplateID != *templateID {
|
||||
return false
|
||||
}
|
||||
|
||||
query, args, _ := q.ToSql()
|
||||
return true
|
||||
}, &tasks)
|
||||
|
||||
_, err = d.sql.Select(&tasks, query, args...)
|
||||
var templates = make(map[int]db.Template)
|
||||
var users = make(map[int]db.User)
|
||||
|
||||
for _, task := range tasks {
|
||||
tpl, ok := templates[task.TemplateID]
|
||||
if !ok {
|
||||
tpl, err = d.GetTemplate(task.ProjectID, task.TemplateID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
templates[task.TemplateID] = tpl
|
||||
}
|
||||
task.TemplatePlaybook = tpl.Playbook
|
||||
task.TemplateAlias = tpl.Alias
|
||||
if task.UserID != nil {
|
||||
usr, ok := users[*task.UserID]
|
||||
if !ok {
|
||||
usr, err = d.GetUser(*task.UserID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
users[*task.UserID] = usr
|
||||
}
|
||||
task.UserName = &usr.Name
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetTask(projectID int, taskID int) (task db.Task, err error) {
|
||||
q := squirrel.Select("task.*").
|
||||
From("task").
|
||||
Join("project__template as tpl on task.template_id=tpl.id").
|
||||
Where("tpl.project_id=? AND task.id=?", projectID, taskID)
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
err = d.getObject(0, db.TaskObject, intObjectID(taskID), &task)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = d.sql.SelectOne(&task, query, args...)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
if task.ProjectID != projectID {
|
||||
task = db.Task{}
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -92,17 +97,19 @@ func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Exec("delete from task__output where task_id=?", taskID)
|
||||
|
||||
err = d.deleteObject(0, db.TaskObject, intObjectID(taskID))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Exec("delete from task where id=?", taskID)
|
||||
_ = d.db.Update(func (tx *bbolt.Tx) error {
|
||||
return tx.DeleteBucket(makeBucketId(db.TaskOutputObject, taskID))
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (output []db.TaskOutput, err error) {
|
||||
func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (outputs []db.TaskOutput, err error) {
|
||||
// check if task exists in the project
|
||||
_, err = d.GetTask(projectID, taskID)
|
||||
|
||||
@ -110,8 +117,7 @@ func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (output []db.TaskOutp
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&output,
|
||||
"select task_id, task, time, output from task__output where task_id=? order by time asc",
|
||||
taskID)
|
||||
err = d.getObjects(taskID, db.TaskOutputObject, db.RetrieveQueryParams{}, nil, &outputs)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ func (d *BoltDb) GetProjectUser(projectID, userID int) (user db.ProjectUser, err
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetProjectUsers(projectID int, params db.RetrieveQueryParams) (users []db.User, err error) {
|
||||
err = d.getObjects(projectID, db.ProjectUserObject, params, &users)
|
||||
err = d.getObjects(projectID, db.ProjectUserObject, params, nil, &users)
|
||||
return
|
||||
}
|
||||
|
||||
@ -128,13 +128,13 @@ func (d *BoltDb) GetUser(userID int) (user db.User, err error) {
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetUsers(params db.RetrieveQueryParams) (users []db.User, err error) {
|
||||
err = d.getObjects(0, db.UserObject, params, &users)
|
||||
err = d.getObjects(0, db.UserObject, params, nil, &users)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *BoltDb) GetUserByLoginOrEmail(login string, email string) (existingUser db.User, err error) {
|
||||
var users []db.User
|
||||
err = d.getObjects(0, db.UserObject, db.RetrieveQueryParams{}, &users)
|
||||
err = d.getObjects(0, db.UserObject, db.RetrieveQueryParams{}, nil, &users)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -74,5 +74,6 @@ func init() {
|
||||
{Major: 2, Minor: 4},
|
||||
{Major: 2, Minor: 5},
|
||||
{Major: 2, Minor: 5, Patch: 2},
|
||||
{Major: 2, Minor: 7, Patch: 1},
|
||||
}
|
||||
}
|
||||
|
1
db/sql/migrations/v2.7.1.sql
Normal file
1
db/sql/migrations/v2.7.1.sql
Normal file
@ -0,0 +1 @@
|
||||
alter table `task` add `project_id` int DEFAULT 0;
|
Loading…
Reference in New Issue
Block a user