From cadfc608d63f4e0df46c0daaa28902423fd88d71 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 23 Mar 2020 14:52:05 -0400 Subject: Convert schema_translate to a post compile Revised the :paramref:`.Connection.execution_options.schema_translate_map` feature such that the processing of the SQL statement to receive a specific schema name occurs within the execution phase of the statement, rather than at the compile phase. This is to support the statement being efficiently cached. Previously, the current schema being rendered into the statement for a particular run would be considered as part of the cache key itself, meaning that for a run against hundreds of schemas, there would be hundreds of cache keys, rendering the cache much less performant. The new behavior is that the rendering is done in a similar manner as the "post compile" rendering added in 1.4 as part of :ticket:`4645`, :ticket:`4808`. Fixes: #5004 Change-Id: Ia5c89eb27cc8dc2c5b8e76d6c07c46290a7901b6 --- lib/sqlalchemy/testing/assertions.py | 8 ++++++++ lib/sqlalchemy/testing/assertsql.py | 16 +++++++++------- lib/sqlalchemy/testing/suite/test_reflection.py | 1 - 3 files changed, 17 insertions(+), 8 deletions(-) (limited to 'lib/sqlalchemy/testing') diff --git a/lib/sqlalchemy/testing/assertions.py b/lib/sqlalchemy/testing/assertions.py index e0bf4326e..7dada1394 100644 --- a/lib/sqlalchemy/testing/assertions.py +++ b/lib/sqlalchemy/testing/assertions.py @@ -352,6 +352,8 @@ class AssertsCompiledSQL(object): literal_binds=False, render_postcompile=False, schema_translate_map=None, + render_schema_translate=False, + default_schema_name=None, inline_flag=None, ): if use_default_dialect: @@ -371,6 +373,9 @@ class AssertsCompiledSQL(object): elif isinstance(dialect, util.string_types): dialect = url.URL(dialect).get_dialect()() + if default_schema_name: + dialect.default_schema_name = default_schema_name + kw = {} compile_kwargs = {} @@ -386,6 +391,9 @@ class AssertsCompiledSQL(object): if render_postcompile: compile_kwargs["render_postcompile"] = True + if render_schema_translate: + kw["render_schema_translate"] = True + from sqlalchemy import orm if isinstance(clause, orm.Query): diff --git a/lib/sqlalchemy/testing/assertsql.py b/lib/sqlalchemy/testing/assertsql.py index e38c7ddd8..f0da69400 100644 --- a/lib/sqlalchemy/testing/assertsql.py +++ b/lib/sqlalchemy/testing/assertsql.py @@ -91,21 +91,23 @@ class CompiledSQL(SQLMatchRule): context = execute_observed.context compare_dialect = self._compile_dialect(execute_observed) + + if "schema_translate_map" in context.execution_options: + map_ = context.execution_options["schema_translate_map"] + else: + map_ = None + if isinstance(context.compiled.statement, _DDLCompiles): + compiled = context.compiled.statement.compile( - dialect=compare_dialect, - schema_translate_map=context.execution_options.get( - "schema_translate_map" - ), + dialect=compare_dialect, schema_translate_map=map_ ) else: compiled = context.compiled.statement.compile( dialect=compare_dialect, column_keys=context.compiled.column_keys, inline=context.compiled.inline, - schema_translate_map=context.execution_options.get( - "schema_translate_map" - ), + schema_translate_map=map_, ) _received_statement = re.sub(r"[\n\t]", "", util.text_type(compiled)) parameters = execute_observed.parameters diff --git a/lib/sqlalchemy/testing/suite/test_reflection.py b/lib/sqlalchemy/testing/suite/test_reflection.py index 473c98116..68a43feb7 100644 --- a/lib/sqlalchemy/testing/suite/test_reflection.py +++ b/lib/sqlalchemy/testing/suite/test_reflection.py @@ -360,7 +360,6 @@ class ComponentReflectionTest(fixtures.TablesTest): @testing.requires.schema_reflection def test_dialect_initialize(self): engine = engines.testing_engine() - assert not hasattr(engine.dialect, "default_schema_name") inspect(engine) assert hasattr(engine.dialect, "default_schema_name") -- cgit v1.2.1