]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Added the ability to strip the schema when using tometadata by passing schema=None...
authorMichael Trier <mtrier@gmail.com>
Thu, 25 Feb 2010 06:34:37 +0000 (06:34 +0000)
committerMichael Trier <mtrier@gmail.com>
Thu, 25 Feb 2010 06:34:37 +0000 (06:34 +0000)
CHANGES
lib/sqlalchemy/schema.py
test/engine/test_metadata.py

diff --git a/CHANGES b/CHANGES
index 23e16e2e9726a05bc485e00f36a3515ea95e14e2..ab0c14fffab44fcffbdf3a07a67e2f34bcc4a5ce 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -181,6 +181,12 @@ CHANGES
     Note that it is *not* built/installed by default.
     See README for installation instructions.
 
+- metadata
+  - Added the ability to strip schema information when using
+    "tometadata" by passing "schema=None" as an argument. If schema
+    is not specified then the table's schema is retained.
+    [ticket: 1673]
+
 - declarative
   - DeclarativeMeta exclusively uses cls.__dict__ (not dict_) 
     as the source of class information; _as_declarative exclusively 
index 80c03bbbaf506331eb8af6eeea235c0b8b4e1bf1..3c6652807a514b2e3032be3a2954945107a4382a 100644 (file)
@@ -43,6 +43,8 @@ __all__ = ['SchemaItem', 'Table', 'Column', 'ForeignKey', 'Sequence', 'Index',
            ]
 __all__.sort()
 
+RETAIN_SCHEMA = util.symbol('retain_schema')
+
 class SchemaItem(visitors.Visitable):
     """Base class for items that define a database schema."""
 
@@ -413,11 +415,11 @@ class Table(SchemaItem, expression.TableClause):
         """
         self.metadata.drop_all(bind=bind, checkfirst=checkfirst, tables=[self])
 
-    def tometadata(self, metadata, schema=None):
+    def tometadata(self, metadata, schema=RETAIN_SCHEMA):
         """Return a copy of this ``Table`` associated with a different ``MetaData``."""
 
         try:
-            if not schema:
+            if schema is RETAIN_SCHEMA:
                 schema = self.schema
             key = _get_table_key(self.name, schema)
             return metadata.tables[key]
index 50109e15a91ee0673b99e58b584b69fbde18a80a..0d2cb7775695c8a0d7b2a3742643670b71c1e42d 100644 (file)
@@ -203,8 +203,57 @@ class MetaDataTest(TestBase, ComparesTables):
 
         eq_(str(table_c.join(table2_c).onclause), str(table_c.c.myid == table2_c.c.myid))
         eq_(str(table_c.join(table2_c).onclause), "someschema.mytable.myid = someschema.othertable.myid")
-        
-        
+
+    def test_tometadata_default_schema(self):
+        meta = MetaData()
+
+        table = Table('mytable', meta,
+            Column('myid', Integer, primary_key=True),
+            Column('name', String(40), nullable=True),
+            Column('description', String(30), CheckConstraint("description='hi'")),
+            UniqueConstraint('name'),
+            test_needs_fk=True,
+            schema='myschema',
+        )
+
+        table2 = Table('othertable', meta,
+            Column('id', Integer, primary_key=True),
+            Column('myid', Integer, ForeignKey('myschema.mytable.myid')),
+            test_needs_fk=True,
+            schema='myschema',
+            )
+
+        meta2 = MetaData()
+        table_c = table.tometadata(meta2)
+        table2_c = table2.tometadata(meta2)
+
+        eq_(str(table_c.join(table2_c).onclause), str(table_c.c.myid == table2_c.c.myid))
+        eq_(str(table_c.join(table2_c).onclause), "myschema.mytable.myid = myschema.othertable.myid")
+
+    def test_tometadata_strip_schema(self):
+        meta = MetaData()
+
+        table = Table('mytable', meta,
+            Column('myid', Integer, primary_key=True),
+            Column('name', String(40), nullable=True),
+            Column('description', String(30), CheckConstraint("description='hi'")),
+            UniqueConstraint('name'),
+            test_needs_fk=True,
+        )
+
+        table2 = Table('othertable', meta,
+            Column('id', Integer, primary_key=True),
+            Column('myid', Integer, ForeignKey('mytable.myid')),
+            test_needs_fk=True,
+            )
+
+        meta2 = MetaData()
+        table_c = table.tometadata(meta2, schema=None)
+        table2_c = table2.tometadata(meta2, schema=None)
+
+        eq_(str(table_c.join(table2_c).onclause), str(table_c.c.myid == table2_c.c.myid))
+        eq_(str(table_c.join(table2_c).onclause), "mytable.myid = othertable.myid")
+
     def test_nonexistent(self):
         assert_raises(tsa.exc.NoSuchTableError, Table,
                           'fake_table',