wangpengfei
2023-08-24 487b20629178159396a93460f7355f5ebee7a8a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package example
 
import (
    "context"
    "github.com/flipped-aurora/gin-vue-admin/server/model/example"
    "github.com/flipped-aurora/gin-vue-admin/server/service/system"
    "github.com/pkg/errors"
    "gorm.io/gorm"
)
 
const initOrderExaFile = system.InitOrderInternal + 1
 
type initExaFileMysql struct{}
 
// auto run
func init() {
    system.RegisterInit(initOrderExaFile, &initExaFileMysql{})
}
 
func (i *initExaFileMysql) MigrateTable(ctx context.Context) (context.Context, error) {
    db, ok := ctx.Value("db").(*gorm.DB)
    if !ok {
        return ctx, system.ErrMissingDBContext
    }
    return ctx, db.AutoMigrate(&example.ExaFileUploadAndDownload{})
}
 
func (i *initExaFileMysql) TableCreated(ctx context.Context) bool {
    db, ok := ctx.Value("db").(*gorm.DB)
    if !ok {
        return false
    }
    return db.Migrator().HasTable(&example.ExaFileUploadAndDownload{})
}
 
func (i initExaFileMysql) InitializerName() string {
    return example.ExaFileUploadAndDownload{}.TableName()
}
 
func (i *initExaFileMysql) InitializeData(ctx context.Context) (context.Context, error) {
    db, ok := ctx.Value("db").(*gorm.DB)
    if !ok {
        return ctx, system.ErrMissingDBContext
    }
    entities := []example.ExaFileUploadAndDownload{
        {Name: "10.png", Url: "https://qmplusimg.henrongyi.top/gvalogo.png", Tag: "png", Key: "158787308910.png"},
        {Name: "logo.png", Url: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"},
    }
    if err := db.Create(&entities).Error; err != nil {
        return ctx, errors.Wrap(err, example.ExaFileUploadAndDownload{}.TableName()+"表数据初始化失败!")
    }
    return ctx, nil
}
 
func (i *initExaFileMysql) DataInserted(ctx context.Context) bool {
    db, ok := ctx.Value("db").(*gorm.DB)
    if !ok {
        return false
    }
    lookup := example.ExaFileUploadAndDownload{Name: "logo.png", Key: "1587973709logo.png"}
    if errors.Is(db.First(&lookup, &lookup).Error, gorm.ErrRecordNotFound) {
        return false
    }
    return true
}