]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add schema argument to AutomapBase.prepare()
authorJosh Marlow <joshmarlow@gmail.com>
Tue, 12 Apr 2016 03:16:12 +0000 (23:16 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 2 Jun 2016 18:00:13 +0000 (14:00 -0400)
This allows automap to reflect tables from a schema other than the
default without the need to resort to calling MetaData.reflect
directly.

Change-Id: Ie73cb113bd6d115555c09c5efc33d27ad2c9c512
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/237

doc/build/changelog/changelog_11.rst
lib/sqlalchemy/ext/automap.py
test/ext/test_automap.py

index 898b8a0ba32e70d87ad6e93a0ce2b6afc3ef13bb..8e7c962ce35bceea8eeb10dcf0fd26410edadb1e 100644 (file)
         Complements the same-named parameter on :class:`.Table`.  Pull
         request courtesy Benjamin Bertrand.
 
+    .. change::
+        :tags: orm, feature
+        :pullreq: github:237
+
+        Added :paramref:`.AutomapBase.prepare.schema` to the
+        :meth:`.AutomapBase.prepare` method, to indicate which schema
+        tables should be reflected from if not the default schema.
+        Pull request courtesy Josh Marlow.
+
     .. change::
         :tags: bug, mssql
         :pullreq: bitbucket:58
index bb1c5da3d8e4200cc3c02b732733789d416fee9d..23ad56b7635e7f39df5dd39c227fb18111369c36 100644 (file)
@@ -695,6 +695,7 @@ class AutomapBase(object):
             cls,
             engine=None,
             reflect=False,
+            schema=None,
             classname_for_table=classname_for_table,
             collection_class=list,
             name_for_scalar_relationship=name_for_scalar_relationship,
@@ -735,10 +736,19 @@ class AutomapBase(object):
          when a new :func:`.relationship` object is created that represents a
          collection.  Defaults to ``list``.
 
+        :param schema: When present in conjunction with the
+         :paramref:`.AutomapBase.prepare.reflect` flag, is passed to
+         :meth:`.MetaData.reflect` to indicate the primary schema where tables
+         should be reflected from.  When omitted, the default schema in use
+         by the database connection is used.
+
+         .. versionadded:: 1.1
+
         """
         if reflect:
             cls.metadata.reflect(
                 engine,
+                schema=schema,
                 extend_existing=True,
                 autoload_replace=False
             )
index 0a57b9caa542e1146c945f4d5b42e47545692f08..0c5c4e9ddf35143afd7b70c3342a410df6788dbb 100644 (file)
@@ -3,7 +3,7 @@ from ..orm._fixtures import FixtureTest
 from sqlalchemy.ext.automap import automap_base
 from sqlalchemy.orm import relationship, interfaces, configure_mappers
 from sqlalchemy.ext.automap import generate_relationship
-from sqlalchemy.testing.mock import Mock
+from sqlalchemy.testing.mock import Mock, patch
 from sqlalchemy import String, Integer, ForeignKey
 from sqlalchemy import testing
 from sqlalchemy.testing.schema import Table, Column
@@ -71,6 +71,41 @@ class AutomapTest(fixtures.MappedTest):
         n1.nodes_collection.append(n2)
         assert n2.nodes is n1
 
+    def test_prepare_accepts_optional_schema_arg(self):
+        """
+        The underlying reflect call accepts an optional schema argument.
+        This is for determining which database schema to load.
+        This test verifies that prepare can accept an optiona schema argument
+        and pass it to reflect.
+        """
+        Base = automap_base(metadata=self.metadata)
+        engine_mock = Mock()
+        with patch.object(Base.metadata, "reflect") as reflect_mock:
+            Base.prepare(engine_mock, reflect=True, schema="some_schema")
+            reflect_mock.assert_called_once_with(
+                engine_mock,
+                schema="some_schema",
+                extend_existing=True,
+                autoload_replace=False,
+            )
+
+    def test_prepare_defaults_to_no_schema(self):
+        """
+        The underlying reflect call accepts an optional schema argument.
+        This is for determining which database schema to load.
+        This test verifies that prepare passes a default None if no schema is provided.
+        """
+        Base = automap_base(metadata=self.metadata)
+        engine_mock = Mock()
+        with patch.object(Base.metadata, "reflect") as reflect_mock:
+            Base.prepare(engine_mock, reflect=True)
+            reflect_mock.assert_called_once_with(
+                engine_mock,
+                schema=None,
+                extend_existing=True,
+                autoload_replace=False,
+            )
+
     def test_naming_schemes(self):
         Base = automap_base(metadata=self.metadata)