1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-12 21:39:30 +00:00

Hard-wire external associations: 6 and 7/7: unit tests for updating A=>B links when instances of A and B change (#702)

* Implement hard-wired external associations:

* The table is in sqlproxy.proxy_store
  - externalGVKDependencies - a map of GVKs to dependencies.
    When the key GVK is updated, it triggers the updates in the database for the dependent GVKs,
    replacing fields as specified in the table.

* This is done in an afterUpsert handler, but it's done after the transaction for the core
  GVK update is finished, because most likely the dependent GVK updates will depend on the
  final database values for the GVK being updated, and if we do it as part of the transaction
  the new values won't be committed to the database.

* Wrote unit tests for external associations.

* When an object is modified/created, check for external deps that need updating.

* Stop emitting errors when joining tables if one of the tables doesn't exist.

* Finish rebasing manually.

* Fix external association unit-tests after rebasing.
This commit is contained in:
Eric Promislow
2025-07-09 13:11:42 -07:00
committed by GitHub
parent 883d2f805a
commit 76477e98df
3 changed files with 167 additions and 17 deletions

View File

@@ -203,7 +203,7 @@ func (s *Store) updateExternalInfo(tx transaction.Client, key string, externalUp
}
}
for _, nonLabelDep := range externalUpdateInfo.ExternalDependencies {
rawGetStmt := fmt.Sprintf(`SELECT f.key, ex2."%s"
rawGetStmt := fmt.Sprintf(`SELECT DISTINCT f.key, ex2."%s"
FROM "%s_fields" f JOIN "%s_fields" ex2 ON f."%s" = ex2."%s"
WHERE f."%s" != ex2."%s"`,
nonLabelDep.TargetFinalFieldName,
@@ -216,7 +216,7 @@ func (s *Store) updateExternalInfo(tx transaction.Client, key string, externalUp
// TODO: Try to fold the two blocks together
getStmt := s.Prepare(rawGetStmt)
rows, err := s.QueryForRows(s.ctx, getStmt, nonLabelDep.SourceFieldName)
rows, err := s.QueryForRows(s.ctx, getStmt)
if err != nil {
if !isDBError(err) {
logrus.Infof("Error getting external info for table %s, key %s: %v", nonLabelDep.TargetGVK, key, &db.QueryError{QueryString: rawGetStmt, Err: err})
@@ -286,6 +286,8 @@ func (s *Store) overrideCheck(finalFieldName, sourceGVK, sourceKey, finalTargetV
return false, nil
}
/* Core methods */
// deleteByKey deletes the object associated with key, if it exists in this Store
func (s *Store) deleteByKey(key string, obj any) error {
return s.WithTransaction(s.ctx, true, func(tx transaction.Client) error {