This might be avoidable in the future, with some auto-reconnect logic at the database interface.
The solution is probably to catch OperationalError and DatabaseError around cursor.execute calls. We would have to reconnect using a backoff, and refresh all connection pool threads if the server is unresponsive. Make sure we're using timeouts to avoid hanging.
I took a look at it. All of the classes in database folder need rewrite. (Factor out the transaction bit, abstract query building probably by using pypika). This is a way bigger thing than I thought.