api.models: add an api_version field to every Node #396
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.
New field: api_version
This specifies the API version the node conforms to. Saving this information in each node makes it possible to make future changes in the models and manage the transitions in a sane way:
When transitioning to a new API version that includes changes in the models, we can check which nodes are compatible with the new changes and which ones aren't, and we can deal with the incompatible nodes properly, for instance, by handling them using old or adapted utility functions in case the old version remains in a "deprecated" status before being completely removed, or by translating these nodes to comply with the new version.
The format is not relevant for now. I set 0 as a pre-release version and the first version is supposed to start at 1. We have to think of a good numbering scheme for this. For instance, we could keep 0 as a "test" version to be used during development of API changes, or use odd numbers for released versions and even numbers for the "test" versions between them (ie. 0 as the test version for 1, 2 as the test version for 3, etc.)
More about API and model changes
Once an API version is released it must be frozen and all clients must adhere to it. No matter how thorough we try to be when defining the models, we'll probably find that we need to change them to accommodate new requirements. A good approach to this could be the following:
extra
field in the base Node model that will be a dict with arbitrary data (not validated). This will allow to add new fields there and work with them temporarily, testing them until we're sure that's what we need.extra
field and have them defined as regular fields that will be validated by pydantic. Then we can freeze the new version and deploy it.An example using node definitions:
Assume a node with this format in stable release 1:
At some point we detect that we won't need "attr3" anymore but that we want to add two new fields: "attr4" and "attr5", so we test them on a new test version (2):
During this transition stage, the models are still unchanged and the applications are all compatible with the stable release 1, but the clients that need to work on this new data structure will include changes to process it and handle the transition gracefully. Note that the nodes defined in test version 2 are compatible with the model definitions of stable version 1.
Finally, when we're happy with all the experiments and tests and are ready to publish a new API release, we define the new model like this:
All the test nodes can be easily found and removed by searching for the specific API test version ({"api_version": 2}).