Skip to content

Commit a7c4fda

Browse files
committed
fix(#27): utilize gorm built-in hasTable
1 parent 2d27660 commit a7c4fda

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

repo/repo.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package repo
33
import (
44
"errors"
55
"log"
6-
"strings"
76

87
"github.com/0x2e/fusion/conf"
98
"github.com/0x2e/fusion/model"
@@ -33,8 +32,8 @@ func migrage() {
3332
// We must delete any duplicate feeds before AutoMigrate applies the
3433
// new unique constraint.
3534
err := DB.Transaction(func(tx *gorm.DB) error {
36-
// skip if it's the first launch
37-
if !tableExist(&model.Feed{}) || !tableExist(&model.Feed{}) {
35+
// skip when it's the first launch
36+
if !tx.Migrator().HasTable(&model.Feed{}) || !tx.Migrator().HasTable(&model.Item{}) {
3837
return nil
3938
}
4039

@@ -88,17 +87,6 @@ func migrage() {
8887
}
8988
}
9089

91-
func tableExist(table interface{}) bool {
92-
err := DB.Model(table).First(table, "id = 1").Error
93-
if err != nil {
94-
if strings.Contains(err.Error(), "no such table") {
95-
return false
96-
}
97-
panic(err)
98-
}
99-
return true
100-
}
101-
10290
func registerCallback() {
10391
if err := DB.Callback().Query().After("*").Register("convert_error", func(db *gorm.DB) {
10492
if errors.Is(db.Error, gorm.ErrRecordNotFound) {

0 commit comments

Comments
 (0)