Files
mayfly-go/server/internal/db/infrastructure/persistence/db_binlog_history.go
kanzihuang ae3d2659aa 重构数据库备份与恢复模块 (#80)
* fix: 保存 LastResult 时截断字符串过长部分,以避免数据库报错

* refactor: 新增 entity.DbTaskBase 和 persistence.dbTaskBase, 用于实现数据库备份和恢复任务处理相关部分

* fix: aeskey变更后,解密密码出现数组越界访问错误

* fix: 时间属性为零值时,保存到 mysql 数据库报错

* refactor db.infrastructure.service.scheduler

* feat: 实现立即备份功能

* refactor db.infrastructure.service.db_instance

* refactor: 从数据库中获取数据库备份目录、mysql文件路径等配置信息

* fix: 数据库备份和恢复问题

* fix: 修改 .gitignore 文件,忽略数据库备份目录和数据库程序目录
2024-01-05 08:55:34 +08:00

124 lines
3.3 KiB
Go

package persistence
import (
"context"
"errors"
"gorm.io/gorm"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"time"
)
var _ repository.DbBinlogHistory = (*dbBinlogHistoryRepoImpl)(nil)
type dbBinlogHistoryRepoImpl struct {
base.RepoImpl[*entity.DbBinlogHistory]
}
func NewDbBinlogHistoryRepo() repository.DbBinlogHistory {
return &dbBinlogHistoryRepoImpl{}
}
func (repo *dbBinlogHistoryRepoImpl) GetHistoryByTime(instanceId uint64, targetTime time.Time) (*entity.DbBinlogHistory, error) {
gdb := gormx.NewQuery(repo.GetModel()).
Eq("db_instance_id", instanceId).
Le("first_event_time", targetTime).
Undeleted().
OrderByDesc("first_event_time").
GenGdb()
history := &entity.DbBinlogHistory{}
err := gdb.First(history).Error
if err != nil {
return nil, err
}
return history, err
}
func (repo *dbBinlogHistoryRepoImpl) GetHistories(instanceId uint64, start, target *entity.BinlogInfo) ([]*entity.DbBinlogHistory, error) {
gdb := gormx.NewQuery(repo.GetModel()).
Eq("db_instance_id", instanceId).
Ge("sequence", start.Sequence).
Le("sequence", target.Sequence).
Undeleted().
OrderByAsc("sequence").
GenGdb()
var histories []*entity.DbBinlogHistory
err := gdb.Find(&histories).Error
if err != nil {
return nil, err
}
if len(histories) == 0 {
return nil, errors.New("未找到满足条件的 binlog 文件")
}
return histories, err
}
func (repo *dbBinlogHistoryRepoImpl) GetLatestHistory(instanceId uint64) (*entity.DbBinlogHistory, bool, error) {
history := &entity.DbBinlogHistory{}
err := gormx.NewQuery(repo.GetModel()).
Eq("db_instance_id", instanceId).
Undeleted().
OrderByDesc("sequence").
GenGdb().
First(history).Error
switch {
case err == nil:
return history, true, nil
case errors.Is(err, gorm.ErrRecordNotFound):
return history, false, nil
default:
return nil, false, err
}
}
func (repo *dbBinlogHistoryRepoImpl) Upsert(_ context.Context, history *entity.DbBinlogHistory) error {
return gormx.Tx(func(db *gorm.DB) error {
old := &entity.DbBinlogHistory{}
err := db.Where("db_instance_id = ?", history.DbInstanceId).
Where("sequence = ?", history.Sequence).
First(old).Error
switch {
case err == nil:
return db.Model(old).Select("create_time", "file_size", "first_event_time").Updates(history).Error
case errors.Is(err, gorm.ErrRecordNotFound):
return db.Create(history).Error
default:
return err
}
})
}
func (repo *dbBinlogHistoryRepoImpl) InsertWithBinlogFiles(ctx context.Context, instanceId uint64, binlogFiles []*entity.BinlogFile) error {
if len(binlogFiles) == 0 {
return nil
}
histories := make([]*entity.DbBinlogHistory, 0, len(binlogFiles))
for _, fileOnServer := range binlogFiles {
if !fileOnServer.Downloaded {
break
}
history := &entity.DbBinlogHistory{
CreateTime: time.Now(),
FileName: fileOnServer.Name,
FileSize: fileOnServer.Size,
Sequence: fileOnServer.Sequence,
FirstEventTime: fileOnServer.FirstEventTime,
DbInstanceId: instanceId,
}
histories = append(histories, history)
}
if len(histories) > 1 {
if err := repo.BatchInsert(ctx, histories[:len(histories)-1]); err != nil {
return err
}
}
if len(histories) > 0 {
if err := repo.Upsert(ctx, histories[len(histories)-1]); err != nil {
return err
}
}
return nil
}