Fix variable links not persisted when variable is used multiple times in config
- Dedupe extracted variable names (duplicate INSERTs aborted the config_has_variable transaction on PK conflict) - Check per-statement rqlite errors in ExecTransaction: a failed statement silently returned success before - Make link upserts idempotent (insert or ignore) and add link backfill on variable create/upsert from existing configs - Validate config variables before insert (no orphaned configs) - Resync active config links on module reconnect and on no-op config update (self-healing)
This commit is contained in:
1 parent
5c8a554d4f
commit
7f4e3ef028
7 files changed
+227
-21
No files matched your search
+10
-5
@@ -152,6 +152,11 @@ 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
|
||||
}
|
||||
@@ -295,15 +300,15 @@ func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.Cre
|
||||
Active: false,
|
||||
AdminId: adminId,
|
||||
}
|
||||
err := c.configRepo.Insert(ctx, config)
|
||||
if err != nil {
|
||||
return domain.Config{}, errors.WithMessage(err, "insert new config")
|
||||
}
|
||||
|
||||
// Check variables before insert so a config with missing variables is never persisted without links.
|
||||
extractResult, err := c.extractAndCheckVariables(ctx, config.Id, config.Data)
|
||||
if err != nil {
|
||||
return domain.Config{}, err
|
||||
}
|
||||
err = c.configRepo.Insert(ctx, config)
|
||||
if err != nil {
|
||||
return domain.Config{}, errors.WithMessage(err, "insert new config")
|
||||
}
|
||||
err = c.variableService.SaveVariableLinks(ctx, config.Id, extractResult.VariableLinks)
|
||||
if err != nil {
|
||||
return domain.Config{}, errors.WithMessage(err, "save variable links")
|
||||
|
||||
Reference in new issue
Block a user