1

I am doing agressive caching and I'd like to make sure that nobody accidentally writes code that updates the database directly. I would imagine that the way to solve this would be to rename the normal object manager .rw_objects for the caches use, and replace .objects with a manager that logs a warning on a non-updating access and throws an exception if someone tries to do an update from it.

I've written an object manager and a query set but I'm not sure how to go about checking whether a query is updating the database.

Any suggestions?

guidoism
  • 7,820
  • 8
  • 41
  • 59
  • Who is this mysterious "somebody" that can "accidentally" bypass the ORM and caching to "update a database directly"? Could you consider calling them up and suggesting that they stop doing this? A phone call may be much cheaper and simpler than a lot of coding. Why not just tell them to stop doing this? – S.Lott Dec 20 '10 at 20:34
  • Me! (and my colleagues) Caching is one of those places where problems that come up can be so subtle and go unnoticed for so long that I'd rather have the code defend against my retardedness than expecting me to remember to not be stupid. Oh, and "by directly" I mean using the ORM instead of my cache. :-) – guidoism Dec 21 '10 at 19:42

1 Answers1

1

I'm thinking about two approaches here.

  1. Create a custom Manager, override _insert(), _update() to raise exception / log the query, and get_query_set() to return custom QuerySet that overrides create(), get_or_create(), and update().

  2. If you're using django 1.2, Create another database connection in settings.py call it "READ_ONLY", and create a custom manager that returns QuerSet using that connection (like def get_query_set() return super(ReadOnlyManager, self).get_query_set().using("READ_ONLY"), and mark the connection read-only. (One way to do it is create read-only user for the database connection "READ_ONLY".... If you're using Postgres, you can do stuff like How do you create a read-only user in PostgreSQL?)

Community
  • 1
  • 1
Jeeyoung Kim
  • 5,827
  • 8
  • 44
  • 54