From: Mike Bayer Date: Mon, 24 Feb 2020 15:44:14 +0000 (-0500) Subject: Ensure schema-level table includes annotations in caching X-Git-Tag: rel_1_4_0b1~506^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=996654df341a30b539434bb4fd1e0d53f46641a0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Ensure schema-level table includes annotations in caching In 29330ec159 we ensured that annotations are part of cache keys. However we failed to do so for the schema-level Table which will definitely need to distinguish between ORM and non-ORM annotated tables when caching, so ensure this is part of the cache key. Change-Id: I8d996873f2d7fa63230ef837db7e69a0101973b2 --- diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 79a700ad8d..7cece42d06 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -431,7 +431,7 @@ class Table(DialectKWArgs, SchemaItem, TableClause): ] def _gen_cache_key(self, anon_map, bindparams): - return (self,) + return (self,) + self._annotations_cache_key @util.deprecated_params( useexisting=( diff --git a/test/sql/test_compare.py b/test/sql/test_compare.py index 2d0e393314..c594628451 100644 --- a/test/sql/test_compare.py +++ b/test/sql/test_compare.py @@ -167,6 +167,25 @@ class CoreFixtures(object): {"orm": True, "parententity": MyEntity("b", select([table_a]))} ), ), + lambda: ( + table_a, + table_a._annotate({"orm": True}), + table_a._annotate({"orm": True})._annotate({"bar": False}), + table_a._annotate( + {"orm": True, "parententity": MyEntity("a", table_a)} + ), + table_a._annotate( + {"orm": True, "parententity": MyEntity("b", table_a)} + ), + table_a._annotate( + {"orm": True, "parententity": MyEntity("b", select([table_a]))} + ), + ), + lambda: ( + table("a", column("x"), column("y")), + table("a", column("x"), column("y"))._annotate({"orm": True}), + table("b", column("x"), column("y"))._annotate({"orm": True}), + ), lambda: ( cast(column("q"), Integer), cast(column("q"), Float), @@ -776,7 +795,27 @@ class CompareClausesTest(fixtures.TestBase): ne_(t1._generate_cache_key(), t2._generate_cache_key()) - eq_(t1._generate_cache_key().key, (t1,)) + eq_(t1._generate_cache_key().key, (t1, "_annotations", ())) + + def test_compare_metadata_tables_annotations(self): + # metadata Table objects cache on their own identity, not their + # structure. This is mainly to reduce the size of cache keys + # as well as reduce computational overhead, as Table objects have + # very large internal state and they are also generally global + # objects. + + t1 = Table("a", MetaData(), Column("q", Integer), Column("p", Integer)) + t2 = Table("a", MetaData(), Column("q", Integer), Column("p", Integer)) + + t1 = t1._annotate({"orm": True}) + t2 = t2._annotate({"orm": True}) + + ne_(t1._generate_cache_key(), t2._generate_cache_key()) + + eq_( + t1._generate_cache_key().key, + (t1, "_annotations", (("orm", True),)), + ) def test_compare_adhoc_tables(self): # non-metadata tables compare on their structure. these objects are