From: Mike Bayer Date: Mon, 19 Feb 2018 19:00:11 +0000 (-0500) Subject: Repair server default comparison for MySQL / MariaDB X-Git-Tag: rel_0_9_9~3^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c551955fc6f089c311cc22a241a19e78f22516b1;p=thirdparty%2Fsqlalchemy%2Falembic.git Repair server default comparison for MySQL / MariaDB The regexp for comparing the TIMESTAMP function was obliterating all other comparisons as it was incorrect. Add new regexp for integers that also adjusts for mariadb 10.2 quoting differences vs. mariadb 10.1 Change-Id: I7bdaceb7e0dbe06bc2c3690cd5dd8e737716278c Fixes: #455 Fixes: #483 --- diff --git a/alembic/ddl/mysql.py b/alembic/ddl/mysql.py index 71b186cd..32e67ee3 100644 --- a/alembic/ddl/mysql.py +++ b/alembic/ddl/mysql.py @@ -89,20 +89,25 @@ class MySQLImpl(DefaultImpl): # partially a workaround for SQLAlchemy issue #3023; if the # column were created without "NOT NULL", MySQL may have added # an implicit default of '0' which we need to skip + # TODO: this is not really covered anymore ? if metadata_column.type._type_affinity is sqltypes.Integer and \ inspector_column.primary_key and \ not inspector_column.autoincrement and \ not rendered_metadata_default and \ rendered_inspector_default == "'0'": return False + elif inspector_column.type._type_affinity is sqltypes.Integer: + rendered_inspector_default = re.sub( + r"^'|'$", '', rendered_inspector_default) + return rendered_inspector_default != rendered_metadata_default elif rendered_inspector_default and rendered_metadata_default: # adjust for "function()" vs. "FUNCTION" return ( re.sub( - r'(.*)(\(\))?$', '\1', + r'(.*?)(?:\(\))?$', r'\1', rendered_inspector_default.lower()) != re.sub( - r'(.*)(\(\))?$', '\1', + r'(.*?)(?:\(\))?$', r'\1', rendered_metadata_default.lower()) ) else: diff --git a/docs/build/unreleased/455.rst b/docs/build/unreleased/455.rst new file mode 100644 index 00000000..c9d439df --- /dev/null +++ b/docs/build/unreleased/455.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, autogenerate, mysql + :tickets: 455 + + The fix for :ticket:`455` in version 0.9.6 involving MySQL server default + comparison was entirely non functional, as the test itself was also broken + and didn't reveal that it wasn't working. The regular expression to compare + server default values like CURRENT_TIMESTAMP to current_timestamp() is + repaired. diff --git a/docs/build/unreleased/483.rst b/docs/build/unreleased/483.rst new file mode 100644 index 00000000..fc4ca12a --- /dev/null +++ b/docs/build/unreleased/483.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: bug, mysql, autogenerate + :tickets: 483 + + Fixed bug where MySQL server default comparisons were basically not working + at all due to incorrect regexp added in :ticket:`455`. Also accommodates + for MariaDB 10.2 quoting differences in reporting integer based server + defaults. + + + diff --git a/tests/test_mysql.py b/tests/test_mysql.py index e7b1cf08..321488fa 100644 --- a/tests/test_mysql.py +++ b/tests/test_mysql.py @@ -277,9 +277,11 @@ class MySQLDefaultCompareTest(TestBase): t1.create(self.bind) insp = Inspector.from_engine(self.bind) cols = insp.get_columns(t1.name) + refl = Table(t1.name, MetaData()) + insp.reflecttable(refl, None) ctx = self.autogen_context['context'] return ctx.impl.compare_server_default( - None, + refl.c[cols[0]['name']], col, rendered, cols[0]['default']) @@ -295,3 +297,23 @@ class MySQLDefaultCompareTest(TestBase): TIMESTAMP(), None, "CURRENT_TIMESTAMP", ) + + def test_compare_integer_same(self): + self._compare_default_roundtrip( + Integer(), "5" + ) + + def test_compare_integer_diff(self): + self._compare_default_roundtrip( + Integer(), "5", "7" + ) + + def test_compare_boolean_same(self): + self._compare_default_roundtrip( + Boolean(), "1" + ) + + def test_compare_boolean_diff(self): + self._compare_default_roundtrip( + Boolean(), "1", "0" + )