Skip to content

Commit 3ebaa83

Browse files
committed
Comment registration support for MSSQL engine
Signed-off-by: Ujfalusi Sándor <ujfalusi.sandor@gmail.com>
1 parent 943b63a commit 3ebaa83

1 file changed

Lines changed: 38 additions & 27 deletions

File tree

sqlmesh/core/engine_adapter/mssql.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -459,63 +459,74 @@ def delete_from(self, table_name: TableName, where: t.Union[str, exp.Expr]) -> N
459459

460460
return super().delete_from(table_name, where)
461461

462-
def _build_create_comment_column_exp(
463-
self, table: exp.Table, column_name: str, column_comment: str, table_kind: str = "TABLE"
462+
def _build_create_comment_table_exp(
463+
self, table: exp.Table, table_comment: str, table_kind: str
464464
) -> exp.Comment | str:
465-
tsql_text = dedent(f"""
466-
SET NOCOUNT ON;
467-
468-
DECLARE @comment sql_variant = {exp.Literal.string(column_comment).sql(dialect=self.dialect) if column_comment is not None else "NULL"};
465+
template = dedent("""
466+
DECLARE @comment sql_variant = {comment};
469467
DECLARE @property_name VARCHAR(128) = 'MS_Description';
470-
DECLARE @schema_name VARCHAR(128) = '{table.db if table.db else "dbo"}';
471-
DECLARE @object_name VARCHAR(128) = '{table.name}';
472-
DECLARE @object_kind VARCHAR(128) = '{table_kind}';
473-
DECLARE @column_name VARCHAR(128) = '{column_name}';
468+
DECLARE @schema_name VARCHAR(128) = '{schema_name}';
469+
DECLARE @object_name VARCHAR(128) = '{object_name}';
470+
DECLARE @object_kind VARCHAR(128) = '{object_kind}';
474471
DECLARE @existing sql_variant;
475472
476-
SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name);
473+
SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, DEFAULT, DEFAULT);
477474
478475
IF @comment IS NULL
479476
BEGIN
480477
IF @existing IS NOT NULL
481-
EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name;
478+
EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name;
482479
END
483480
ELSE
484481
BEGIN
485482
IF @existing IS NULL
486-
EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name;
483+
EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name;
487484
ELSE IF @existing != @comment
488-
EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name;
485+
EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name;
489486
END
490487
""")
488+
tsql_text = template.format(
489+
comment = exp.Literal.string(table_comment).sql(dialect=self.dialect) if table_comment is not None else "NULL",
490+
schema_name = table.db if table.db else "dbo",
491+
object_name = table.name,
492+
object_kind = table_kind,
493+
)
491494
return tsql_text
492495

493-
def _build_create_comment_table_exp(
494-
self, table: exp.Table, table_comment: str, table_kind: str
496+
def _build_create_comment_column_exp(
497+
self, table: exp.Table, column_name: str, column_comment: str, table_kind: str
495498
) -> exp.Comment | str:
496-
tsql_text = dedent(f"""
497-
SET NOCOUNT ON;
498499

499-
DECLARE @comment sql_variant = {exp.Literal.string(table_comment).sql(dialect=self.dialect) if table_comment is not None else "NULL"};
500+
template = dedent("""
501+
DECLARE @comment sql_variant = {comment};
500502
DECLARE @property_name VARCHAR(128) = 'MS_Description';
501-
DECLARE @schema_name VARCHAR(128) = '{table.db if table.db else "dbo"}';
502-
DECLARE @object_name VARCHAR(128) = '{table.name}';
503-
DECLARE @object_kind VARCHAR(128) = '{table_kind}';
503+
DECLARE @schema_name VARCHAR(128) = '{schema_name}';
504+
DECLARE @object_name VARCHAR(128) = '{object_name}';
505+
DECLARE @object_kind VARCHAR(128) = '{object_kind}';
506+
DECLARE @column_name VARCHAR(128) = '{column_name}';
504507
DECLARE @existing sql_variant;
505508
506-
SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, DEFAULT, DEFAULT);
509+
SELECT TOP 1 @existing = CAST(VALUE AS NVARCHAR) FROM fn_listextendedproperty(@property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name);
507510
508511
IF @comment IS NULL
509512
BEGIN
510513
IF @existing IS NOT NULL
511-
EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name;
514+
EXEC sp_dropextendedproperty @property_name, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name;
512515
END
513516
ELSE
514517
BEGIN
515518
IF @existing IS NULL
516-
EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name;
519+
EXEC sp_addextendedproperty @property_name,@comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name;
517520
ELSE IF @existing != @comment
518-
EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name;
521+
EXEC sp_updateextendedproperty @property_name, @comment, 'schema', @schema_name, @object_kind, @object_name, 'column', @column_name;
519522
END
520523
""")
521-
return tsql_text
524+
tsql_text = template.format(
525+
comment = exp.Literal.string(column_comment).sql(dialect=self.dialect) if column_comment is not None else "NULL",
526+
schema_name = table.db if table.db else "dbo",
527+
object_name = table.name,
528+
object_kind = table_kind,
529+
column_name = column_name,
530+
)
531+
532+
return tsql_text

0 commit comments

Comments
 (0)