From 80c7a8215a0e402e92e1257f6d3c237c3ed1211c Mon Sep 17 00:00:00 2001 From: toshski <104477758+toshski@users.noreply.github.com> Date: Tue, 6 Aug 2024 02:10:43 +1200 Subject: [PATCH] Fix: extra unreferenced tags been created (#1807) * Fix Unreferenced Tags been created in error * Add checks to migration and retry if they fail --- pkg/migrations/migrations.go | 32 ++++++++++++++++++++++++++++++++ pkg/models/model_scene.go | 7 ++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pkg/migrations/migrations.go b/pkg/migrations/migrations.go index e63ff8da3..8b8419b11 100644 --- a/pkg/migrations/migrations.go +++ b/pkg/migrations/migrations.go @@ -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{ @@ -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() diff --git a/pkg/models/model_scene.go b/pkg/models/model_scene.go index 5e13ca425..fd3ec0cdc 100644 --- a/pkg/models/model_scene.go +++ b/pkg/models/model_scene.go @@ -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()