Keep only last N config versions

This commit is contained in:
komissarovyv committed 2024-10-29 17:41:03 +03:00
1 parent cb8c66849a
commit 68c8681a9b
8 files changed
+91 -33

No files matched your search

+17 -20
View File
@@ -26,12 +26,16 @@ type EventRepo interface {
Insert(ctx context.Context, event entity.Event) error
}
type ConfigHistoryService interface {
OnUpdateConfig(ctx context.Context, oldConfig entity.Config) error
}
type Config struct {
configRepo ConfigRepo
moduleRepo ModuleRepo
schemaRepo SchemaRepo
eventRepo EventRepo
configHistoryRepo ConfigHistoryRepo
configRepo ConfigRepo
moduleRepo ModuleRepo
schemaRepo SchemaRepo
eventRepo EventRepo
configHistoryService ConfigHistoryService
}
func NewConfig(
@@ -39,14 +43,14 @@ func NewConfig(
moduleRepo ModuleRepo,
schemaRepo SchemaRepo,
eventRepo EventRepo,
configHistoryRepo ConfigHistoryRepo,
configHistoryService ConfigHistoryService,
) Config {
return Config{
configRepo: configRepo,
moduleRepo: moduleRepo,
schemaRepo: schemaRepo,
eventRepo: eventRepo,
configHistoryRepo: configHistoryRepo,
configRepo: configRepo,
moduleRepo: moduleRepo,
schemaRepo: schemaRepo,
eventRepo: eventRepo,
configHistoryService: configHistoryService,
}
}
@@ -137,16 +141,9 @@ func (c Config) CreateUpdateConfig(
return nil, entity.ErrConfigConflictUpdate
}
history := entity.ConfigHistory{
Id: uuid.NewString(),
ConfigId: req.Id,
Data: oldConfig.Data,
Version: oldConfig.Version,
AdminId: oldConfig.AdminId,
}
err = c.configHistoryRepo.Insert(ctx, history)
err = c.configHistoryService.OnUpdateConfig(ctx, *oldConfig)
if err != nil {
return nil, errors.WithMessage(err, "save config history")
return nil, errors.WithMessage(err, "change config history")
}
if oldConfig.Active {
+44 -3
View File
@@ -2,6 +2,9 @@ package api
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/txix-open/isp-kit/log"
"time"
"github.com/pkg/errors"
@@ -13,15 +16,24 @@ type ConfigHistoryRepo interface {
Delete(ctx context.Context, id string) error
GetByConfigId(ctx context.Context, configId string) ([]entity.ConfigHistory, error)
Insert(ctx context.Context, history entity.ConfigHistory) error
DeleteOld(ctx context.Context, configId string, keepVersions int) (int, error)
}
type ConfigHistory struct {
repo ConfigHistoryRepo
repo ConfigHistoryRepo
keepVersions int
logger log.Logger
}
func NewConfigHistory(repo ConfigHistoryRepo) ConfigHistory {
func NewConfigHistory(
repo ConfigHistoryRepo,
keepVersions int,
logger log.Logger,
) ConfigHistory {
return ConfigHistory{
repo: repo,
repo: repo,
keepVersions: keepVersions,
logger: logger,
}
}
@@ -52,3 +64,32 @@ func (s ConfigHistory) Delete(ctx context.Context, id string) error {
}
return nil
}
func (s ConfigHistory) OnUpdateConfig(ctx context.Context, oldConfig entity.Config) error {
history := entity.ConfigHistory{
Id: uuid.NewString(),
ConfigId: oldConfig.Id,
Data: oldConfig.Data,
Version: oldConfig.Version,
AdminId: oldConfig.AdminId,
}
err := s.repo.Insert(ctx, history)
if err != nil {
return errors.WithMessage(err, "save config history")
}
if s.keepVersions < 0 {
return nil
}
go func() {
deletedCount, err := s.repo.DeleteOld(context.Background(), oldConfig.Id, s.keepVersions)
if err != nil {
s.logger.Error(ctx, errors.WithMessage(err, "delete old config versions"))
} else {
s.logger.Debug(ctx, fmt.Sprintf("delete '%d' old config versions", deletedCount))
}
}()
return nil
}