Skip to content

Commit

Permalink
feat: tasks for BoltDB
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Jun 24, 2021
1 parent c1853fa commit 482d244
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 52 deletions.
28 changes: 13 additions & 15 deletions db/Store.go
Expand Up @@ -6,8 +6,6 @@ import (
"time"
)



const databaseTimeFormat = "2006-01-02T15:04:05:99Z"

// GetParsedTime returns the timestamp as it will retrieved from the database
Expand Down Expand Up @@ -172,45 +170,45 @@ var RepositoryProps = ObjectProperties{
}

var TemplateProps = ObjectProperties{
TableName: "project__template",
SortableColumns: []string{"name"},
TableName: "project__template",
SortableColumns: []string{"name"},
PrimaryColumnName: "id",
}

var ProjectUserProps = ObjectProperties{
TableName: "project__user",
TableName: "project__user",
PrimaryColumnName: "user_id",
}

var ProjectProps = ObjectProperties{
TableName: "project",
IsGlobal: true,
TableName: "project",
IsGlobal: true,
PrimaryColumnName: "id",
}

var UserProps = ObjectProperties{
TableName: "user",
IsGlobal: true,
TableName: "user",
IsGlobal: true,
PrimaryColumnName: "id",
}

var SessionProps = ObjectProperties{
TableName: "session",
TableName: "session",
PrimaryColumnName: "id",
}

var TokenProps = ObjectProperties{
TableName: "user__token",
TableName: "user__token",
PrimaryColumnName: "id",
}

var TaskProps = ObjectProperties{
TableName: "task",
IsGlobal: true,
TableName: "task",
IsGlobal: true,
PrimaryColumnName: "id",
}

var TaskOutputProps = ObjectProperties{
TableName: "task__output",
PrimaryColumnName: "id",
TableName: "task__output",
PrimaryColumnName: "",
}
1 change: 1 addition & 0 deletions db/Task.go
Expand Up @@ -26,6 +26,7 @@ type Task struct {
End *time.Time `db:"end" json:"end"`
}

// TaskWithTpl is the task data with additional fields
type TaskWithTpl struct {
Task
TemplatePlaybook string `db:"tpl_playbook" json:"tpl_playbook"`
Expand Down
66 changes: 38 additions & 28 deletions db/bolt/BoltDb.go
Expand Up @@ -502,46 +502,56 @@ func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object in
tmpObj := reflect.New(objPtr.Elem().Type()).Elem()
tmpObj.Set(objPtr.Elem())

idFieldName, err := getFieldNameByTag(reflect.TypeOf(object), "db", props.PrimaryColumnName)
var objectID objectID

if err != nil {
return err
}
if props.PrimaryColumnName != "" {
idFieldName, err := getFieldNameByTag(reflect.TypeOf(object), "db", props.PrimaryColumnName)

idValue := tmpObj.FieldByName(idFieldName)
var objectID objectID
switch idValue.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
if idValue.Int() == 0 {
if err != nil {
return err
}

idValue := tmpObj.FieldByName(idFieldName)

switch idValue.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
if idValue.Int() == 0 {
id, err2 := b.NextSequence()
if err2 != nil {
return err2
}
idValue.SetInt(int64(id))
}
objectID = intObjectID(idValue.Int())
case reflect.String:
if idValue.String() == "" {
return fmt.Errorf("object ID can not be empty string")
}
objectID = strObjectID(idValue.String())
case reflect.Invalid:
id, err2 := b.NextSequence()
if err2 != nil {
return err2
}
idValue.SetInt(int64(id))
objectID = intObjectID(id)
default:
return fmt.Errorf("unsupported ID type")
}
objectID = intObjectID(idValue.Int())
case reflect.String:
if idValue.String() == "" {
return fmt.Errorf("object ID can not be empty string")
}
objectID = strObjectID(idValue.String())
case reflect.Invalid:
} else {
id, err2 := b.NextSequence()
if err2 != nil {
return err2
}
objectID = intObjectID(id)
default:
return fmt.Errorf("unsupported ID type")
}

if objectID == nil {
Expand Down
1 change: 1 addition & 0 deletions db/bolt/repository.go
Expand Up @@ -6,6 +6,7 @@ import (

func (d *BoltDb) GetRepository(projectID int, repositoryID int) (repository db.Repository, err error) {
err = d.getObject(projectID, db.RepositoryProps, intObjectID(repositoryID), &repository)
repository.SSHKey, err = d.GetAccessKey(projectID, repository.SSHKeyID)
return
}

Expand Down
26 changes: 17 additions & 9 deletions db/bolt/task.go
Expand Up @@ -8,7 +8,11 @@ import (

func (d *BoltDb) CreateTask(task db.Task) (newTask db.Task, err error) {
task.Created = time.Now()
err = d.getObject(0, db.TaskProps, intObjectID(task.ID), &newTask)
res, err := d.createObject(0, db.TaskProps, task)
if err != nil {
return
}
newTask = res.(db.Task)
return
}

Expand All @@ -24,9 +28,11 @@ func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
return newOutput.(db.TaskOutput), nil
}

func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
err = d.getObjects(0, db.TaskProps, params, func (tsk interface{}) bool {
task := tsk.(db.TaskWithTpl)
func (d *BoltDb) getTasks(projectID int, templateID *int, params db.RetrieveQueryParams) (tasksWithTpl []db.TaskWithTpl, err error) {
var tasks []db.Task

err = d.getObjects(0, db.TaskProps, params, func(tsk interface{}) bool {
task := tsk.(db.Task)

if task.ProjectID != projectID {
return false
Expand All @@ -42,7 +48,8 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer
var templates = make(map[int]db.Template)
var users = make(map[int]db.User)

for _, task := range tasks {
tasksWithTpl = make([]db.TaskWithTpl, len(tasks))
for i, task := range tasks {
tpl, ok := templates[task.TemplateID]
if !ok {
tpl, err = d.GetTemplate(task.ProjectID, task.TemplateID)
Expand All @@ -51,8 +58,9 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer
}
templates[task.TemplateID] = tpl
}
task.TemplatePlaybook = tpl.Playbook
task.TemplateAlias = tpl.Alias
tasksWithTpl[i] = db.TaskWithTpl{Task: task}
tasksWithTpl[i].TemplatePlaybook = tpl.Playbook
tasksWithTpl[i].TemplateAlias = tpl.Alias
if task.UserID != nil {
usr, ok := users[*task.UserID]
if !ok {
Expand All @@ -62,7 +70,7 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer
}
users[*task.UserID] = usr
}
task.UserName = &usr.Name
tasksWithTpl[i].UserName = &usr.Name
}
}

Expand Down Expand Up @@ -102,7 +110,7 @@ func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) {
return
}

_ = d.db.Update(func (tx *bbolt.Tx) error {
_ = d.db.Update(func(tx *bbolt.Tx) error {
return tx.DeleteBucket(makeBucketId(db.TaskOutputProps, taskID))
})

Expand Down

0 comments on commit 482d244

Please sign in to comment.