Files
mayfly-go/server/internal/db/infrastructure/persistence/db_backup_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

62 lines
1.9 KiB
Go

package persistence
import (
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/base"
"mayfly-go/pkg/global"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
var _ repository.DbBackupHistory = (*dbBackupHistoryRepoImpl)(nil)
type dbBackupHistoryRepoImpl struct {
base.RepoImpl[*entity.DbBackupHistory]
}
func NewDbBackupHistoryRepo() repository.DbBackupHistory {
return &dbBackupHistoryRepoImpl{}
}
func (repo *dbBackupHistoryRepoImpl) GetHistories(condition *entity.DbBackupHistoryQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(new(entity.DbBackupHistory)).
Eq("id", condition.Id).
Eq0("db_instance_id", condition.DbInstanceId).
In0("db_name", condition.InDbNames).
Eq("db_backup_id", condition.DbBackupId).
Eq("db_name", condition.DbName)
return gormx.PageQuery(qd, pageParam, toEntity)
}
func (repo *dbBackupHistoryRepoImpl) GetLatestHistory(instanceId uint64, dbName string, bi *entity.BinlogInfo) (*entity.DbBackupHistory, error) {
history := &entity.DbBackupHistory{}
db := global.Db
err := db.Model(repo.GetModel()).
Where("db_instance_id = ?", instanceId).
Where("db_name = ?", dbName).
Where(db.Where("binlog_sequence < ?", bi.Sequence).
Or(db.Where("binlog_sequence = ?", bi.Sequence).
Where("binlog_position <= ?", bi.Position))).
Scopes(gormx.UndeleteScope).
Order("binlog_sequence desc, binlog_position desc").
First(history).Error
if err != nil {
return nil, err
}
return history, err
}
func (repo *dbBackupHistoryRepoImpl) GetEarliestHistory(instanceId uint64) (*entity.DbBackupHistory, error) {
history := &entity.DbBackupHistory{}
db := global.Db.Model(repo.GetModel())
err := db.Where("db_instance_id = ?", instanceId).
Scopes(gormx.UndeleteScope).
Order("binlog_sequence").
First(history).Error
if err != nil {
return nil, err
}
return history, nil
}