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
@@ -221,24 +221,35 @@ func (s Service) OnModuleConfigSchema(
|
||||
Version: 1,
|
||||
Active: xtypes.Bool(true),
|
||||
}
|
||||
err := s.configRepo.Insert(ctx, initialConfig)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "insert config in store")
|
||||
}
|
||||
|
||||
result, err := s.variableService.ExtractVariables(ctx, initialConfig.Id, initialConfig.Data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "extract and save variable links in store")
|
||||
return errors.WithMessage(err, "extract variables from initial config")
|
||||
}
|
||||
if len(result.AbsentVariables) > 0 {
|
||||
return errors.Errorf("missing variables in initial config: [%s]", strings.Join(result.AbsentVariables, ","))
|
||||
}
|
||||
|
||||
err = s.configRepo.Insert(ctx, initialConfig)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "insert config in store")
|
||||
}
|
||||
err = s.variableService.SaveVariableLinks(ctx, initialConfig.Id, result.VariableLinks)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "save variable links in store")
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user