mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 23:40:24 +08:00
* 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 文件,忽略数据库备份目录和数据库程序目录
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"mayfly-go/internal/db/domain/entity"
|
|
"mayfly-go/internal/db/domain/repository"
|
|
"mayfly-go/pkg/gormx"
|
|
"mayfly-go/pkg/model"
|
|
"slices"
|
|
)
|
|
|
|
var _ repository.DbBackup = (*dbBackupRepoImpl)(nil)
|
|
|
|
type dbBackupRepoImpl struct {
|
|
//base.RepoImpl[*entity.DbBackup]
|
|
dbTaskBase[*entity.DbBackup]
|
|
}
|
|
|
|
func NewDbBackupRepo() repository.DbBackup {
|
|
return &dbBackupRepoImpl{}
|
|
}
|
|
|
|
// GetDbBackupList 分页获取数据库备份任务列表
|
|
func (d *dbBackupRepoImpl) GetDbBackupList(condition *entity.DbBackupQuery, pageParam *model.PageParam, toEntity any, _ ...string) (*model.PageResult[any], error) {
|
|
qd := gormx.NewQuery(d.GetModel()).
|
|
Eq("id", condition.Id).
|
|
Eq0("db_instance_id", condition.DbInstanceId).
|
|
Eq0("repeated", condition.Repeated).
|
|
In0("db_name", condition.InDbNames).
|
|
Like("db_name", condition.DbName)
|
|
return gormx.PageQuery(qd, pageParam, toEntity)
|
|
}
|
|
|
|
func (d *dbBackupRepoImpl) AddTask(ctx context.Context, tasks ...*entity.DbBackup) error {
|
|
return gormx.Tx(func(db *gorm.DB) error {
|
|
var instanceId uint64
|
|
dbNames := make([]string, 0, len(tasks))
|
|
for _, task := range tasks {
|
|
if instanceId == 0 {
|
|
instanceId = task.DbInstanceId
|
|
}
|
|
if task.DbInstanceId != instanceId {
|
|
return errors.New("不支持同时为多个数据库实例添加备份任务")
|
|
}
|
|
if task.Interval == 0 {
|
|
// 单次执行的备份任务可重复创建
|
|
continue
|
|
}
|
|
dbNames = append(dbNames, task.DbName)
|
|
}
|
|
var res []string
|
|
err := db.Model(d.GetModel()).Select("db_name").
|
|
Where("db_instance_id = ?", instanceId).
|
|
Where("db_name in ?", dbNames).
|
|
Where("repeated = true").
|
|
Scopes(gormx.UndeleteScope).Find(&res).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(res) > 0 {
|
|
return errors.New(fmt.Sprintf("数据库备份任务已存在: %v", res))
|
|
}
|
|
|
|
return d.BatchInsertWithDb(ctx, db, tasks)
|
|
})
|
|
}
|
|
|
|
func (d *dbBackupRepoImpl) GetDbNamesWithoutBackup(instanceId uint64, dbNames []string) ([]string, error) {
|
|
var dbNamesWithBackup []string
|
|
query := gormx.NewQuery(d.GetModel()).
|
|
Eq("db_instance_id", instanceId).
|
|
Eq("repeated", true)
|
|
if err := query.GenGdb().Pluck("db_name", &dbNamesWithBackup).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]string, 0, len(dbNames))
|
|
for _, name := range dbNames {
|
|
if !slices.Contains(dbNamesWithBackup, name) {
|
|
result = append(result, name)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|