Skip to content

Commit

Permalink
Fix Unreferenced Tags been created in error
Browse files Browse the repository at this point in the history
  • Loading branch information
toshski committed Jul 26, 2024
1 parent 2f4d462 commit 3f0b979
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pkg/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,14 @@ func Migrate() {
return db.Where("scene_id = ?", "virtualtaboo-").Delete(&models.Scene{}).Error
},
},
{
// remove unreferenced tags created due to an error
ID: "0079-remove-unreferenced-tags",
Migrate: func(tx *gorm.DB) error {
tasks.CountTags()
return tx.Model(&models.Tag{}).Exec("delete from tags where `count` = 0").Error
},
},
})

if err := m.Migrate(); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions pkg/models/model_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,16 +433,17 @@ func SceneCreateUpdateFromExternal(db *gorm.DB, ext ScrapedScene) error {
var site Site
db.Where("id = ?", o.ScraperId).FirstOrInit(&site)
o.IsSubscribed = site.Subscribed
SaveWithRetry(db, &o)

// Clean & Associate Tags
var tags = o.Tags
db.Model(&o).Association("Tags").Clear()
for _, tag := range tags {
for idx, tag := range tags {
tmpTag := Tag{}
db.Where(&Tag{Name: tag.Name}).FirstOrCreate(&tmpTag)
db.Model(&o).Association("Tags").Append(tmpTag)
tags[idx] = tmpTag
}
o.Tags = tags
SaveWithRetry(db, &o)

// Clean & Associate Actors
db.Model(&o).Association("Cast").Clear()
Expand Down

0 comments on commit 3f0b979

Please sign in to comment.