Sort hashes of signature fields and save branch hash back to DB if the DB hash is incorrect #4769
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While troubleshooting #4682 I realized that there was something wrong with how branches are created and in my database any branch that I created would have a different schema hash compared to "main". This leads to a situation where we always download a new branch from the database.
The call to update the hash of a branch here:
https://github.com/opsmill/infrahub/blob/infrahub-v0.16.4/backend/infrahub/graphql/mutations/branch.py#L89
Will (depending on schema) produce a different hash than the main branch.
There might be others but I tracked this problem down to
_get_signature_field()
where it looks like we've thought about this before where we order the values. It might be that we now have list of lists and only sort by the outer list.I.e in this example we'd sort the uniqueness_constraints list but not the content so we could get different load orders:
vs.
I haven't dug deep into this so the last part is just me guessing.
Another problem that can occur is when we restart Infrahub, and pull the branch information from the database then we have a function that checks if the hash coming from the database is valid or not, if the DB hash is invalid we log a warning and just proceed instead of saving back the hash to the database. This often leads to us being in a situation where the schema hash differs between branches which is especially problematic when we create a new branch and the logic to populate the registry thinks that it needs to load all of the schema from the database instead of using the local cache.