]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Ensure .engine is part of Connectable interface, implement as descriptor
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 8 Jul 2019 14:42:20 +0000 (10:42 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 8 Jul 2019 14:42:20 +0000 (10:42 -0400)
Fixed bug where using reflection function such as :meth:`.MetaData.reflect`
with an :class:`.Engine` object that had execution options applied to it
would fail, as the resulting :class:`.OptionEngine` proxy object failed to
include a ``.engine`` attribute used within the reflection routines.

Fixes: #4754
Change-Id: I6c342af5c6db6fe362b9d25f3f26d6859f62f87a

doc/build/changelog/unreleased_13/4754.rst [new file with mode: 0644]
lib/sqlalchemy/engine/base.py
lib/sqlalchemy/engine/interfaces.py
test/engine/test_execute.py
test/engine/test_reflection.py

diff --git a/doc/build/changelog/unreleased_13/4754.rst b/doc/build/changelog/unreleased_13/4754.rst
new file mode 100644 (file)
index 0000000..d8a3375
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, engine
+    :tickets: 4754
+
+    Fixed bug where using reflection function such as :meth:`.MetaData.reflect`
+    with an :class:`.Engine` object that had execution options applied to it
+    would fail, as the resulting :class:`.OptionEngine` proxy object failed to
+    include a ``.engine`` attribute used within the reflection routines.
index 7aee1a73b2d318fd5ba111b4f522110930e9b683..07d1b8f893a465ddcd4e62560dd241e780516361 100644 (file)
@@ -1899,13 +1899,16 @@ class Engine(Connectable, log.Identified):
         if logging_name:
             self.logging_name = logging_name
         self.echo = echo
-        self.engine = self
         log.instance_logger(self, echoflag=echo)
         if proxy:
             interfaces.ConnectionProxy._adapt_listener(self, proxy)
         if execution_options:
             self.update_execution_options(**execution_options)
 
+    @property
+    def engine(self):
+        return self
+
     def update_execution_options(self, **opt):
         r"""Update the default execution_options dictionary
         of this :class:`.Engine`.
index 4806e72a587892c38f11c7f18a3e16e15a7fc3fd..aadadb84575cfe0dd45ac325378d08dd0b7b69cc 100644 (file)
@@ -1117,6 +1117,13 @@ class Connectable(object):
 
         """
 
+    engine = None
+    """The :class:`.Engine` instance referred to by this :class:`.Connectable`.
+
+    May be ``self`` if this is already an :class:`.Engine`.
+
+    """
+
     @util.deprecated(
         "1.3",
         "The :meth:`.Engine.contextual_connect` and "
index 20c8ae659c8c6b8d7c46e5cfdf9dc001d93f8ea8..335c3a487b6b02db3e1b6c76c65199d55b5ea595 100644 (file)
@@ -635,10 +635,14 @@ class ExecuteTest(fixtures.TestBase):
             options={"execution_options": {"base": "x1"}}
         )
 
+        is_(eng.engine, eng)
+
         eng1 = eng.execution_options(foo="b1")
+        is_(eng1.engine, eng1)
         eng2 = eng.execution_options(foo="b2")
         eng1a = eng1.execution_options(bar="a1")
         eng2a = eng2.execution_options(foo="b3", bar="a2")
+        is_(eng2a.engine, eng2a)
 
         eq_(eng._execution_options, {"base": "x1"})
         eq_(eng1._execution_options, {"base": "x1", "foo": "b1"})
index 3dc3a4461cf11416f5dfd73fa294e937fea46960..1cabf8963c6b18f9e48b0a4213bc97adc1b2f629 100644 (file)
@@ -1198,6 +1198,11 @@ class ReflectionTest(fixtures.TestBase, ComparesTables):
     def test_reflect_uses_bind_engine_reflect(self):
         self._test_reflect_uses_bind(lambda e: MetaData().reflect(e))
 
+    def test_reflect_uses_bind_option_engine_reflect(self):
+        self._test_reflect_uses_bind(
+            lambda e: MetaData().reflect(e.execution_options(foo="bar"))
+        )
+
     @testing.provide_metadata
     def test_reflect_all(self):
         existing = testing.db.table_names()