From d07cc90345ee45919f122bcb1f89cdfc79532ea1 Mon Sep 17 00:00:00 2001 From: Grzegorz Niewisiewicz Date: Fri, 17 Jan 2014 14:54:46 +0100 Subject: [PATCH 1/2] Update the table cache in update_table (fixes 67) The problem occurs in the following scenario: * an instance of a Table, obtained through Database.get_table, is used to insert some data * a different instance of Table, obtained through Database.get_table call after the data has been inserted, uses a Table instance that is not up-to-date The result is that, e.g. the keys created with the first Table object aren't accessible via the second object. This fixes the problem by updating the table cache in Database.update_table. --- dataset/persistence/database.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dataset/persistence/database.py b/dataset/persistence/database.py index b1c07bf..c536e50 100644 --- a/dataset/persistence/database.py +++ b/dataset/persistence/database.py @@ -192,7 +192,8 @@ class Database(object): self.metadata = MetaData(schema=self.schema) self.metadata.bind = self.engine self.metadata.reflect(self.engine) - return SQLATable(table_name, self.metadata) + self._tables[table_name] = SQLATable(table_name, self.metadata) + return self._tables[table_name] def get_table(self, table_name, primary_id='id', primary_type='Integer'): """ From a9abc9908dc8cda554d4987f073e22d31b241f91 Mon Sep 17 00:00:00 2001 From: Grzegorz Niewisiewicz Date: Mon, 27 Jan 2014 08:58:45 +0100 Subject: [PATCH 2/2] Add a test case for table cache updates This test case verifies whether get_table returns the most up-to-date table object containing all columns that has been added to the table since the last cache update. --- test/test_persistence.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_persistence.py b/test/test_persistence.py index 696f746..ca52c03 100644 --- a/test/test_persistence.py +++ b/test/test_persistence.py @@ -121,6 +121,13 @@ class DatabaseTestCase(unittest.TestCase): r = self.db.query('SELECT COUNT(*) AS num FROM weather').next() assert r['num'] == len(TEST_DATA), r + def test_table_cache_updates(self): + tbl1 = self.db.get_table('people') + tbl1.insert(dict(first_name='John', last_name='Smith')) + tbl2 = self.db.get_table('people') + + assert list(tbl2.all()) == [(1, 'John', 'Smith')] + class TableTestCase(unittest.TestCase):