Skip to content

Commit

Permalink
add type synonym for mysql.LONGTEXT / JSON
Browse files Browse the repository at this point in the history
Added a rule to the MySQL impl so that the translation between JSON /
LONGTEXT is accommodated by autogenerate, treating LONGTEXT from the server
as equivalent to an existing JSON in the model.

Change-Id: Ia8370d2269c4decea00aa94beafd3d9d861a1269
Fixes: #968
  • Loading branch information
zzzeek committed Nov 15, 2021
1 parent bb2cf1e commit f0bd40f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
5 changes: 4 additions & 1 deletion alembic/ddl/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class MySQLImpl(DefaultImpl):
__dialect__ = "mysql"

transactional_ddl = False
type_synonyms = DefaultImpl.type_synonyms + ({"BOOL", "TINYINT"},)
type_synonyms = DefaultImpl.type_synonyms + (
{"BOOL", "TINYINT"},
{"JSON", "LONGTEXT"},
)
type_arg_extract = [r"character set ([\w\-_]+)", r"collate ([\w\-_]+)"]

def alter_column( # type:ignore[override]
Expand Down
7 changes: 7 additions & 0 deletions docs/build/unreleased/968.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. change::
:tags: bug, mysql, autogenerate
:tickets: 968

Added a rule to the MySQL impl so that the translation between JSON /
LONGTEXT is accommodated by autogenerate, treating LONGTEXT from the server
as equivalent to an existing JSON in the model.
49 changes: 49 additions & 0 deletions tests/requirements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sqlalchemy import exc as sqla_exc
from sqlalchemy import text

from alembic.testing import exclusions
Expand Down Expand Up @@ -300,6 +301,54 @@ def _mysql_and_check_constraints_exist(self, config):
else:
return False

@property
def json_type(self):
return exclusions.only_on(
[
lambda config: exclusions.against(config, "mysql")
and (
(
not config.db.dialect._is_mariadb
and exclusions.against(config, "mysql >= 5.7")
)
or (
config.db.dialect._mariadb_normalized_version_info
>= (10, 2, 7)
)
),
"mariadb>=10.2.7",
"postgresql >= 9.3",
self._sqlite_json,
self._mssql_json,
]
)

def _mssql_json(self, config):
if not sqla_compat.sqla_14:
return False
else:
return exclusions.against(config, "mssql")

def _sqlite_json(self, config):
if not sqla_compat.sqla_14:
return False
elif not exclusions.against(config, "sqlite >= 3.9"):
return False
else:
with config.db.connect() as conn:
try:
return (
conn.execute(
text(
"""select json_extract('{"foo": "bar"}', """
"""'$."foo"')"""
)
).scalar()
== "bar"
)
except sqla_exc.DBAPIError:
return False

@property
def identity_columns(self):
# TODO: in theory if these could come from SQLAlchemy dialects
Expand Down
7 changes: 5 additions & 2 deletions tests/test_autogen_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sqlalchemy import inspect
from sqlalchemy import INTEGER
from sqlalchemy import Integer
from sqlalchemy import JSON
from sqlalchemy import LargeBinary
from sqlalchemy import MetaData
from sqlalchemy import Numeric
Expand Down Expand Up @@ -927,6 +928,8 @@ def _compare_columns(self, cola, colb):
(String(32),),
(LargeBinary(),),
(Unicode(32),),
(JSON(), config.requirements.json_type),
(mysql.LONGTEXT(), config.requirements.mysql),
(Enum("one", "two", "three", name="the_enum"),),
)
def test_introspected_columns_match_metadata_columns(self, cola):
Expand Down Expand Up @@ -965,7 +968,7 @@ def test_introspected_columns_match_metadata_columns(self, cola):
mysql.VARCHAR(200, charset="latin1"),
mysql.VARCHAR(200, charset="utf-8"),
True,
config.requirements.mysql + config.requirements.sqlalchemy_13,
config.requirements.mysql,
),
(
String(255, collation="utf8_bin"),
Expand All @@ -977,7 +980,7 @@ def test_introspected_columns_match_metadata_columns(self, cola):
String(255, collation="utf8_bin"),
String(255, collation="latin1_bin"),
True,
config.requirements.mysql + config.requirements.sqlalchemy_13,
config.requirements.mysql,
),
)
def test_string_comparisons(self, cola, colb, expect_changes):
Expand Down

0 comments on commit f0bd40f

Please sign in to comment.