From a7c4fda8062af39795d8cf207ea436dec5d52a1a Mon Sep 17 00:00:00 2001 From: rook1e Date: Tue, 3 Sep 2024 11:14:46 +0800 Subject: [PATCH] fix(#27): utilize gorm built-in hasTable --- repo/repo.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/repo/repo.go b/repo/repo.go index ed4d5a5..c3498ed 100644 --- a/repo/repo.go +++ b/repo/repo.go @@ -3,7 +3,6 @@ package repo import ( "errors" "log" - "strings" "github.com/0x2e/fusion/conf" "github.com/0x2e/fusion/model" @@ -33,8 +32,8 @@ func migrage() { // We must delete any duplicate feeds before AutoMigrate applies the // new unique constraint. err := DB.Transaction(func(tx *gorm.DB) error { - // skip if it's the first launch - if !tableExist(&model.Feed{}) || !tableExist(&model.Feed{}) { + // skip when it's the first launch + if !tx.Migrator().HasTable(&model.Feed{}) || !tx.Migrator().HasTable(&model.Item{}) { return nil } @@ -88,17 +87,6 @@ func migrage() { } } -func tableExist(table interface{}) bool { - err := DB.Model(table).First(table, "id = 1").Error - if err != nil { - if strings.Contains(err.Error(), "no such table") { - return false - } - panic(err) - } - return true -} - func registerCallback() { if err := DB.Callback().Query().After("*").Register("convert_error", func(db *gorm.DB) { if errors.Is(db.Error, gorm.ErrRecordNotFound) {