Skip to content

Commit

Permalink
Fix: extra unreferenced tags been created (#1807)
Browse files Browse the repository at this point in the history
* Fix Unreferenced Tags been created in error

* Add checks to migration and retry if they fail
  • Loading branch information
toshski authored Aug 5, 2024
1 parent 76b7311 commit 80c7a82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
32 changes: 32 additions & 0 deletions pkg/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (i *RequestSceneList) ToJSON() string {
}

func Migrate() {
var retryMigration []string
db, _ := models.GetDB()

m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
Expand Down Expand Up @@ -1963,11 +1964,42 @@ 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 {
// update tag counts
tasks.CountTags()

// check there are no Tags with a count of 0 that are in use, should not happen if CountTags is working properly,
// but don't want to risk a referential integrity issue
type tagsInUse struct {
Cnt int
}
var result tagsInUse
db.Raw("select count(*) as cnt from scene_tags st join scenes s on s.id=st.scene_id join tags t on t.id=st.tag_id where t.`count` = 0 and s.deleted_at is NULL").Scan(&result)
if result.Cnt > 0 {
// this should never happen, but not deleting unreferenced tags will not break the system, so don't fail the migration, flag it to retry
retryMigration = append(retryMigration, "0079-remove-unreferenced-tags")
return nil
}
return tx.Model(&models.Tag{}).Exec("delete from tags where `count` = 0").Error
},
},
})

if err := m.Migrate(); err != nil {
common.Log.Fatalf("Could not migrate: %v", err)
}
if len(retryMigration) > 0 {
for _, migration := range retryMigration {
common.Log.Warnf("*** MIGRATION WARNING ***: Could not migrate: '%v', this migration will retry the next time XBVR is started", migration)
err := db.Exec("DELETE FROM migrations WHERE id = ?", migration).Error
if err != nil {
common.Log.Fatalf("Failed to remove %v from the miigration table - will not be retried", err)
}
}
}
common.Log.Printf("Migration did run successfully")

db.Close()
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 80c7a82

Please sign in to comment.