Viktor/sqlfunctions

This is a massive rewrite of a lot of stuff, but it will let us lock down the database a lot better.

I've tested everything affected afaik, but I could have missed something. We need to write real integration tests.
If we're going to lookup a user based on `lower(email)`, we should have a unique constraint on `lower(email)` instead of `email`. This would prevent someone from signing up with the same email with a different case, while still preserving the case of the address they signed up with.