Skip to content

Commit

Permalink
Fix draft images being deleted while they're still in use (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash authored Feb 29, 2024
1 parent 59a6e28 commit 7ad379e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
7 changes: 5 additions & 2 deletions pkg/draft/draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ func Destroy(fac models.Repo, id uuid.UUID) error {
return fmt.Errorf("Unsupported type: %s", draft.Type)
}

if err = dqb.Destroy(id); err != nil {
return err
}

if imageID != nil {
imageService := image.GetService(fac.Image())
if err := imageService.DestroyUnusedImage(*imageID); err != nil {
return err
}
}

return dqb.Destroy(id)
return nil
}
35 changes: 22 additions & 13 deletions pkg/sqlx/querybuilder_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ func (qb *imageQueryBuilder) FindUnused() ([]*models.Image, error) {
FROM edits
WHERE status = 'PENDING'
) edit_images ON edit_images.image_id = images.id
WHERE
scene_images.scene_id IS NULL AND
performer_images.performer_id IS NULL AND
studio_images IS NULL AND
edit_images IS NULL LIMIT 1000
LEFT JOIN (
SELECT id, (data->>'image')::uuid AS image_id
FROM drafts
) drafts ON images.id = drafts.image_id
WHERE scene_images.scene_id IS NULL
AND performer_images.performer_id IS NULL
AND studio_images IS NULL
AND edit_images IS NULL
AND drafts.id IS NULL
LIMIT 1000
`
args := []interface{}{}

Expand All @@ -116,16 +121,20 @@ func (qb *imageQueryBuilder) IsUnused(imageID uuid.UUID) (bool, error) {
SELECT (jsonb_array_elements(data#>'{new_data,added_images}')->>0)::uuid AS image_id
FROM edits
WHERE status = 'PENDING'
) edit_images ON edit_images.image_id = images.id`

query.AddWhere("images.id = ?")
) edit_images ON edit_images.image_id = images.id
LEFT JOIN (
SELECT id, (data->>'image')::uuid AS image_id
FROM drafts
) drafts ON images.id = drafts.image_id
WHERE images.id = ?
AND scene_images.scene_id IS NULL
AND performer_images.performer_id IS NULL
AND studio_images.studio_id IS NULL
AND edit_images.image_id IS NULL
AND drafts.id IS NULL
`
query.AddArg(imageID)

query.AddWhere("scene_images.scene_id IS NULL")
query.AddWhere("performer_images.performer_id IS NULL")
query.AddWhere("studio_images.studio_id IS NULL")
query.AddWhere("edit_images.image_id IS NULL")

count, err := qb.dbi.Count(*query)
if err != nil {
return false, err
Expand Down

0 comments on commit 7ad379e

Please sign in to comment.