From: Mike Bayer Date: Mon, 4 Mar 2019 14:37:08 +0000 (-0500) Subject: Clarify and correct PostgreSQL server default comparison for py37 X-Git-Tag: rel_1_0_8~2^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e95b81548dde1c363945cd3937b27af90f20459;p=thirdparty%2Fsqlalchemy%2Falembic.git Clarify and correct PostgreSQL server default comparison for py37 Fixed issue where server default comparison on the PostgreSQL dialect would fail for a blank string on Python 3.7 only, due to a change in regular expression behavior in Python 3.7. Change-Id: Iac62ba77622d63ad0c2666cf20a4622f98cf14e6 Fixes: #541 --- diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index a8e332ec..255c7e62 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -68,19 +68,22 @@ class PostgresqlImpl(DefaultImpl): if None in (conn_col_default, rendered_metadata_default): return not defaults_equal + if compat.py2k: + # look for a python 2 "u''" string and filter + m = re.match(r"^u'(.*)'$", rendered_metadata_default) + if m: + rendered_metadata_default = "'%s'" % m.group(1) + + # check for unquoted string and quote for PG String types if ( + not isinstance(inspector_column.type, Numeric) and metadata_column.server_default is not None and isinstance( metadata_column.server_default.arg, compat.string_types ) - and not re.match(r"^'.+'$", rendered_metadata_default) - and not isinstance(inspector_column.type, Numeric) + and not re.match(r"^'.*'$", rendered_metadata_default) ): - # don't single quote if the column type is float/numeric, - # otherwise a comparison such as SELECT 5 = '5.0' will fail - rendered_metadata_default = re.sub( - r"^u?'?|'?$", "'", rendered_metadata_default - ) + rendered_metadata_default = "'%s'" % rendered_metadata_default return not self.connection.scalar( "SELECT %s = %s" % (conn_col_default, rendered_metadata_default) diff --git a/docs/build/unreleased/541.rst b/docs/build/unreleased/541.rst new file mode 100644 index 00000000..9da38503 --- /dev/null +++ b/docs/build/unreleased/541.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, autogenerate, postgresql, py3k + :tickets: 541 + + Fixed issue where server default comparison on the PostgreSQL dialect would + fail for a blank string on Python 3.7 only, due to a change in regular + expression behavior in Python 3.7. + diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 73300f5e..9a0e8f3a 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -506,6 +506,12 @@ class PostgresqlDefaultCompareTest(TestBase): None, col, rendered, cols[0]["default"] ) + def test_compare_string_blank_default(self): + self._compare_default_roundtrip(String(8), '') + + def test_compare_string_nonblank_default(self): + self._compare_default_roundtrip(String(8), 'hi') + def test_compare_interval_str(self): # this form shouldn't be used but testing here # for compatibility