Skip to content

Commit

Permalink
fix(#27): utilize gorm built-in hasTable
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2E committed Sep 3, 2024
1 parent 2d27660 commit a7c4fda
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repo
import (
"errors"
"log"
"strings"

"github.com/0x2e/fusion/conf"
"github.com/0x2e/fusion/model"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a7c4fda

Please sign in to comment.