diff --git a/.gitignore b/.gitignore index 947dceed..2ba029c9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ web2/dist/**/* /.dredd/config.json /database.boltdb /database.boltdb.lock +/database2.boltdb .DS_Store node_modules/ diff --git a/db/bolt/BoltDb.go b/db/bolt/BoltDb.go index bcdf16ea..814cba69 100644 --- a/db/bolt/BoltDb.go +++ b/db/bolt/BoltDb.go @@ -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 { @@ -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 { diff --git a/db/bolt/access_key.go b/db/bolt/access_key.go index c789bde6..086f77fe 100644 --- a/db/bolt/access_key.go +++ b/db/bolt/access_key.go @@ -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 @@ -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)) -} \ No newline at end of file +} diff --git a/db/bolt/environment.go b/db/bolt/environment.go index de82f9e5..283bf770 100644 --- a/db/bolt/environment.go +++ b/db/bolt/environment.go @@ -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 { diff --git a/db/bolt/inventory.go b/db/bolt/inventory.go index b0980024..1189e817 100644 --- a/db/bolt/inventory.go +++ b/db/bolt/inventory.go @@ -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) @@ -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 { @@ -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 } - - - diff --git a/db/bolt/project.go b/db/bolt/project.go index 509d0832..b36c0e20 100644 --- a/db/bolt/project.go +++ b/db/bolt/project.go @@ -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 { diff --git a/db/bolt/repository.go b/db/bolt/repository.go index 8f387c51..5f7033f2 100644 --- a/db/bolt/repository.go +++ b/db/bolt/repository.go @@ -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 { diff --git a/db/bolt/schedule.go b/db/bolt/schedule.go index 4af78872..ba928e9f 100644 --- a/db/bolt/schedule.go +++ b/db/bolt/schedule.go @@ -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 @@ -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 { diff --git a/db/bolt/task.go b/db/bolt/task.go index 4c98f7a9..992974df 100644 --- a/db/bolt/task.go +++ b/db/bolt/task.go @@ -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)) +} + +func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) error { + return d.db.Update(func(tx *bbolt.Tx) error { + return d.deleteTaskWithOutputs(projectID, taskID, tx) }) - - return } func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (outputs []db.TaskOutput, err error) { diff --git a/db/bolt/template.go b/db/bolt/template.go index c4b739a6..b9958b55 100644 --- a/db/bolt/template.go +++ b/db/bolt/template.go @@ -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) { @@ -73,6 +74,34 @@ func (d *BoltDb) GetTemplate(projectID int, templateID int) (template db.Templat return } -func (d *BoltDb) DeleteTemplate(projectID int, templateID int) error { - return d.deleteObject(projectID, db.TemplateProps, intObjectID(templateID)) +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.db.Update(func(tx *bbolt.Tx) error { + return d.deleteTemplate(projectID, templateID, tx) + }) } diff --git a/db/bolt/user.go b/db/bolt/user.go index 8cb27ec8..037ca649 100644 --- a/db/bolt/user.go +++ b/db/bolt/user.go @@ -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 { @@ -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 diff --git a/db/bolt/view.go b/db/bolt/view.go index 9221e775..cc184260 100644 --- a/db/bolt/view.go +++ b/db/bolt/view.go @@ -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 { diff --git a/db/sql/template.go b/db/sql/template.go index 36510062..e1d3130a 100644 --- a/db/sql/template.go +++ b/db/sql/template.go @@ -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 }