Commits

Jason Moiron  committed 175a2b2

fix error in johnny where '.update' wasn't invalidating

  • Participants
  • Parent commits 1403e7c

Comments (0)

Files changed (2)

File johnny/cache.py

             result_type = args[0] if args else kwargs.get('result_type', MULTI)
 
             if type(cls) in (compiler.SQLInsertCompiler, compiler.SQLDeleteCompiler, compiler.SQLUpdateCompiler):
+                if type(cls) == compiler.SQLUpdateCompiler:
+                    # XXX: is there a way to check if we've actually performed an update here?
+                    ret = original(cls, *args, **kwargs)
+                    for table in cls.query.tables:
+                        self.keyhandler.invalidate_table(table, getattr(cls, 'using', 'default'))
+                    return ret
                 return original(cls, *args, **kwargs)
             try:
                 sql, params = cls.as_sql()
                 # blacklisted, in which case we just don't care.
                 if key is not None:
                     self.cache_backend.set(key, result)
+            elif cls.tables and sql.startswith('UPDATE'):
+                # issue #1 in bitbucket, not invalidating on update
+                for table in cls.tables:
+                    self.keyhandler.invalidate_table(table)
             return result
         return newfun
 

File johnny/tests/cache.py

         self.failUnless(new_count == 2)
         # this tests the codepath after 'except EmptyResultSet' where
         # result_type == MULTI
-        list(Publisher.objects.filter(title__in=[]))
+        self.failUnless(not list(Publisher.objects.filter(title__in=[])))
 
     def test_querycache_return_results(self):
         """Test that the return results from the query cache are what we
         Genre.objects.get(title='Fantasy')
         self.failUnless(not q.get_nowait())
 
+    def test_update(self):
+        from testapp.models import Genre
+        connection.queries = []
+        g1 = Genre.objects.get(pk=1)
+        Genre.objects.all().update(title="foo")
+        g2 = Genre.objects.get(pk=1)
+        self.failUnless(g1.title != g2.title)
+        self.failUnless(g2.title == "foo")
+        self.failUnless(len(connection.queries) == 3)
+
     def test_empty_count(self):
         """Test for an empty count aggregate query with an IN"""
         from testapp.models import Genre