From 1c67b1ab8c6fe273d4e175a14f0df5d3cbbd0edc Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 2 Sep 2015 21:52:33 -0400 Subject: [PATCH] - Fixed issue in PG server default comparison where model-side defaults configured with Python unicode literals would leak the "u" character from a ``repr()`` into the SQL used for comparison, creating an invalid SQL expression, as the server-side comparison feature in PG currently repurposes the autogenerate Python rendering feature to get a quoted version of a plain string default. fixes #324 --- alembic/__init__.py | 2 +- alembic/ddl/postgresql.py | 3 ++- docs/build/changelog.rst | 15 +++++++++++++++ tests/test_postgresql.py | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/alembic/__init__.py b/alembic/__init__.py index 5645f100..bbd3e47f 100644 --- a/alembic/__init__.py +++ b/alembic/__init__.py @@ -1,6 +1,6 @@ from os import path -__version__ = '0.8.2' +__version__ = '0.8.3' package_dir = path.abspath(path.dirname(__file__)) diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index 5109b11f..39c380d1 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -47,7 +47,8 @@ class PostgresqlImpl(DefaultImpl): not isinstance(inspector_column.type, Numeric): # 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 = "'%s'" % rendered_metadata_default + rendered_metadata_default = re.sub( + r"^u?'?|'?$", "'", rendered_metadata_default) return not self.connection.scalar( "SELECT %s = %s" % ( diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 27006da5..96fdabce 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -3,6 +3,21 @@ Changelog ========== +.. changelog:: + :version: 0.8.3 + + .. change:: + :tags: bug, autogenerate, postgresql + :tickets: 324 + + Fixed issue in PG server default comparison where model-side defaults + configured with Python unicode literals would leak the "u" character + from a ``repr()`` into the SQL used for comparison, creating an invalid + SQL expression, as the server-side comparison feature in PG currently + repurposes the autogenerate Python rendering feature to get a quoted + version of a plain string default. + + .. changelog:: :version: 0.8.2 :released: August 25, 2015 diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 576d957a..21ba6b9e 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -331,6 +331,21 @@ class PostgresqlDefaultCompareTest(TestBase): diff_expected=False ) + def test_compare_unicode_literal(self): + self._compare_default_roundtrip( + String(), + u'im a default' + ) + + # TOOD: will need to actually eval() the repr() and + # spend more effort figuring out exactly the kind of expression + # to use + def _TODO_test_compare_character_str_w_singlequote(self): + self._compare_default_roundtrip( + String(), + "hel''lo", + ) + def test_compare_character_str(self): self._compare_default_roundtrip( String(), -- 2.47.2