Keep only last N config versions
This commit is contained in:
1 parent
cb8c66849a
commit
68c8681a9b
8 files changed
+91
-33
No files matched your search
+4
-4
@@ -94,6 +94,9 @@ func (l Locator) Config() Config {
|
||||
)
|
||||
moduleController := controller.NewModule(moduleService, l.logger)
|
||||
|
||||
configHistoryApiService := apisvs.NewConfigHistory(configHistoryRepo, l.cfg.Local.KeepConfigVersions, l.logger)
|
||||
configHistoryController := api.NewConfigHistory(configHistoryApiService)
|
||||
|
||||
moduleApiService := apisvs.NewModule(moduleRepo, backendRepo, configSchemaRepo)
|
||||
moduleApiController := api.NewModule(moduleApiService)
|
||||
|
||||
@@ -102,13 +105,10 @@ func (l Locator) Config() Config {
|
||||
moduleRepo,
|
||||
configSchemaRepo,
|
||||
eventRepo,
|
||||
configHistoryRepo,
|
||||
configHistoryApiService,
|
||||
)
|
||||
configApiController := api.NewConfig(configApiService)
|
||||
|
||||
configHistoryApiService := apisvs.NewConfigHistory(configHistoryRepo)
|
||||
configHistoryController := api.NewConfigHistory(configHistoryApiService)
|
||||
|
||||
configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo)
|
||||
configSchemaController := api.NewConfigSchema(configSchemaApiService)
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
## v3.0.2
|
||||
* Новая реализация модуля на базе rqlite
|
||||
+2
-5
@@ -3,13 +3,13 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/pressly/goose/v3/database"
|
||||
"github.com/txix-open/isp-kit/config"
|
||||
"github.com/txix-open/isp-kit/dbx"
|
||||
"github.com/txix-open/isp-kit/http/httpcli"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"github.com/txix-open/isp-kit/validator"
|
||||
"isp-config-service/service/rqlite/goose_store"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -66,10 +66,7 @@ func main() {
|
||||
}
|
||||
defer dbClient.Close()
|
||||
|
||||
migrationStore, err := database.NewStore(migration.DialectSqlite3, repository.Table("goose_db_version"))
|
||||
if err != nil {
|
||||
panic(errors.WithMessage(err, "create store"))
|
||||
}
|
||||
migrationStore := goose_store.NewStore(dbClient.DB.DB)
|
||||
mr := migration.NewRunner("", migrations.Migrations, logger)
|
||||
err = mr.Run(ctx, dbClient.DB.DB, goose.WithStore(migrationStore))
|
||||
if err != nil {
|
||||
|
||||
+2
-1
@@ -15,11 +15,12 @@ logLevel: debug
|
||||
|
||||
maintenanceMode: false
|
||||
|
||||
keepConfigVersions: 64
|
||||
|
||||
credentials:
|
||||
- username: default
|
||||
password: 38kPk0upqI5j
|
||||
perms: ["all"]
|
||||
|
||||
internalClientCredential: default
|
||||
|
||||
rqlite:
|
||||
|
||||
@@ -9,5 +9,6 @@ type Local struct {
|
||||
ConfigServiceAddress bootstrap.ConfigServiceAddr
|
||||
MaintenanceMode bool
|
||||
Credentials []auth.Credential
|
||||
KeepConfigVersions int `validate:"required"`
|
||||
InternalClientCredential string `validate:"required"`
|
||||
}
|
||||
@@ -62,3 +62,22 @@ func (r ConfigHistory) Insert(ctx context.Context, history entity.ConfigHistory)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r ConfigHistory) DeleteOld(ctx context.Context, configId string, keepVersions int) (int, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "ConfigHistory.DeleteOld")
|
||||
|
||||
query := fmt.Sprintf(`delete from %s where config_id = ? and id not in (
|
||||
select id from %s where config_id = ? order by version desc limit ?
|
||||
)`, Table("config_history"), Table("config_history"))
|
||||
result, err := r.db.Exec(ctx, query, configId, configId, keepVersions)
|
||||
if err != nil {
|
||||
return 0, errors.WithMessagef(err, "exec: %s", query)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, errors.WithMessage(err, "get rows affected")
|
||||
}
|
||||
|
||||
return int(rowsAffected), nil
|
||||
}
|
||||
+17
-20
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user