Drop link self-healing paths, fix linter findings
- Remove link resync on module reconnect, link resave on no-op config update, and link backfill on variable create/upsert: links are formed only on config save - Disable exhaustruct_v5 and goconst linters - Fix golangci-lint findings: noinlineerr, slices.Backward, wrapcheck for ConfigValidationError (errors.As in controller keeps working via %w), funlen nolint on acceptance test
This commit is contained in:
1 parent
7f4e3ef028
commit
c464393e93
9 files changed
+9
-114
No files matched your search
@@ -8,6 +8,8 @@ linters:
|
||||
- depguard # не всегда имеет смысл
|
||||
- dupword # не имеет смысла
|
||||
- exhaustruct # не имеет смысла
|
||||
- exhaustruct_v5 # не имеет смысла
|
||||
- goconst # не всегда имеет смысл
|
||||
- godot # не актуально для swagger
|
||||
- godox # не позволяет оставлять todo
|
||||
- musttag # у нас собственное правило: не используем теги, если работаем с пакетом isp-kit/json и с внутренними структурами(между нашими сервисами), при этом теги обязательный для всех внешних для нас структур
|
||||
|
||||
@@ -2,6 +2,7 @@ package middlewares
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
|
||||
"github.com/txix-open/etp/v4"
|
||||
|
||||
@@ -27,7 +28,7 @@ func EtpLogger(logger log.Logger) EtpMiddleware {
|
||||
|
||||
//nolint:ireturn
|
||||
func EtpChain(root etp.Handler, middlewares ...EtpMiddleware) etp.Handler {
|
||||
for i := len(middlewares) - 1; i >= 0; i-- {
|
||||
for i := range slices.Backward(middlewares) {
|
||||
root = middlewares[i](root)
|
||||
}
|
||||
return root
|
||||
|
||||
@@ -3,7 +3,6 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
@@ -204,37 +203,6 @@ func (r Config) GetMetaByVariables(ctx context.Context, variables []string) (map
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r Config) FindIdsByVariableReference(ctx context.Context, variableName string) ([]string, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Config.FindIdsByVariableReference")
|
||||
|
||||
escaped := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(variableName)
|
||||
pattern := `%\_Var\(` + escaped + `)%`
|
||||
|
||||
// nolint:unqueryvet
|
||||
query, args, err := squirrel.Select("id").
|
||||
From(Table("config")).
|
||||
Where(squirrel.Expr(`data like ? escape '\'`, pattern)).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "build query")
|
||||
}
|
||||
|
||||
type configIdRow struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
rows := make([]configIdRow, 0)
|
||||
err = r.db.Select(ctx, &rows, query, args...)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "select: %s", query)
|
||||
}
|
||||
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
ids = append(ids, row.Id)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (r Config) UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) (bool, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Config.UpdateConfigName")
|
||||
|
||||
|
||||
@@ -171,29 +171,6 @@ func (r Variable) UpsertLinks(ctx context.Context, configId string, links []enti
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Variable) InsertLinks(ctx context.Context, links []entity.ConfigHasVariable) error {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Variable.InsertLinks")
|
||||
|
||||
if len(links) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
requests := make([][]any, 0, len(links))
|
||||
for _, link := range links {
|
||||
requests = append(requests, db.Request(
|
||||
`insert or ignore into `+Table("config_has_variable")+` (config_id, variable_name) values (?, ?)`,
|
||||
link.ConfigId, link.VariableName,
|
||||
))
|
||||
}
|
||||
|
||||
_, err := r.db.ExecTransaction(ctx, requests)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "exec transaction: %v", requests)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r Variable) Delete(ctx context.Context, name string) (bool, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Variable.Delete")
|
||||
|
||||
|
||||
@@ -152,11 +152,6 @@ func (c Config) CreateUpdateConfig(
|
||||
return nil, errors.WithMessage(err, "compare data configs")
|
||||
}
|
||||
if isMatch {
|
||||
// Links may be missing after a partially failed update; re-save them even when data is unchanged.
|
||||
err = c.variableService.SaveVariableLinks(ctx, req.Id, variableExtractionResult.VariableLinks)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "save variable links")
|
||||
}
|
||||
c.logger.Info(ctx, "no changes in config; skip update")
|
||||
return &domain.CreateUpdateConfigResponse{Config: configToDto(*oldConfig, nil), IsChanged: false}, nil
|
||||
}
|
||||
@@ -328,7 +323,7 @@ func (c Config) extractAndCheckVariables(ctx context.Context, configId string, i
|
||||
for _, variable := range variableExtractionResult.AbsentVariables {
|
||||
details[variable] = "variable is not defined"
|
||||
}
|
||||
return nil, domain.NewConfigValidationError(details)
|
||||
return nil, errors.WithMessage(domain.NewConfigValidationError(details), "config validation")
|
||||
}
|
||||
return variableExtractionResult, nil
|
||||
}
|
||||
@@ -350,7 +345,7 @@ func (c Config) validateConfigUpdate(ctx context.Context, moduleId string, confi
|
||||
return nil
|
||||
}
|
||||
|
||||
return domain.NewConfigValidationError(details)
|
||||
return errors.WithMessage(domain.NewConfigValidationError(details), "config validation")
|
||||
}
|
||||
|
||||
func (c Config) emitChangeActiveConfigEvent(ctx context.Context, moduleId string) error {
|
||||
|
||||
@@ -240,16 +240,6 @@ func (s Service) OnModuleConfigSchema(
|
||||
}
|
||||
|
||||
config = &initialConfig
|
||||
} else {
|
||||
// Re-sync links on every connect: they are derived data and may be missing after a partially failed update.
|
||||
result, err := s.variableService.ExtractVariables(ctx, config.Id, config.Data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "extract variables from active config")
|
||||
}
|
||||
err = s.variableService.SaveVariableLinks(ctx, config.Id, result.VariableLinks)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "resync variable links in store")
|
||||
}
|
||||
}
|
||||
|
||||
configData, err := s.variableService.RenderConfig(ctx, config.Data)
|
||||
|
||||
@@ -150,7 +150,8 @@ func (d Adapter) exec(ctx context.Context, inTx bool, requests [][]any) ([]sql.R
|
||||
for _, result := range resp.Results {
|
||||
// rqlite reports per-statement failures in individual results with HTTP 200,
|
||||
// and aborts the whole transaction on the first failed statement.
|
||||
if err := catchError(nil, result); err != nil {
|
||||
err := catchError(nil, result)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "exec transaction on statement: %v", requests[min(len(results), len(requests)-1)])
|
||||
}
|
||||
results = append(results, result)
|
||||
|
||||
@@ -20,12 +20,10 @@ type Repo interface {
|
||||
Upsert(ctx context.Context, variables []entity.Variable) error
|
||||
Delete(ctx context.Context, name string) (bool, error)
|
||||
UpsertLinks(ctx context.Context, configId string, links []entity.ConfigHasVariable) error
|
||||
InsertLinks(ctx context.Context, links []entity.ConfigHasVariable) error
|
||||
}
|
||||
|
||||
type ConfigRepo interface {
|
||||
GetMetaByVariables(ctx context.Context, variables []string) (map[string][]entity.ConfigMetaWithVariable, error)
|
||||
FindIdsByVariableReference(ctx context.Context, variableName string) ([]string, error)
|
||||
}
|
||||
|
||||
type EventRepo interface {
|
||||
@@ -108,16 +106,6 @@ func (s Service) Create(ctx context.Context, req domain.CreateVariableRequest) e
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "insert variable")
|
||||
}
|
||||
|
||||
err = s.syncVariableLinks(ctx, []string{variable.Name})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "sync variable links")
|
||||
}
|
||||
|
||||
err = s.syncConfigsByVariables(ctx, []string{variable.Name})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "sync configs by variables")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -163,11 +151,6 @@ func (s Service) Upsert(ctx context.Context, req []domain.UpsertVariableRequest)
|
||||
return errors.WithMessage(err, "upsert variables")
|
||||
}
|
||||
|
||||
err = s.syncVariableLinks(ctx, variableNames)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "sync variable links")
|
||||
}
|
||||
|
||||
err = s.syncConfigsByVariables(ctx, variableNames)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "sync configs by variables")
|
||||
@@ -310,28 +293,6 @@ func (s Service) toDto(variable entity.Variable, metaByVariables map[string][]en
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) syncVariableLinks(ctx context.Context, variableNames []string) error {
|
||||
for _, name := range variableNames {
|
||||
configIds, err := s.configRepo.FindIdsByVariableReference(ctx, name)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "find configs referencing variable: %s", name)
|
||||
}
|
||||
links := make([]entity.ConfigHasVariable, 0, len(configIds))
|
||||
for _, configId := range configIds {
|
||||
links = append(links, entity.ConfigHasVariable{
|
||||
ConfigId: configId,
|
||||
VariableName: name,
|
||||
})
|
||||
}
|
||||
err = s.repo.InsertLinks(ctx, links)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "insert links for variable: %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) syncConfigsByVariables(ctx context.Context, variables []string) error {
|
||||
metaByVariables, err := s.configRepo.GetMetaByVariables(ctx, variables)
|
||||
if err != nil {
|
||||
|
||||
@@ -124,7 +124,7 @@ func TestVariableAcceptance(t *testing.T) {
|
||||
require.Empty(allResponse[1].Value) // secret
|
||||
}
|
||||
|
||||
//nolint:paralleltest
|
||||
//nolint:funlen,paralleltest
|
||||
func TestVariableLinksAfterConfigUpdate(t *testing.T) {
|
||||
require := require.New(t)
|
||||
logger := setupTest(t)
|
||||
|
||||
Reference in new issue
Block a user