Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80c7a82

Browse files
authored
Fix: extra unreferenced tags been created (#1807)
* Fix Unreferenced Tags been created in error* Add checks to migration and retry if they fail
1 parent76b7311 commit80c7a82

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

‎pkg/migrations/migrations.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (i *RequestSceneList) ToJSON() string {
5353
}
5454

5555
funcMigrate() {
56+
varretryMigration []string
5657
db,_:=models.GetDB()
5758

5859
m:=gormigrate.New(db,gormigrate.DefaultOptions, []*gormigrate.Migration{
@@ -1963,11 +1964,42 @@ func Migrate() {
19631964
returndb.Where("scene_id = ?","virtualtaboo-").Delete(&models.Scene{}).Error
19641965
},
19651966
},
1967+
{
1968+
// remove unreferenced tags created due to an error
1969+
ID:"0079-remove-unreferenced-tags",
1970+
Migrate:func(tx*gorm.DB)error {
1971+
// update tag counts
1972+
tasks.CountTags()
1973+
1974+
// check there are no Tags with a count of 0 that are in use, should not happen if CountTags is working properly,
1975+
// but don't want to risk a referential integrity issue
1976+
typetagsInUsestruct {
1977+
Cntint
1978+
}
1979+
varresulttagsInUse
1980+
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)
1981+
ifresult.Cnt>0 {
1982+
// this should never happen, but not deleting unreferenced tags will not break the system, so don't fail the migration, flag it to retry
1983+
retryMigration=append(retryMigration,"0079-remove-unreferenced-tags")
1984+
returnnil
1985+
}
1986+
returntx.Model(&models.Tag{}).Exec("delete from tags where `count` = 0").Error
1987+
},
1988+
},
19661989
})
19671990

19681991
iferr:=m.Migrate();err!=nil {
19691992
common.Log.Fatalf("Could not migrate: %v",err)
19701993
}
1994+
iflen(retryMigration)>0 {
1995+
for_,migration:=rangeretryMigration {
1996+
common.Log.Warnf("*** MIGRATION WARNING ***: Could not migrate: '%v', this migration will retry the next time XBVR is started",migration)
1997+
err:=db.Exec("DELETE FROM migrations WHERE id = ?",migration).Error
1998+
iferr!=nil {
1999+
common.Log.Fatalf("Failed to remove %v from the miigration table - will not be retried",err)
2000+
}
2001+
}
2002+
}
19712003
common.Log.Printf("Migration did run successfully")
19722004

19732005
db.Close()

‎pkg/models/model_scene.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,17 @@ func SceneCreateUpdateFromExternal(db *gorm.DB, ext ScrapedScene) error {
433433
varsiteSite
434434
db.Where("id = ?",o.ScraperId).FirstOrInit(&site)
435435
o.IsSubscribed=site.Subscribed
436-
SaveWithRetry(db,&o)
437436

438437
// Clean & Associate Tags
439438
vartags=o.Tags
440439
db.Model(&o).Association("Tags").Clear()
441-
for_,tag:=rangetags {
440+
foridx,tag:=rangetags {
442441
tmpTag:=Tag{}
443442
db.Where(&Tag{Name:tag.Name}).FirstOrCreate(&tmpTag)
444-
db.Model(&o).Association("Tags").Append(tmpTag)
443+
tags[idx]=tmpTag
445444
}
445+
o.Tags=tags
446+
SaveWithRetry(db,&o)
446447

447448
// Clean & Associate Actors
448449
db.Model(&o).Association("Cast").Clear()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp