Skip to content

Commit a9fdea8

Browse files
Write MySQL table and column descriptions as COMMENTs in export sql (#1815)
Snowflake already got them. Views are left alone, since MySQL views can't have column comments.
1 parent c3060fa commit a9fdea8

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +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)
1112

1213
### Fixed
1314
- 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ 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 == "snowflake" and prop.description is not None:
161+
if server_type in ("snowflake", "mysql") and prop.description is not None:
162162
col += f" COMMENT '{_escape_single_quoted(prop.description)}'"
163163
if current_line < total_lines:
164164
col += ","
@@ -185,7 +185,7 @@ def _to_sql_table(
185185
result += f"\nCOMMENT '{_escape_single_quoted(model.description)}'"
186186
elif server_type == "databricks" and model.description is not None:
187187
result += f' COMMENT "{_escape(model.description)}"'
188-
elif server_type == "snowflake" and model.description is not None:
188+
elif server_type in ("snowflake", "mysql") and model.description is not None:
189189
result += f" COMMENT='{_escape_single_quoted(model.description)}'"
190190
result += ";\n"
191191
return result

‎tests/test_export_sql.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ def test_to_sql_ddl_snowflake_escapes_quotes_in_comments():
7777
assert actual == expected
7878

7979

80+
def test_to_sql_ddl_mysql_comments():
81+
actual = DataContract(
82+
data_contract_str="""
83+
apiVersion: v3.1.0
84+
kind: DataContract
85+
id: mysql-comments
86+
name: MySQL Comments
87+
version: 1.0.0
88+
status: active
89+
schema:
90+
- name: orders
91+
description: The shop's orders.
92+
properties:
93+
- name: order_id
94+
logicalType: integer
95+
description: Unique identifier of the order.
96+
"""
97+
).export("sql", sql_server_type="mysql")
98+
expected = """
99+
-- Data Contract: mysql-comments
100+
-- SQL Dialect: mysql
101+
CREATE TABLE orders (
102+
order_id int COMMENT 'Unique identifier of the order.'
103+
) COMMENT='The shop''s orders.';
104+
""".strip()
105+
assert actual == expected
106+
107+
80108
def test_to_sql_ddl_databricks_unity_catalog():
81109
actual = DataContract(data_contract_file="fixtures/databricks-sql/datacontract.yaml").export("sql")
82110
expected = """

0 commit comments

Comments
 (0)