feat(be): physical delete template from database

This commit is contained in:
Denis Gukov 2022-02-01 03:16:00 +05:00
parent 221454a89f
commit 8e8ca94126
13 changed files with 71 additions and 30 deletions

1
.gitignore vendored
View File

@ -10,6 +10,7 @@ web2/dist/**/*
/.dredd/config.json
/database.boltdb
/database.boltdb.lock
/database2.boltdb
.DS_Store
node_modules/

View File

@ -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 {

View File

@ -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))
}
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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) {

View File

@ -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)
})
}

View File

@ -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

View File

@ -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 {

View File

@ -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
}