|
10 | 10 | alter table {{ relation }} {{ on_cluster_clause(relation) }} modify comment '{{ comment }}' |
11 | 11 | {% endmacro %} |
12 | 12 |
|
13 | | -{% macro clickhouse__persist_docs(relation, model, for_relation, for_columns) %} |
14 | | - {%- set alter_comments = [] %} |
15 | | - |
| 13 | +{% macro clickhouse__persist_docs(relation, model, for_relation, for_columns) -%} |
| 14 | + {# Persist table comment if enabled and description provided #} |
16 | 15 | {%- if for_relation and config.persist_relation_docs() and model.description -%} |
17 | | - {% set escaped_comment = clickhouse_escape_comment(model.description) %} |
18 | | - {% do alter_comments.append("modify comment {comment}".format(comment=escaped_comment)) %} |
| 16 | + {{ _persist_table_comment(relation, model.description) }} |
19 | 17 | {%- endif -%} |
20 | 18 |
|
| 19 | + {#- Persist column comments if enabled and columns defined -#} |
21 | 20 | {%- if for_columns and config.persist_column_docs() and model.columns -%} |
22 | | - {% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %} |
23 | | - {% for column_name in model.columns if (column_name in existing_columns) %} |
24 | | - {%- set comment = model.columns[column_name]['description'] -%} |
25 | | - {%- if comment %} |
26 | | - {% set escaped_comment = clickhouse_escape_comment(comment) %} |
27 | | - {% do alter_comments.append("comment column `{column_name}` {comment}".format(column_name=column_name, comment=escaped_comment)) %} |
28 | | - {%- endif %} |
29 | | - {%- endfor -%} |
| 21 | + {{ _persist_column_comments(relation, model.columns) }} |
30 | 22 | {%- endif -%} |
| 23 | +{%- endmacro %} |
| 24 | + |
| 25 | +{#- Helper macro: persist the table comment for a ClickHouse relation. -#} |
| 26 | +{% macro _persist_table_comment(relation, description) -%} |
| 27 | + {#- Escape the description to be safe for ClickHouse #} |
| 28 | + {%- set escaped_comment = clickhouse_escape_comment(description) -%} |
| 29 | + {#- Build and run the ALTER TABLE ... MODIFY COMMENT statement -#} |
| 30 | + {%- set sql = "modify comment {comment}".format(comment=escaped_comment) -%} |
| 31 | + {{ run_query(one_alter_relation(relation, sql)) }} |
| 32 | +{%- endmacro %} |
31 | 33 |
|
32 | | - {%- if alter_comments | length > 0 -%} |
33 | | - {% do run_query(one_alter_relation(relation, alter_comments|join(', '))) %} |
| 34 | +{#- Helper macro: persist comments for multiple columns on a ClickHouse table. |
| 35 | + relation: target table relation |
| 36 | + columns: dict mapping column names to metadata (including 'description') -#} |
| 37 | +{% macro _persist_column_comments(relation, columns) -%} |
| 38 | + {#- Gather existing columns in the relation to avoid altering non-existent ones -#} |
| 39 | + {%- set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list -%} |
| 40 | + {#- Collect ALTER statements for each column with a description -#} |
| 41 | + {%- set alterations = [] -%} |
| 42 | + {%- for column_name, info in columns.items() if info.description and column_name in existing_columns -%} |
| 43 | + {%- set escaped_comment = clickhouse_escape_comment(info.description) -%} |
| 44 | + {%- do alterations.append("\ncomment column `{column_name}` {comment}".format(column_name=column_name, comment=escaped_comment)) -%} |
| 45 | + {%- endfor -%} |
| 46 | + {#- Execute a single ALTER TABLE statement for all column comments -#} |
| 47 | + {%- if alterations -%} |
| 48 | + {{ run_query(one_alter_relation(relation, alterations | join(", "))) }} |
34 | 49 | {%- endif -%} |
35 | | -{% endmacro %} |
| 50 | +{%- endmacro %} |
| 51 | + |
36 | 52 |
|
37 | 53 | {# |
38 | 54 | By using dollar-quoting like this, users can embed anything they want into their comments |
|
0 commit comments