zhangqian
2024-03-22 9c03486e5a7f0b0298436c4b6227f21cd3c10649
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
package service
 
import (
    "context"
)
 
type MysqlInitHandler struct{}
 
func NewMysqlInitHandler() *MysqlInitHandler {
    return &MysqlInitHandler{}
}
 
func (h MysqlInitHandler) InitData(ctx context.Context, inits initSlice) error {
    next, cancel := context.WithCancel(ctx)
    defer func(c func()) { c() }(cancel)
    for _, init := range inits {
        if init.DataInserted(next) {
            continue
        }
        if n, err := init.InitializeData(next); err != nil {
            return err
        } else {
            next = n
        }
    }
    return nil
}