]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug where updated PG index reflection as a result of
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 1 Apr 2015 20:50:32 +0000 (16:50 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 1 Apr 2015 20:50:32 +0000 (16:50 -0400)
:ticket:`3184` would cause index operations to fail on Postgresql
versions 8.4 and earlier.  The enhancements are now
disabled when using an older version of Postgresql.
fixes #3343

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_reflection.py
test/requirements.py

index 1b1ab383fb92b99309a232f16e3fa8a8eb61b231..597c8adcd58bfec69dc7876bf661a4e5ce52d226 100644 (file)
 .. changelog::
     :version: 1.0.0b5
 
+    .. change::
+        :tags: bug, postgresql
+        :tickets: 3343
+
+        Fixed bug where updated PG index reflection as a result of
+        :ticket:`3184` would cause index operations to fail on Postgresql
+        versions 8.4 and earlier.  The enhancements are now
+        disabled when using an older version of Postgresql.
+
     .. change::
         :tags: bug, sql
         :tickets: 3346
index 7529c6ed3d34826b0305518187836dd474e55213..c1c0ab08e4076cfd1a4ca0dab43f18d500606bde 100644 (file)
@@ -2601,37 +2601,59 @@ class PGDialect(default.DefaultDialect):
         # cast indkey as varchar since it's an int2vector,
         # returned as a list by some drivers such as pypostgresql
 
-        IDX_SQL = """
-          SELECT
-              i.relname as relname,
-              ix.indisunique, ix.indexprs, ix.indpred,
-              a.attname, a.attnum, c.conrelid, ix.indkey%s
-          FROM
-              pg_class t
-                    join pg_index ix on t.oid = ix.indrelid
-                    join pg_class i on i.oid = ix.indexrelid
-                    left outer join
-                        pg_attribute a
-                        on t.oid = a.attrelid and %s
-                    left outer join
-                        pg_constraint c
-                        on (ix.indrelid = c.conrelid and
-                            ix.indexrelid = c.conindid and
-                            c.contype in ('p', 'u', 'x'))
-          WHERE
-              t.relkind IN ('r', 'v', 'f', 'm')
-              and t.oid = :table_oid
-              and ix.indisprimary = 'f'
-          ORDER BY
-              t.relname,
-              i.relname
-        """ % (
-            # version 8.3 here was based on observing the
-            # cast does not work in PG 8.2.4, does work in 8.3.0.
-            # nothing in PG changelogs regarding this.
-            "::varchar" if self.server_version_info >= (8, 3) else "",
-            self._pg_index_any("a.attnum", "ix.indkey")
-        )
+        if self.server_version_info < (8, 5):
+            IDX_SQL = """
+              SELECT
+                  i.relname as relname,
+                  ix.indisunique, ix.indexprs, ix.indpred,
+                  a.attname, a.attnum, NULL, ix.indkey%s
+              FROM
+                  pg_class t
+                        join pg_index ix on t.oid = ix.indrelid
+                        join pg_class i on i.oid = ix.indexrelid
+                        left outer join
+                            pg_attribute a
+                            on t.oid = a.attrelid and %s
+              WHERE
+                  t.relkind IN ('r', 'v', 'f', 'm')
+                  and t.oid = :table_oid
+                  and ix.indisprimary = 'f'
+              ORDER BY
+                  t.relname,
+                  i.relname
+            """ % (
+                # version 8.3 here was based on observing the
+                # cast does not work in PG 8.2.4, does work in 8.3.0.
+                # nothing in PG changelogs regarding this.
+                "::varchar" if self.server_version_info >= (8, 3) else "",
+                self._pg_index_any("a.attnum", "ix.indkey")
+            )
+        else:
+            IDX_SQL = """
+              SELECT
+                  i.relname as relname,
+                  ix.indisunique, ix.indexprs, ix.indpred,
+                  a.attname, a.attnum, c.conrelid, ix.indkey::varchar
+              FROM
+                  pg_class t
+                        join pg_index ix on t.oid = ix.indrelid
+                        join pg_class i on i.oid = ix.indexrelid
+                        left outer join
+                            pg_attribute a
+                            on t.oid = a.attrelid and a.attnum = ANY(ix.indkey)
+                        left outer join
+                            pg_constraint c
+                            on (ix.indrelid = c.conrelid and
+                                ix.indexrelid = c.conindid and
+                                c.contype in ('p', 'u', 'x'))
+              WHERE
+                  t.relkind IN ('r', 'v', 'f', 'm')
+                  and t.oid = :table_oid
+                  and ix.indisprimary = 'f'
+              ORDER BY
+                  t.relname,
+                  i.relname
+            """
 
         t = sql.text(IDX_SQL, typemap={'attname': sqltypes.Unicode})
         c = connection.execute(t, table_oid=table_oid)
index 0dda1fa452b7dc20b571b070c6f45cd47c7994c7..0ebe68cba040c37bbeacb8d6b027eef9c3579e64 100644 (file)
@@ -817,6 +817,7 @@ class ReflectionTest(fixtures.TestBase):
             }])
 
     @testing.provide_metadata
+    @testing.only_on("postgresql>=8.5")
     def test_reflection_with_unique_constraint(self):
         insp = inspect(testing.db)
 
index 67bdfb8a34eec43357534daece5b07f2a6daf165..3ed6bea4da54a9476b80c20225db141c4fbcb218 100644 (file)
@@ -372,7 +372,7 @@ class DefaultRequirements(SuiteRequirements):
     @property
     def window_functions(self):
         return only_if([
-                    "postgresql", "mssql", "oracle"
+                    "postgresql>=8.4", "mssql", "oracle"
                 ], "Backend does not support window functions")
 
     @property