feat: complete Kra admin backend migration
This commit is contained in:
+34
-29
@@ -13,15 +13,17 @@ import (
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
var ProviderSet = wire.NewSet(NewData, NewAdminRepo, NewSystemRepo, NewAccessRepo, NewSettingsRepo, NewVersionRepo, NewExportRepo, NewAuditRepo, NewTaskRepo, NewMediaRepo, NewAnnouncementRepo, NewEmailRepo, NewCache, NewFileStorage)
|
||||
var ProviderSet = wire.NewSet(NewData, NewSystemRepo, NewAccessRepo, NewSettingsRepo, NewVersionRepo, NewExportRepo, NewAuditRepo, NewTaskRepo, NewMediaRepo, NewAnnouncementRepo, NewEmailRepo, NewCache, NewFileStorage)
|
||||
|
||||
type Data struct {
|
||||
mu sync.RWMutex
|
||||
initMu sync.Mutex
|
||||
gormDB *gorm.DB
|
||||
redis *redis.Client
|
||||
database *conf.Data_Database
|
||||
config *conf.Data
|
||||
admin *conf.AdminBackend
|
||||
storage *reloadableStorage
|
||||
}
|
||||
|
||||
func NewData(c *conf.Data, admin *conf.AdminBackend) (*Data, func(), error) {
|
||||
@@ -29,7 +31,8 @@ func NewData(c *conf.Data, admin *conf.AdminBackend) (*Data, func(), error) {
|
||||
return nil, nil, fmt.Errorf("database configuration is required")
|
||||
}
|
||||
d := &Data{database: c.Database, config: c, admin: admin}
|
||||
db, err := openDatabase(c.Database, false)
|
||||
db, err := openDatabase(c.Database, false, "")
|
||||
usingFallback := false
|
||||
if err != nil {
|
||||
// The initialization endpoint must remain available when the configured
|
||||
// target database has not been created yet.
|
||||
@@ -38,28 +41,16 @@ func NewData(c *conf.Data, admin *conf.AdminBackend) (*Data, func(), error) {
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("open bootstrap database: %w", err)
|
||||
}
|
||||
usingFallback = true
|
||||
}
|
||||
d.gormDB = db
|
||||
if err = db.AutoMigrate(&announcementPO{}, &adminPO{}); err != nil {
|
||||
return nil, nil, fmt.Errorf("migrate bootstrap tables: %w", err)
|
||||
if !usingFallback {
|
||||
if err = migrateAll(db); err != nil {
|
||||
return nil, nil, fmt.Errorf("migrate tables: %w", err)
|
||||
}
|
||||
}
|
||||
if c.Redis != nil && c.Redis.Addr != "" {
|
||||
options := &redis.Options{Addr: c.Redis.Addr, Network: c.Redis.Network}
|
||||
if c.Redis.ReadTimeout != nil {
|
||||
options.ReadTimeout = c.Redis.ReadTimeout.AsDuration()
|
||||
}
|
||||
if c.Redis.WriteTimeout != nil {
|
||||
options.WriteTimeout = c.Redis.WriteTimeout.AsDuration()
|
||||
}
|
||||
candidate := redis.NewClient(options)
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), 800*time.Millisecond)
|
||||
if pingErr := candidate.Ping(pingCtx).Err(); pingErr != nil {
|
||||
log.Printf("redis unavailable, using in-memory cache: %v", pingErr)
|
||||
_ = candidate.Close()
|
||||
} else {
|
||||
d.redis = candidate
|
||||
}
|
||||
cancel()
|
||||
if candidate := openRedis(c.Redis); candidate != nil {
|
||||
d.redis = candidate
|
||||
}
|
||||
cleanup := func() {
|
||||
d.mu.RLock()
|
||||
@@ -75,24 +66,38 @@ func NewData(c *conf.Data, admin *conf.AdminBackend) (*Data, func(), error) {
|
||||
return d, cleanup, nil
|
||||
}
|
||||
|
||||
func (d *Data) switchDatabase(config *conf.Data_Database) error {
|
||||
db, err := openDatabase(config, true)
|
||||
if err != nil {
|
||||
return err
|
||||
func openRedis(config *conf.Data_Redis) *redis.Client {
|
||||
if config == nil || config.Addr == "" {
|
||||
return nil
|
||||
}
|
||||
options := &redis.Options{Addr: config.Addr, Network: config.Network}
|
||||
if config.ReadTimeout != nil {
|
||||
options.ReadTimeout = config.ReadTimeout.AsDuration()
|
||||
}
|
||||
if config.WriteTimeout != nil {
|
||||
options.WriteTimeout = config.WriteTimeout.AsDuration()
|
||||
}
|
||||
candidate := redis.NewClient(options)
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), 800*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := candidate.Ping(pingCtx).Err(); err != nil {
|
||||
log.Printf("redis unavailable, using in-memory cache: %v", err)
|
||||
_ = candidate.Close()
|
||||
return nil
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
|
||||
func (d *Data) activateDatabase(db *gorm.DB, config *conf.Data_Database) {
|
||||
d.mu.Lock()
|
||||
old := d.gormDB
|
||||
d.gormDB = db
|
||||
d.database = config
|
||||
d.config.Database = config
|
||||
d.mu.Unlock()
|
||||
if err := d.persistConfig(); err != nil {
|
||||
return fmt.Errorf("persist database configuration: %w", err)
|
||||
}
|
||||
if old != nil {
|
||||
if sqlDB, e := old.DB(); e == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user