From b058e7ae99890f981c8df016a314283a2a3f68a2 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 4 May 2019 13:19:10 -0400 Subject: [PATCH] Open up mysql CHECK constraint detection to include new versions MySQL 8.0.16 introduces real CHECK constraints and MariaDB has also added them into the 10.2 series sometime before 10.2.22. Change-Id: Ia0f1be69f99df935aae069f63381bcc994f73cc7 (cherry picked from commit 1c3e92627362604472ca483055fc827a97942e6b) --- lib/sqlalchemy/dialects/mysql/base.py | 1 - test/engine/test_reflection.py | 2 +- test/requirements.py | 34 +++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 5725b1f05b..81bdd60c94 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -2610,7 +2610,6 @@ class MySQLDialect(default.DefaultDialect): @reflection.cache def get_check_constraints(self, connection, table_name, schema=None, **kw): - parsed_state = self._parsed_state_or_create( connection, table_name, schema, **kw ) diff --git a/test/engine/test_reflection.py b/test/engine/test_reflection.py index 273cc73565..b414e15ad4 100644 --- a/test/engine/test_reflection.py +++ b/test/engine/test_reflection.py @@ -1388,7 +1388,7 @@ class ReflectionTest(fixtures.TestBase, ComparesTables): if isinstance(const, sa.CheckConstraint) ][0] - eq_regex(ck.sqltext.text, r".?q.? > 10") + eq_regex(ck.sqltext.text, r"[\(`]*q[\)`]* > 10") eq_(ck.name, "ck1") @testing.provide_metadata diff --git a/test/requirements.py b/test/requirements.py index ab3f01d04a..059ce01871 100644 --- a/test/requirements.py +++ b/test/requirements.py @@ -52,7 +52,7 @@ class DefaultRequirements(SuiteRequirements): """Target database must also enforce check constraints.""" return self.check_constraints + fails_on( - self._mysql_not_mariadb_102, + self._mysql_check_constraints_dont_exist, "check constraints don't enforce on MySQL, MariaDB<10.2", ) @@ -457,7 +457,10 @@ class DefaultRequirements(SuiteRequirements): @property def check_constraint_reflection(self): return fails_on_everything_except( - "postgresql", "sqlite", "oracle", self._mariadb_102 + "postgresql", + "sqlite", + "oracle", + self._mysql_and_check_constraints_exist, ) @property @@ -1311,6 +1314,33 @@ class DefaultRequirements(SuiteRequirements): and config.db.dialect._mariadb_normalized_version_info > (10, 2) ) + def _mysql_and_check_constraints_exist(self, config): + # 1. we have mysql / mariadb and + # 2. it enforces check constraints + if exclusions.against(config, "mysql"): + if config.db.dialect._is_mariadb: + norm_version_info = ( + config.db.dialect._mariadb_normalized_version_info + ) + return norm_version_info >= (10, 2) + else: + norm_version_info = config.db.dialect.server_version_info + return norm_version_info >= (8, 0, 16) + else: + return False + + def _mysql_check_constraints_exist(self, config): + # 1. we dont have mysql / mariadb or + # 2. we have mysql / mariadb that enforces check constraints + return not exclusions.against( + config, "mysql" + ) or self._mysql_and_check_constraints_exist(config) + + def _mysql_check_constraints_dont_exist(self, config): + # 1. we have mysql / mariadb and + # 2. they dont enforce check constraints + return not self._mysql_check_constraints_exist(config) + def _mysql_not_mariadb_102(self, config): return against(config, "mysql") and ( not config.db.dialect._is_mariadb -- 2.47.3