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
+27
-8
@@ -157,14 +157,10 @@ func (r Variable) UpsertLinks(ctx context.Context, configId string, links []enti
|
||||
}
|
||||
|
||||
for _, variable := range links {
|
||||
query, args, err := squirrel.Insert(Table("config_has_variable")).
|
||||
Columns("config_id", "variable_name").
|
||||
Values(variable.ConfigId, variable.VariableName).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "build query")
|
||||
}
|
||||
requests = append(requests, db.Request(query, args...))
|
||||
requests = append(requests, db.Request(
|
||||
`insert or ignore into `+Table("config_has_variable")+` (config_id, variable_name) values (?, ?)`,
|
||||
variable.ConfigId, variable.VariableName,
|
||||
))
|
||||
}
|
||||
|
||||
_, err = r.db.ExecTransaction(ctx, requests)
|
||||
@@ -175,6 +171,29 @@ 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")
|
||||
|
||||
|
||||
Reference in new issue
Block a user