]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug which prevented "domain" built from a
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Oct 2010 21:28:08 +0000 (17:28 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Oct 2010 21:28:08 +0000 (17:28 -0400)
custom type such as "enum" from being reflected.
[ticket:1933]

CHANGES
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index 04c75ac5963178d18d3e772768d379bbe7f57c3b..c2ebff1c498bccd2e9f1d06573ea0a066b06d3d4 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -197,6 +197,10 @@ CHANGES
 - postgresql
    - Added "as_tuple" flag to ARRAY type, returns results
      as tuples instead of lists to allow hashing.
+
+   - Fixed bug which prevented "domain" built from a 
+     custom type such as "enum" from being reflected.
+     [ticket:1933]
      
 - mssql
    - Fixed reflection bug which did not properly handle
index 03260cdb349fc5362f4afe54a5e5bcf1bc30a73f..0d103cb0dc9c566e0fe76ac9f81596c0ef959a6a 100644 (file)
@@ -1053,28 +1053,32 @@ class PGDialect(default.DefaultDialect):
             else:
                 args = ()
             
-            if attype in self.ischema_names:
-                coltype = self.ischema_names[attype]
-            elif attype in enums:
-                enum = enums[attype]
-                coltype = ENUM
-                if "." in attype:
-                    kwargs['schema'], kwargs['name'] = attype.split('.')
-                else:
-                    kwargs['name'] = attype
-                args = tuple(enum['labels'])
-            elif attype in domains:
-                domain = domains[attype]
-                if domain['attype'] in self.ischema_names:
+            while True:
+                if attype in self.ischema_names:
+                    coltype = self.ischema_names[attype]
+                    break
+                elif attype in enums:
+                    enum = enums[attype]
+                    coltype = ENUM
+                    if "." in attype:
+                        kwargs['schema'], kwargs['name'] = attype.split('.')
+                    else:
+                        kwargs['name'] = attype
+                    args = tuple(enum['labels'])
+                    break
+                elif attype in domains:
+                    domain = domains[attype]
+                    attype = domain['attype']
                     # A table can't override whether the domain is nullable.
                     nullable = domain['nullable']
                     if domain['default'] and not default:
                         # It can, however, override the default 
                         # value, but can't set it to null.
                         default = domain['default']
-                    coltype = self.ischema_names[domain['attype']]
-            else:
-                coltype = None
+                    continue
+                else:
+                    coltype = None
+                    break
                 
             if coltype:
                 coltype = coltype(*args, **kwargs)
index 36aa7f2c6f973953795da5e0cb5bb7f0a1b8d257..e20274aefa281caa6a76cf3148f03b5781c3ec5e 100644 (file)
@@ -909,7 +909,10 @@ class DomainReflectionTest(TestBase, AssertsExecutionResults):
         con = testing.db.connect()
         for ddl in \
             'CREATE DOMAIN testdomain INTEGER NOT NULL DEFAULT 42', \
-            'CREATE DOMAIN test_schema.testdomain INTEGER DEFAULT 0':
+            'CREATE DOMAIN test_schema.testdomain INTEGER DEFAULT 0', \
+            "CREATE TYPE testtype AS ENUM ('test')", \
+            'CREATE DOMAIN enumdomain AS testtype'\
+            :
             try:
                 con.execute(ddl)
             except exc.SQLError, e:
@@ -923,6 +926,8 @@ class DomainReflectionTest(TestBase, AssertsExecutionResults):
         con.execute('CREATE TABLE crosschema (question integer, answer '
                     'test_schema.testdomain)')
 
+        con.execute('CREATE TABLE enum_test (id integer, data enumdomain)')
+
     @classmethod
     def teardown_class(cls):
         con = testing.db.connect()
@@ -931,7 +936,10 @@ class DomainReflectionTest(TestBase, AssertsExecutionResults):
         con.execute('DROP TABLE crosschema')
         con.execute('DROP DOMAIN testdomain')
         con.execute('DROP DOMAIN test_schema.testdomain')
-
+        con.execute("DROP TABLE enum_test")
+        con.execute("DROP DOMAIN enumdomain")
+        con.execute("DROP TYPE testtype")
+        
     def test_table_is_reflected(self):
         metadata = MetaData(testing.db)
         table = Table('testtable', metadata, autoload=True)
@@ -946,7 +954,15 @@ class DomainReflectionTest(TestBase, AssertsExecutionResults):
             "Reflected default value didn't equal expected value")
         assert not table.columns.answer.nullable, \
             'Expected reflected column to not be nullable.'
-
+    
+    def test_enum_domain_is_reflected(self):
+        metadata = MetaData(testing.db)
+        table = Table('enum_test', metadata, autoload=True)
+        eq_(
+            table.c.data.type.enums,
+            ('test', )
+        )
+        
     def test_table_is_reflected_test_schema(self):
         metadata = MetaData(testing.db)
         table = Table('testtable', metadata, autoload=True,
@@ -990,6 +1006,7 @@ class DomainReflectionTest(TestBase, AssertsExecutionResults):
         finally:
             postgresql.PGDialect.ischema_names = ischema_names
 
+
 class MiscTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL):
 
     __only_on__ = 'postgresql'