Skip to content

Commit 1dd48a1

Browse files
committed
Truncate MySQL comments that exceed MySQL's length limits in export sql
1 parent a9fdea8 commit 1dd48a1

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99
### Added
1010
- Trino connections support custom HTTP headers via `DATACONTRACT_TRINO_HTTP_HEADERS`
11-
- `datacontract export sql`: MySQL tables and columns get their descriptions as `COMMENT`s (#1811)
11+
- `datacontract export sql` writes MySQL table and column descriptions as `COMMENT`s (#1811)
1212

1313
### Fixed
1414
- Inlining a linked definition merges `logicalTypeOptions` key by key, and allowed values the contract declares in an older form (`logicalTypeOptions.enum`, the `enum` custom property, the `invalidValues` quality rule) win over the definition's `enum`

‎datacontract/export/sql_exporter.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ def _to_sql_table(
158158
col += " primary key"
159159
if server_type == "databricks" and prop.description is not None:
160160
col += f' COMMENT "{_escape(prop.description)}"'
161-
if server_type in ("snowflake", "mysql") and prop.description is not None:
161+
if server_type == "snowflake" and prop.description is not None:
162162
col += f" COMMENT '{_escape_single_quoted(prop.description)}'"
163+
if server_type == "mysql" and prop.description is not None:
164+
col += f" COMMENT '{_escape_single_quoted(_truncate_mysql_comment(prop.description, 1024))}'"
163165
if current_line < total_lines:
164166
col += ","
165167
col += "\n"
@@ -185,8 +187,10 @@ def _to_sql_table(
185187
result += f"\nCOMMENT '{_escape_single_quoted(model.description)}'"
186188
elif server_type == "databricks" and model.description is not None:
187189
result += f' COMMENT "{_escape(model.description)}"'
188-
elif server_type in ("snowflake", "mysql") and model.description is not None:
190+
elif server_type == "snowflake" and model.description is not None:
189191
result += f" COMMENT='{_escape_single_quoted(model.description)}'"
192+
elif server_type == "mysql" and model.description is not None:
193+
result += f" COMMENT='{_escape_single_quoted(_truncate_mysql_comment(model.description, 2048))}'"
190194
result += ";\n"
191195
return result
192196

@@ -280,6 +284,14 @@ def _escape(text: str | None) -> str | None:
280284
return text.replace('"', '\\"')
281285

282286

287+
def _truncate_mysql_comment(comment: str, max_chars: int) -> str:
288+
"""MySQL in strict mode rejects the whole CREATE TABLE when a comment exceeds its limit."""
289+
if len(comment) <= max_chars:
290+
return comment
291+
kept = max_chars - len(f"... ({len(comment)} chars omitted)")
292+
return f"{comment[:kept]}... ({len(comment) - kept} chars omitted)"
293+
294+
283295
def _escape_single_quoted(text: str | None) -> str | None:
284296
if text is None:
285297
return None

‎tests/test_export_sql.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ def test_to_sql_ddl_mysql_comments():
105105
assert actual == expected
106106

107107

108+
def test_to_sql_ddl_mysql_truncates_long_comments():
109+
actual = DataContract(
110+
data_contract_str=f"""
111+
apiVersion: v3.1.0
112+
kind: DataContract
113+
id: mysql-long-comments
114+
name: MySQL Long Comments
115+
version: 1.0.0
116+
status: active
117+
schema:
118+
- name: orders
119+
description: {"t" * 3000}
120+
properties:
121+
- name: order_id
122+
logicalType: integer
123+
description: {"c" * 2000}
124+
"""
125+
).export("sql", sql_server_type="mysql")
126+
column_comment = "c" * 1000 + "... (1000 chars omitted)"
127+
table_comment = "t" * 2024 + "... (976 chars omitted)"
128+
assert f"order_id int COMMENT '{column_comment}'" in actual
129+
assert f") COMMENT='{table_comment}';" in actual
130+
assert len(column_comment) <= 1024
131+
assert len(table_comment) <= 2048
132+
133+
108134
def test_to_sql_ddl_databricks_unity_catalog():
109135
actual = DataContract(data_contract_file="fixtures/databricks-sql/datacontract.yaml").export("sql")
110136
expected = """

0 commit comments

Comments
 (0)