]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added "schema" argument to all has_table() calls, only supported so far by PG
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 4 Feb 2007 23:45:45 +0000 (23:45 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 4 Feb 2007 23:45:45 +0000 (23:45 +0000)
- added basic unit test for PG reflection of tables in an alternate schema

lib/sqlalchemy/ansisql.py
lib/sqlalchemy/databases/firebird.py
lib/sqlalchemy/databases/mssql.py
lib/sqlalchemy/databases/mysql.py
lib/sqlalchemy/databases/oracle.py
lib/sqlalchemy/databases/sqlite.py
lib/sqlalchemy/engine/base.py
lib/sqlalchemy/schema.py
test/engine/reflection.py

index 63ee5f7fdc1932a5110dd06f3e3001035b4299d7..974319845d613a4c3b86f272139c1064782c2c25 100644 (file)
@@ -677,7 +677,7 @@ class ANSISchemaGenerator(ANSISchemaBase):
         raise NotImplementedError()
     
     def visit_metadata(self, metadata):
-        collection = [t for t in metadata.table_iterator(reverse=False, tables=self.tables) if (not self.checkfirst or not self.dialect.has_table(self.connection, t.name))]
+        collection = [t for t in metadata.table_iterator(reverse=False, tables=self.tables) if (not self.checkfirst or not self.dialect.has_table(self.connection, t.name, schema=t.schema))]
         for table in collection:
             table.accept_schema_visitor(self, traverse=False)
         if self.supports_alter():
@@ -807,7 +807,7 @@ class ANSISchemaDropper(ANSISchemaBase):
         self.dialect = self.engine.dialect
 
     def visit_metadata(self, metadata):
-        collection = [t for t in metadata.table_iterator(reverse=True, tables=self.tables) if (not self.checkfirst or  self.dialect.has_table(self.connection, t.name))]
+        collection = [t for t in metadata.table_iterator(reverse=True, tables=self.tables) if (not self.checkfirst or  self.dialect.has_table(self.connection, t.name, schema=t.schema))]
         if self.supports_alter():
             for alterable in self.find_alterables(collection):
                 self.drop_foreignkey(alterable)
index 6af6d6d87c569c65d3a693be8dfc2557a9df2ecb..5a25b12db3efddf5feafc0d8d6dccb5e92bf58c3 100644 (file)
@@ -167,7 +167,7 @@ class FBDialect(ansisql.ANSIDialect):
     def preparer(self):
         return FBIdentifierPreparer(self)
 
-    def has_table(self, connection, table_name):
+    def has_table(self, connection, table_name, schema=None):
         tblqry = """
         SELECT count(*)
         FROM RDB$RELATIONS R
index 6d351d0fb47c6f73fb284f5bfdfc7ac45410c629..fbb89de1ab3f9b3c1366ace2f0cea5d2f9396f21 100644 (file)
@@ -345,10 +345,10 @@ class MSSQLDialect(ansisql.ANSIDialect):
             c.name = c.name.upper()
         return t
 
-    def has_table(self, connection, tablename):
+    def has_table(self, connection, tablename, schema=None):
         import sqlalchemy.databases.information_schema as ischema
 
-        current_schema = self.get_default_schema_name()
+        current_schema = schema or self.get_default_schema_name()
         columns = self.uppercase_table(ischema.columns)
         s = sql.select([columns],
                    current_schema
index c09d02756c85dbefb9509da3e9be2b8b295afe9a..c6bf2695fddec19a55f98141ab49ce7ee13fb3a9 100644 (file)
@@ -335,7 +335,7 @@ class MySQLDialect(ansisql.ANSIDialect):
     def dbapi(self):
         return self.module
 
-    def has_table(self, connection, table_name):
+    def has_table(self, connection, table_name, schema=None):
         cursor = connection.execute("show table status like '" + table_name + "'")
         return bool( not not cursor.rowcount )
 
index 7221d566202ff43f553457cac6a124a67110f0cd..2db90959243d09f7d696301b7a89ad79c0e4e9c8 100644 (file)
@@ -211,7 +211,7 @@ class OracleDialect(ansisql.ANSIDialect):
         return OracleDefaultRunner(engine, proxy)
 
 
-    def has_table(self, connection, table_name):
+    def has_table(self, connection, table_name, schema=None):
         cursor = connection.execute("""select table_name from all_tables where table_name=:name""", {'name':table_name.upper()})
         return bool( cursor.fetchone() is not None )
 
index dda488f9c01c01c02c2332ca3969778ff0bbe44c..2ab3c0d5ad944fffde992a267cbff7c6345aa35d 100644 (file)
@@ -171,7 +171,7 @@ class SQLiteDialect(ansisql.ANSIDialect):
     def dbapi(self):
         return sqlite
         
-    def has_table(self, connection, table_name):
+    def has_table(self, connection, table_name, schema=None):
         cursor = connection.execute("PRAGMA table_info(" + table_name + ")", {})
         row = cursor.fetchone()
         
index 69d9ef9556e6cbcee461be4af49d37d99d64584b..8871cac1daf1d8c7405d3e6d5621a98fb86f2427 100644 (file)
@@ -91,7 +91,7 @@ class Dialect(sql.AbstractDialect):
     def reflecttable(self, connection, table):
         """given an Connection and a Table object, reflects its columns and properties from the database."""
         raise NotImplementedError()
-    def has_table(self, connection, table_name):
+    def has_table(self, connection, table_name, schema=None):
         raise NotImplementedError()
     def has_sequence(self, connection, sequence_name):
         raise NotImplementedError()
@@ -516,8 +516,8 @@ class Engine(sql.Executor, Connectable):
         finally:
             if connection is None:
                 conn.close()
-    def has_table(self, table_name):
-        return self.run_callable(lambda c: self.dialect.has_table(c, table_name))
+    def has_table(self, table_name, schema=None):
+        return self.run_callable(lambda c: self.dialect.has_table(c, table_name, schema=schema))
         
     def raw_connection(self):
         """returns a DBAPI connection."""
index ae712252bab3680c398ad89479c99befed431730..a2d328bf354812154c2f0a3eaf48ab6b780aa4dc 100644 (file)
@@ -272,7 +272,7 @@ class Table(SchemaItem, sql.TableClause):
 
         def do(conn):
             e = conn.engine
-            return e.dialect.has_table(conn, self.name)
+            return e.dialect.has_table(conn, self.name, schema=self.schema)
         return connectable.run_callable(do)
 
     def create(self, connectable=None, checkfirst=False):
index 3467c6ae423beb26a1de53e8f4af8b546bededf1..c77e15e28452f93229ffc59c99c0b163a8a4b248 100644 (file)
@@ -491,9 +491,37 @@ class SchemaTest(PersistTest):
         print buf
         assert buf.index("CREATE TABLE someschema.table1") > -1
         assert buf.index("CREATE TABLE someschema.table2") > -1
-         
-        
+    
+    @testbase.supported('postgres')
+    def testpg(self):
+        """note: this test requires that the 'test_schema' schema be separate and accessible by the test user"""
         
+        meta1 = BoundMetaData(testbase.db)
+        users = Table('users', meta1,
+            Column('user_id', Integer, primary_key = True),
+            Column('user_name', String(30), nullable = False),
+            schema="test_schema"
+            )
+
+        addresses = Table('email_addresses', meta1,
+            Column('address_id', Integer, primary_key = True),
+            Column('remote_user_id', Integer, ForeignKey(users.c.user_id)),
+            Column('email_address', String(20)),
+            schema="test_schema"
+        )
+        meta1.create_all()
+        try:
+            meta2 = BoundMetaData(testbase.db)
+            users = Table('users', meta2, autoload = True, schema="test_schema")
+            addresses = Table('email_addresses', meta2, autoload = True, schema="test_schema")
+
+            print users
+            print addresses
+            j = join(users, addresses)
+            print str(j.onclause)
+            self.assert_((users.c.user_id==addresses.c.remote_user_id).compare(j.onclause))
+        finally:
+            meta1.drop_all()
         
 if __name__ == "__main__":
     testbase.main()