jdkandersson / OpenAlchemy

Define SQLAlchemy models using the OpenAPI specification.
https://openapi-sqlalchemy.readthedocs.io/en/latest/
Apache License 2.0
47 stars 13 forks source link

Faster Startup after Build #251

Closed jdkandersson closed 3 years ago

jdkandersson commented 3 years ago

As a user I want the startup to be faster after I have built a package so that my application is ready faster.

Every time OpenAlchemy starts up, the OpenAPI specification is validated against all rules. This is required if the OpenAPI specification can't be trusted. However, the validation is also run when the package is created during the build process. If a signature is included in the build the OpenAPI specification validation step can be skipped assuming the OpenAPI specification hasn't changed.

This can also be extended to be done if OpenAlchemy is run multiple times with the same spec by writing something to the file system.

jdkandersson commented 3 years ago

Create a cache. It takes a path to a specification file and exposes the following functions:

Create a file with the following name: __open_alchemy_<schemas filename where . is replaced with _>_cache__

The content of the file is:

{
    "hash": "<sha256 hash of the file contents>",
    "data": {
        "schemas": {
            "valid": true/false
        }
    }
}

schemas_valid

  1. If the file does not exist, return False.
  2. If the file is actually a folder, return False.
  3. Try to load the file, if it fails, return False.
  4. Try to retrieve the hash key, if it does not exist, return False.
  5. Calculate the hash of the file contents, if they are different to the hash in the cache, return False.
  6. Look for the data.schemas.valid key, if it does not exist, return False.
  7. If the value of data.schemas.valid is True return True, otherwise return False.

schemas_are_valid

  1. If there already is something present at the filename, delete it.
  2. If the file does not exist, create the file.
  3. Load the contents of the file. If it is not a dictionary, throw the contents away and create an empty dictionary.
  4. Calculate the hash of the file contents.
  5. Create or update the hash key in the dictionary to be the calculated value.
  6. Look for the data key. If it does not exist or is not a dictionary, make it an empty dictionary.
  7. Look for the schemas key under data in the dictionary. If it does not exist or is not a dictionary, set it to be an empty dictionary.
  8. Create or update the valid key under data.schemas and set it to True.
  9. Write the dictionary to the file as JSON.