Skip to content

Commit 8ba841d

Browse files
committed
fix: custom migration should skip the first launch
1 parent 8beb858 commit 8ba841d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

repo/repo.go

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package repo
33
import (
44
"errors"
55
"log"
6+
"strings"
67

78
"github.com/0x2e/fusion/conf"
89
"github.com/0x2e/fusion/model"
@@ -32,6 +33,11 @@ func migrage() {
3233
// We must delete any duplicate feeds before AutoMigrate applies the
3334
// new unique constraint.
3435
err := DB.Transaction(func(tx *gorm.DB) error {
36+
// skip if it's the first launch
37+
if !tableExist(&model.Feed{}) || !tableExist(&model.Feed{}) {
38+
return nil
39+
}
40+
3541
// query duplicate feeds
3642
dupFeeds := make([]model.Feed, 0)
3743
err := tx.Model(&model.Feed{}).Where(
@@ -82,6 +88,17 @@ func migrage() {
8288
}
8389
}
8490

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+
85102
func registerCallback() {
86103
if err := DB.Callback().Query().After("*").Register("convert_error", func(db *gorm.DB) {
87104
if errors.Is(db.Error, gorm.ErrRecordNotFound) {

0 commit comments

Comments
 (0)