diff --git a/README.md b/README.md index e887c30..a70a363 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,6 @@ Call this macro from another macro or dbt model: ## print_profile ([source](macros/print_profile.sql)) -❗ **This macro does not work in dbt Cloud. The profile doesn't display in the cloud console log because the underlying [print_table()](https://agate.readthedocs.io/en/1.6.1/api/table.html#agate.Table.print_table) method is disabled.** - This macro prints a relation profile as a Markdown table to `stdout`. ### Arguments @@ -337,8 +335,6 @@ This what the profile looks like on the dbt docs site: ## print_profile_docs ([source](macros/print_profile_docs.sql)) -❗ **This macro does not work in dbt Cloud. The profile doesn't display in the cloud console log because the underlying [print_table()](https://agate.readthedocs.io/en/1.6.1/api/table.html#agate.Table.print_table) method is disabled.** - This macro prints a relation profile as a Markdown table wrapped in a Jinja `docs` macro to `stdout`. ### Arguments diff --git a/macros/print_profile.sql b/macros/print_profile.sql index 9580c37..b7fa448 100644 --- a/macros/print_profile.sql +++ b/macros/print_profile.sql @@ -3,7 +3,7 @@ {%- set results = dbt_profiler.get_profile_table(relation=relation, relation_name=relation_name, schema=schema, database=database, exclude_measures=exclude_measures, include_columns=include_columns, exclude_columns=exclude_columns, where_clause=where_clause) -%} {% if execute %} - {% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %} + {{ dbt_profiler.print_table(results, max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) }} {% endif %} {% endmacro %} \ No newline at end of file diff --git a/macros/print_profile_docs.sql b/macros/print_profile_docs.sql index 098f4f8..2fbc17b 100644 --- a/macros/print_profile_docs.sql +++ b/macros/print_profile_docs.sql @@ -11,54 +11,10 @@ {%- set startdocs = '{% docs ' ~ docs_name ~ ' %}' -%} {%- set enddocs = '{% enddocs %}' -%} - {# Check if macro is called in dbt Cloud? #} - {%- if flags.WHICH == 'rpc' -%} - {%- set is_dbt_cloud = true -%} - {%- else -%} - {%- set is_dbt_cloud = false -%} - {%- endif -%} + {{ print(startdocs) }} + {{ dbt_profiler.print_table(results, max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) }} + {{ print(enddocs) }} - {% if not is_dbt_cloud %} - - {{ print(startdocs) }} - {% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %} - {{ print(enddocs) }} - - {% else %} - - {%- set profile_docs=[] -%} - {% do profile_docs.append(startdocs) -%} - {% do profile_docs.append('') %} - - {# Get header from column names #} - {%- set headers = results.column_names -%} - {%- set header = [] -%} - {%- set horizontal_line = [] -%} - - {% for i in range(0,headers|length) %} - {% do header.append(headers[i]) %} - {% do horizontal_line.append('---') %} - {% endfor %} - {% do profile_docs.append('| ' ~ header|join(' | ') ~ ' |') %} - {% do profile_docs.append('| ' ~ horizontal_line|join(' | ') ~ ' |') %} - - {# Get row values #} - {% for row in results.rows %} - {%- set list_row = [''] -%} - {% for val in row.values() %} - {% do list_row.append(val) %} - {% endfor %} - {% do profile_docs.append(list_row|join(' | ') ~ ' |') %} - {% endfor %} - {% do profile_docs.append('') %} - {% do profile_docs.append(enddocs) %} - - {# Join profile docs #} - {%- set joined = profile_docs | join ('\n') -%} - {{ log(joined, info=True) }} - {% do return(joined) %} - - {% endif %} {% endif %} diff --git a/macros/print_table.sql b/macros/print_table.sql new file mode 100644 index 0000000..6ca261f --- /dev/null +++ b/macros/print_table.sql @@ -0,0 +1,31 @@ +{% macro print_table(table, max_rows=none, max_columns=13, max_column_width=30, max_precision=none) %} + +{# Check if macro is called in dbt Cloud #} +{%- set is_dbt_cloud = flags.WHICH == 'rpc' -%} + +{%- if not is_dbt_cloud -%} + {% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %} +{% else %} + {%- set table_printout_lines = [] -%} + + {# Get header from column names #} + {%- set headers = results.column_names | list -%} + {%- set horizontal_lines = ['---'] * headers | length -%} + {% do table_printout_lines.append('| ' ~ headers | join(' | ') ~ ' |') %} + {% do table_printout_lines.append('| ' ~ horizontal_lines | join(' | ') ~ ' |') %} + + {# Get row values #} + {% for row in results.rows %} + {%- set list_row = [''] -%} + {% for val in row.values() %} + {% do list_row.append(val) %} + {% endfor %} + {% do table_printout_lines.append(list_row | join(' | ') ~ ' |') %} + {% endfor %} + + {%- set table_printout = table_printout_lines | join ('\n') -%} + {{ print(table_printout) }} +{% endif %} + + +{% endmacro %} \ No newline at end of file