Repository: TobikoData/sqlmesh (python)

Issue:
Comments on `MODEL` properties are removed by `sqlmesh format`
For example:

```sql
MODEL(
  name foo.bar, -- my model is awesome
  kind VIEW,
);
SELECT * FROM foo_bar
```

Will produce

```sql
MODEL(
  name foo.bar,
  kind VIEW,
);
SELECT
  *
FROM foo_bar
```


Pull request description:
fix: add model prop comments closes #2533

Patch:
diff --git a/sqlmesh/core/dialect.py b/sqlmesh/core/dialect.py
index 4006e8f1..42f29b41 100644
--- a/sqlmesh/core/dialect.py
+++ b/sqlmesh/core/dialect.py
@@ -501,7 +501,7 @@ def _create_parser(parser_type: t.Type[exp.Expression], table_keys: t.List[str])
 
             expressions.append(self.expression(exp.Property, this=key, value=value))
 
-            if not self._match(TokenType.COMMA):
+            if not self._match(TokenType.COMMA, expression=expressions[-1]):
                 break
 
         return self.expression(parser_type, expressions=expressions)
@@ -516,17 +516,26 @@ PARSERS = {
 }
 
 
+def _props_sql(self: Generator, expressions: t.List[exp.Expression]) -> str:
+    props = []
+    size = len(expressions)
+
+    for i, prop in enumerate(expressions):
+        sql = self.indent(f"{prop.name} {self.sql(prop, 'value')}")
+
+        if i < size - 1:
+            sql += ","
+        props.append(self.maybe_comment(sql, expression=prop))
+
+    return "\n".join(props)
+
+
 def _sqlmesh_ddl_sql(self: Generator, expression: Model | Audit | Metric, name: str) -> str:
-    props = ",\n".join(
-        self.indent(f"{prop.name} {self.sql(prop, 'value')}") for prop in expression.expressions
-    )
-    return "\n".join([f"{name} (", props, ")"])
+    return "\n".join([f"{name} (", _props_sql(self, expression.expressions), ")"])
 
 
 def _model_kind_sql(self: Generator, expression: ModelKind) -> str:
-    props = ",\n".join(
-        self.indent(f"{prop.this} {self.sql(prop, 'value')}") for prop in expression.expressions
-    )
+    props = _props_sql(self, expression.expressions)
     if props:
         return "\n".join([f"{expression.this} (", props, ")"])
     return expression.name.upper()


Test patch:
diff --git a/tests/core/test_dialect.py b/tests/core/test_dialect.py
index 4bbfb22d..1dd5cb32 100644
--- a/tests/core/test_dialect.py
+++ b/tests/core/test_dialect.py
@@ -20,12 +20,12 @@ def test_format_model_expressions():
         parse(
             """
     MODEL(
-    name a.b,
-    kind full,
+    name a.b, -- a
+    kind full, -- b
     references (
      a,
      (b, c) as d,
-     ),
+     ),  -- c
      audits [
     not_null(columns=[
       foo_id,
@@ -83,9 +83,9 @@ def test_format_model_expressions():
     assert (
         x
         == """MODEL (
-  name a.b,
-  kind FULL,
-  references (a, (b, c) AS d),
+  name a.b, /* a */
+  kind FULL, /* b */
+  references (a, (b, c) AS d), /* c */
   audits ARRAY(
     NOT_NULL(
       columns = ARRAY(
@@ -318,13 +318,21 @@ def test_seed():
         """
         MODEL (
             kind SEED (
-                path '..\..\..\data\data.csv',
+                path '..\..\..\data\data.csv', -- c
             ),
         );
     """
     )
     assert len(expressions) == 1
     assert "../../../data/data.csv" in expressions[0].sql()
+    assert (
+        format_model_expressions(expressions)
+        == """MODEL (
+  kind SEED (
+    path '../../../data/data.csv' /* c */
+  )
+)"""
+    )
 
 
 def test_select_from_values_for_batch_range_json():