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()