Skip to content

Commit

Permalink
refactor: add sql_keywords filter for custom lowercase_columns macro
Browse files Browse the repository at this point in the history
 - this macro now safely quotes safewords in use throughout the repo
  • Loading branch information
lawrenceadams committed Oct 6, 2024
1 parent da8eec5 commit 2df584a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion macros/lowercase_columns.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{% macro lowercase_columns(column_names) %}
{# List of SQL Keywords in use by dbt-synthea #}
{% set sql_keywords = ["start", "stop", "type", "system", "date", "first", "last", "value", "name"] %}
{% for column_name in column_names %}
"{{ column_name }}" AS {{ column_name | lower }}
{% set lowercase_column = column_name | lower %}
{# If a keyword - quote the output column name so it can be safely used in models #}
{% if column_name | lower in sql_keywords %}
"{{ column_name }}" AS "{{ lowercase_column }}"
{% else %}
"{{ column_name }}" AS {{ lowercase_column }}
{% endif %}
{% if not loop.last %},{% endif %}
{% endfor %}
{% endmacro %}

0 comments on commit 2df584a

Please sign in to comment.