]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Clarify and correct PostgreSQL server default comparison for py37
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 4 Mar 2019 14:37:08 +0000 (09:37 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 4 Mar 2019 14:37:08 +0000 (09:37 -0500)
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
alembic/ddl/postgresql.py
docs/build/unreleased/541.rst [new file with mode: 0644]
tests/test_postgresql.py

index a8e332ec911f8df39d79da19c451ba59bf8004e0..255c7e6238a042c0e7605e17fc76fb918be31195 100644 (file)
@@ -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 (file)
index 0000000..9da3850
--- /dev/null
@@ -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.
+
index 73300f5efb79a23612a8e658ee18f0d4f07a58d6..9a0e8f3aa93bb8ce3f38adfa7041a64074432318 100644 (file)
@@ -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