Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9656f95

Browse files
authored
Merge pull request #6 from sede-open/task/add-column-comment-code
First test with new compiler function
2 parents 72231d6 + a301b16 commit 9656f95

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/databricks/sqlalchemy/dialect/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ def get_indexes(self, connection, table_name, schema=None, **kw):
245245
return []
246246

247247
def get_table_names(self, connection, schema=None, **kwargs):
248+
# TODO: Change this to ignore views!!!!!!!
248249
TABLE_NAME = 1
249250
catalog = "`" + self.catalog + "`"
250251
schema = ("`" + schema + "`") or ("`" + self.schema + "`")

src/databricks/sqlalchemy/dialect/base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
2+
from alembic.ddl.base import ColumnComment
23
from sqlalchemy import util, exc
34
from sqlalchemy.sql import compiler, sqltypes, ColumnElement
5+
from sqlalchemy.ext.compiler import compiles
46

57

68
class DatabricksIdentifierPreparer(compiler.IdentifierPreparer):
@@ -123,3 +125,25 @@ def visit_drop_table(self, drop, **kw):
123125
text = "\nDROP TABLE IF EXISTS "
124126

125127
return text + self.preparer.format_table(drop.element)
128+
129+
130+
@compiles(ColumnComment, "databricks")
131+
def visit_column_comment(
132+
element: ColumnComment, compiler: DatabricksDDLCompiler, **kw
133+
) -> str:
134+
ddl = "ALTER TABLE `{schema}`.{table_name} ALTER COLUMN {column_name} COMMENT {comment}"
135+
comment = (
136+
compiler.sql_compiler.render_literal_value(
137+
element.comment, sqltypes.String()
138+
)
139+
if element.comment is not None
140+
else "NULL"
141+
)
142+
143+
return ddl.format(
144+
schema=element.schema,
145+
table_name=element.table_name,
146+
column_name=element.column_name,
147+
comment=comment,
148+
)
149+

0 commit comments

Comments
 (0)