Skip to content

Commit 8171bfd

Browse files
authored
Merge pull request #1 from PeterDKay/task/sqlalchemy-dialect-updates-for-comments
Updates to dialect to handle comments in alembic
2 parents 6f83144 + 532a4cf commit 8171bfd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/databricks/sqlalchemy/dialect/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class DatabricksDialect(default.DefaultDialect):
8080
supports_multivalues_insert: bool = True
8181
supports_native_decimal: bool = True
8282
supports_sane_rowcount: bool = False
83+
supports_comments: bool = True
8384

8485
@classmethod
8586
def dbapi(cls):
@@ -174,6 +175,7 @@ def get_columns(self, connection, table_name, schema=None, **kwargs):
174175
"nullable": bool(col.NULLABLE),
175176
"default": col.COLUMN_DEF,
176177
"autoincrement": False if col.IS_AUTO_INCREMENT == "NO" else True,
178+
'comment': col.REMARKS,
177179
}
178180
columns.append(this_column)
179181

src/databricks/sqlalchemy/dialect/base.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from sqlalchemy.sql import compiler
2+
from sqlalchemy.sql import compiler, sqltypes, ColumnElement
33

44

55
class DatabricksIdentifierPreparer(compiler.IdentifierPreparer):
@@ -15,3 +15,53 @@ def __init__(self, dialect):
1515
class DatabricksDDLCompiler(compiler.DDLCompiler):
1616
def post_create_table(self, table):
1717
return " USING DELTA"
18+
19+
def visit_set_column_comment(self, create, **kw):
20+
"""
21+
Example syntax for adding column comment:
22+
"ALTER TABLE schema.table_name CHANGE COLUMN COLUMN_NAME COMMENT 'Comment to be added to column';"
23+
24+
"""
25+
return """ALTER TABLE {0} CHANGE COLUMN {1} COMMENT {2}""".format(
26+
'testing.alembic_test1',
27+
self._format_table_from_column(
28+
create, use_schema=True
29+
),
30+
self.preparer.format_column(
31+
create.element, use_table=False
32+
),
33+
self.sql_compiler.render_literal_value(
34+
create.element.comment, sqltypes.String()
35+
),
36+
)
37+
38+
def visit_drop_column_comment(self, drop, **kw):
39+
"""
40+
Example syntax for dropping column comment:
41+
"ALTER TABLE schema.table_name CHANGE COLUMN COLUMN_NAME COMMENT '';"
42+
43+
Note: There is no syntactical 'DROP' statement in this case, the comment must be replaced with an empty string
44+
"""
45+
return "ALTER TABLE {0} CHANGE COLUMN {1} COMMENT '';".format(
46+
self._format_table_from_column(
47+
drop, use_schema=True
48+
),
49+
self.preparer.format_column(
50+
drop.element, use_table=False
51+
)
52+
)
53+
54+
def _format_table_from_column(self, column_object, use_schema=False):
55+
"""
56+
Prepare a quoted table name from the column object (including schema if specified)
57+
"""
58+
schema_table_column = self.preparer.format_column(
59+
column_object.element, use_table=True, use_schema=True
60+
)
61+
62+
name = schema_table_column.split(".")[1]
63+
64+
if use_schema:
65+
name = schema_table_column.split(".")[0] + '.' + name
66+
67+
return name

0 commit comments

Comments
 (0)