Skip to content

Commit

Permalink
feat(be): physical delete template from database
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Jan 31, 2022
1 parent 221454a commit 8e8ca94
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ web2/dist/**/*
/.dredd/config.json
/database.boltdb
/database.boltdb.lock
/database2.boltdb
.DS_Store
node_modules/

Expand Down
12 changes: 9 additions & 3 deletions db/bolt/BoltDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (d *BoltDb) isObjectInUse(bucketID int, objProps db.ObjectProperties, objID
return
}

func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProperties, objectID objectID) error {
func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProperties, objectID objectID, tx *bbolt.Tx) error {
for _, u := range []db.ObjectProperties{db.TemplateProps, db.EnvironmentProps, db.InventoryProps, db.RepositoryProps} {
inUse, err := d.isObjectInUse(bucketID, props, objectID, u)
if err != nil {
Expand All @@ -407,13 +407,19 @@ func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProperties, objectID
}
}

return d.db.Update(func(tx *bbolt.Tx) error {
fn := func(tx *bbolt.Tx) error {
b := tx.Bucket(makeBucketId(props, bucketID))
if b == nil {
return db.ErrNotFound
}
return b.Delete(objectID.ToBytes())
})
}

if tx != nil {
return fn(tx)
}

return d.db.Update(fn)
}

