From: Mike Bayer Date: Wed, 22 Aug 2018 00:59:04 +0000 (-0400) Subject: Strip quotes from format_type in addition to other characters X-Git-Tag: rel_1_3_0b1~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32ce703a98eba8a7685e609b4a7ca86b79dd0904;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Strip quotes from format_type in addition to other characters Fixed bug in PostgreSQL ENUM reflection where a case-sensitive, quoted name would be reported by the query including quotes, which would not match a target column during table reflection as the quotes needed to be stripped off. Fixes: #4323 Change-Id: I668f3acccc578e58f23b70c82d31d5c1ec194913 --- diff --git a/doc/build/changelog/unreleased_12/4323.rst b/doc/build/changelog/unreleased_12/4323.rst new file mode 100644 index 0000000000..fa9b4f5b65 --- /dev/null +++ b/doc/build/changelog/unreleased_12/4323.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, postgresql + :tickets: 4323 + + Fixed bug in PostgreSQL ENUM reflection where a case-sensitive, quoted name + would be reported by the query including quotes, which would not match a + target column during table reflection as the quotes needed to be stripped + off. + diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index ad15cb0eab..7db26e4c0c 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2599,6 +2599,9 @@ class PGDialect(default.DefaultDialect): # with time zone, geometry(POLYGON), etc. attype = re.sub(r'\(.*\)', '', format_type) + # strip quotes from case sensitive enum names + attype = re.sub(r'^"|"$', '', attype) + # strip '[]' from integer[], etc. attype = attype.replace('[]', '') @@ -3128,7 +3131,6 @@ class PGDialect(default.DefaultDialect): 'labels': [enum['label']], } enums.append(enum_rec) - return enums def _load_domains(self, connection): diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index 5c52195d13..70bc26e0b6 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -908,6 +908,41 @@ class ReflectionTest(fixtures.TestBase): 'schema': 'public' }]) + @testing.provide_metadata + def test_inspect_enums_case_sensitive(self): + enum_type = postgresql.ENUM( + 'CapsOne', 'CapsTwo', name='UpperCase', metadata=self.metadata) + enum_type.create(testing.db) + inspector = reflection.Inspector.from_engine(testing.db) + eq_(inspector.get_enums(), [ + { + 'visible': True, + 'labels': ['CapsOne', 'CapsTwo'], + 'name': 'UpperCase', + 'schema': 'public' + }]) + + @testing.provide_metadata + def test_inspect_enums_case_sensitive_from_table(self): + enum_type = postgresql.ENUM( + 'CapsOne', 'CapsTwo', name='UpperCase', metadata=self.metadata) + + t = Table('t', self.metadata, Column('q', enum_type)) + + enum_type.create(testing.db) + t.create(testing.db) + + inspector = reflection.Inspector.from_engine(testing.db) + cols = inspector.get_columns("t") + cols[0]['type'] = (cols[0]['type'].name, cols[0]['type'].enums) + eq_(cols, [ + { + 'name': 'q', + 'type': ('UpperCase', ['CapsOne', 'CapsTwo']), + 'nullable': True, 'default': None, + 'autoincrement': False, 'comment': None} + ]) + @testing.provide_metadata def test_inspect_enums_star(self): enum_type = postgresql.ENUM(