mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 17:01:35 -05:00
The `@version_class` decorator looks at every dirty (modified) model in the session to work out which new history models to create. However, if there are dirty items in the session, sqlalchemy might flush to the database, clearing the whole session. We ran into problems with the archive service function, which is versioned for api keys, templates and services. When constructing the TemplateHistory objects, `history_meta.py::create_history` would call getattr on `Template.folders`, which would make a database call to join across to the TemplateFolder objects - this would then flush the dirty Service object from the session before the ServiceHistory object was created. To get around this, we eager load the Template.folder object, joining on to it automatically when the Template is fetched. That way, it doesn't make a SELECT mid-way through the version decorator, and the history is preserved. Note: This relationship is only on Template, not TemplateHistory - so we're not doing this join every single time we send a message.
72 KiB
72 KiB