func (d *BoltDb) deleteObjectSoft(bucketID int, props db.ObjectProperties, objectID objectID) error {
Expand Down
6 changes: 3 additions & 3 deletions db/bolt/access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (d *BoltDb) UpdateAccessKey(key db.AccessKey) error {
return d.updateObject(*key.ProjectID, db.AccessKeyProps, key)
}

func (d *BoltDb) CreateAccessKey(key db.AccessKey) (db.AccessKey, error) {
func (d *BoltDb) CreateAccessKey(key db.AccessKey) (db.AccessKey, error) {
err := key.SerializeSecret()
if err != nil {
return db.AccessKey{}, err
Expand All @@ -53,9 +53,9 @@ func (d *BoltDb) CreateAccessKey(key db.AccessKey) (db.AccessKey, error) {
}

func (d *BoltDb) DeleteAccessKey(projectID int, accessKeyID int) error {
return d.deleteObject(projectID, db.AccessKeyProps, intObjectID(accessKeyID))
return d.deleteObject(projectID, db.AccessKeyProps, intObjectID(accessKeyID), nil)
}

func (d *BoltDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error {
return d.deleteObjectSoft(projectID, db.AccessKeyProps, intObjectID(accessKeyID))
}
}
2 changes: 1 addition & 1 deletion db/bolt/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (d *BoltDb) CreateEnvironment(env db.Environment) (db.Environment, error) {
}

func (d *BoltDb) DeleteEnvironment(projectID int, environmentID int) error {
return d.deleteObject(projectID, db.EnvironmentProps, intObjectID(environmentID))
return d.deleteObject(projectID, db.EnvironmentProps, intObjectID(environmentID), nil)
}

func (d *BoltDb) DeleteEnvironmentSoft(projectID int, environmentID int) error {
Expand Down
6 changes: 1 addition & 5 deletions db/bolt/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/ansible-semaphore/semaphore/db"
)


func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) {
err = d.getObject(projectID, db.InventoryProps, intObjectID(inventoryID), &inventory)

Expand All @@ -22,7 +21,7 @@ func (d *BoltDb) GetInventories(projectID int, params db.RetrieveQueryParams) (i
}

func (d *BoltDb) DeleteInventory(projectID int, inventoryID int) error {
return d.deleteObject(projectID, db.InventoryProps, intObjectID(inventoryID))
return d.deleteObject(projectID, db.InventoryProps, intObjectID(inventoryID), nil)
}

func (d *BoltDb) DeleteInventorySoft(projectID int, inventoryID int) error {
Expand All @@ -37,6 +36,3 @@ func (d *BoltDb) CreateInventory(inventory db.Inventory) (db.Inventory, error) {
newInventory, err := d.createObject(inventory.ProjectID, db.InventoryProps, inventory)
return newInventory.(db.Inventory), err
}



2 changes: 1 addition & 1 deletion db/bolt/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (d *BoltDb) GetProject(projectID int) (project db.Project, err error) {
}

func (d *BoltDb) DeleteProject(projectID int) error {
return d.deleteObject(0, db.ProjectProps, intObjectID(projectID))
return d.deleteObject(0, db.ProjectProps, intObjectID(projectID), nil)
}

func (d *BoltDb) UpdateProject(project db.Project) error {
Expand Down
2 changes: 1 addition & 1 deletion db/bolt/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *BoltDb) CreateRepository(repository db.Repository) (db.Repository, erro
}

func (d *BoltDb) DeleteRepository(projectID int, repositoryId int) error {
return d.deleteObject(projectID, db.RepositoryProps, intObjectID(repositoryId))
return d.deleteObject(projectID, db.RepositoryProps, intObjectID(repositoryId), nil)
}

func (d *BoltDb) DeleteRepositorySoft(projectID int, repositoryId int) error {
Expand Down
13 changes: 11 additions & 2 deletions db/bolt/schedule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bolt

import "github.com/ansible-semaphore/semaphore/db"
import (
"github.com/ansible-semaphore/semaphore/db"
"go.etcd.io/bbolt"
)

func (d *BoltDb) GetSchedules() (schedules []db.Schedule, err error) {
var allProjects []db.Project
Expand Down Expand Up @@ -63,8 +66,14 @@ func (d *BoltDb) GetSchedule(projectID int, scheduleID int) (schedule db.Schedul
return
}

func (d *BoltDb) deleteSchedule(projectID int, scheduleID int, tx *bbolt.Tx) error {
return d.deleteObject(projectID, db.ScheduleProps, intObjectID(scheduleID), tx)
}

func (d *BoltDb) DeleteSchedule(projectID int, scheduleID int) error {
return d.deleteObject(projectID, db.ScheduleProps, intObjectID(scheduleID))
return d.db.Update(func(tx *bbolt.Tx) error {
return d.deleteSchedule(projectID, scheduleID, tx)
})
}

func (d *BoltDb) SetScheduleCommitHash(projectID int, scheduleID int, hash string) error {
Expand Down
15 changes: 8 additions & 7 deletions db/bolt/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,25 @@ func (d *BoltDb) GetProjectTasks(projectID int, params db.RetrieveQueryParams) (
return d.getTasks(projectID, nil, params)
}

func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) {
func (d *BoltDb) deleteTaskWithOutputs(projectID int, taskID int, tx *bbolt.Tx) (err error) {
// check if task exists in the project
_, err = d.GetTask(projectID, taskID)

if err != nil {
return
}

err = d.deleteObject(0, db.TaskProps, intObjectID(taskID))
err = d.deleteObject(0, db.TaskProps, intObjectID(taskID), tx)
if err != nil {
return
}

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

return
func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) error {
return d.db.Update(func(tx *bbolt.Tx) error {
return d.deleteTaskWithOutputs(projectID, taskID, tx)
})
}

func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (outputs []db.TaskOutput, err error) {
Expand Down
31 changes: 30 additions & 1 deletion db/bolt/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bolt

import (
"github.com/ansible-semaphore/semaphore/db"
"go.etcd.io/bbolt"
)

func (d *BoltDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
Expand Down Expand Up @@ -73,6 +74,34 @@ func (d *BoltDb) GetTemplate(projectID int, templateID int) (template db.Templat
return
}

func (d *BoltDb) deleteTemplate(projectID int, templateID int, tx *bbolt.Tx) (err error) {
tasks, err := d.GetTemplateTasks(projectID, templateID, db.RetrieveQueryParams{})
if err != nil {
return
}
for _, task := range tasks {
err = d.deleteTaskWithOutputs(projectID, task.ID, tx)
if err != nil {
return
}
}

schedules, err := d.GetTemplateSchedules(projectID, templateID)
if err != nil {
return
}
for _, sch := range schedules {
err = d.deleteSchedule(projectID, sch.ID, tx)
if err != nil {
return
}
}

return d.deleteObject(projectID, db.TemplateProps, intObjectID(templateID), tx)
}

func (d *BoltDb) DeleteTemplate(projectID int, templateID int) error {
return d.deleteObject(projectID, db.TemplateProps, intObjectID(templateID))
return d.db.Update(func(tx *bbolt.Tx) error {
return d.deleteTemplate(projectID, templateID, tx)
})
}
4 changes: 2 additions & 2 deletions db/bolt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (d *BoltDb) DeleteUser(userID int) error {
_ = d.DeleteProjectUser(p.ID, userID)
}

return d.deleteObject(0, db.UserProps, intObjectID(userID))
return d.deleteObject(0, db.UserProps, intObjectID(userID), nil)
}

func (d *BoltDb) UpdateUser(user db.UserWithPwd) error {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (d *BoltDb) UpdateProjectUser(projectUser db.ProjectUser) error {
}

func (d *BoltDb) DeleteProjectUser(projectID, userID int) error {
return d.deleteObject(projectID, db.ProjectUserProps, intObjectID(userID))
return d.deleteObject(projectID, db.ProjectUserProps, intObjectID(userID), nil)
}

//GetUser retrieves a user from the database by ID
Expand Down
2 changes: 1 addition & 1 deletion db/bolt/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (d *BoltDb) CreateView(view db.View) (db.View, error) {
}

func (d *BoltDb) DeleteView(projectID int, viewID int) error {
return d.deleteObject(projectID, db.ViewProps, intObjectID(viewID))
return d.deleteObject(projectID, db.ViewProps, intObjectID(viewID), nil)
}

func (d *BoltDb) SetViewPositions(projectID int, positions map[int]int) error {
Expand Down
3 changes: 1 addition & 2 deletions db/sql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func (d *SqlDb) GetTemplate(projectID int, templateID int) (template db.Template
}

func (d *SqlDb) DeleteTemplate(projectID int, templateID int) error {
_, err := d.exec("update project__template set removed=true where project_id=? and id=?", projectID, templateID)

_, err := d.exec("delete from project__template where project_id=? and id=?", projectID, templateID)
return err
}

0 comments on commit 8e8ca94

Please sign in to comment.