diff --git a/README.md b/README.md index 0b553b0..ea3bf2c 100644 --- a/README.md +++ b/README.md @@ -139,11 +139,11 @@ select * from renamed 4. Paste the output in to a model, and refactor as required. ## generate_model_yaml ([source](macros/generate_model_yaml.sql)) -This macro generates the YAML for a model, which you can then paste into a +This macro generates the YAML for a list of model(s), which you can then paste into a schema.yml file. ### Arguments: -* `model_name` (required): The model you wish to generate YAML for. +* `model_names` (required): The model(s) you wish to generate YAML for. * `upstream_descriptions` (optional, default=False): Whether you want to include descriptions for identical column names from upstream models. ### Usage: @@ -152,17 +152,17 @@ schema.yml file. ``` {{ codegen.generate_model_yaml( - model_name='customers' + model_names=['customers'] ) }} ``` Alternatively, call the macro as an [operation](https://docs.getdbt.com/docs/using-operations): ``` -$ dbt run-operation generate_model_yaml --args '{"model_name": "customers"}' +$ dbt run-operation generate_model_yaml --args '{"model_names": ["customers"]}' ``` -3. The YAML for a base model will be logged to the command line +3. The YAML for a base model(s) will be logged to the command line ``` version: 2 diff --git a/integration_tests/tests/test_generate_model_struct_yaml.sql b/integration_tests/tests/test_generate_model_struct_yaml.sql index 9670f11..44c1d92 100644 --- a/integration_tests/tests/test_generate_model_struct_yaml.sql +++ b/integration_tests/tests/test_generate_model_struct_yaml.sql @@ -10,7 +10,7 @@ ) %} {% set actual_source_yaml = codegen.generate_model_yaml( - model_name='model_struct' + model_names=['model_struct'] ) %} diff --git a/integration_tests/tests/test_generate_model_yaml.sql b/integration_tests/tests/test_generate_model_yaml.sql index 2f865a8..dbb9747 100644 --- a/integration_tests/tests/test_generate_model_yaml.sql +++ b/integration_tests/tests/test_generate_model_yaml.sql @@ -1,5 +1,5 @@ {% set actual_model_yaml = codegen.generate_model_yaml( - model_name='data__a_relation' + model_names=['data__a_relation'] ) %} diff --git a/integration_tests/tests/test_generate_model_yaml_multiple_models.sql b/integration_tests/tests/test_generate_model_yaml_multiple_models.sql new file mode 100644 index 0000000..843959f --- /dev/null +++ b/integration_tests/tests/test_generate_model_yaml_multiple_models.sql @@ -0,0 +1,30 @@ +{% set actual_model_yaml = codegen.generate_model_yaml( + model_names=['data__a_relation','data__b_relation'] + ) +%} + +{% set expected_model_yaml %} +version: 2 + +models: + - name: data__a_relation + description: "" + columns: + - name: col_a + description: "" + + - name: col_b + description: "" + + - name: data__b_relation + description: "" + columns: + - name: col_a + description: "" + + - name: col_b + description: "" + +{% endset %} + +{{ assert_equal (actual_model_yaml | trim, expected_model_yaml | trim) }} diff --git a/integration_tests/tests/test_generate_model_yaml_upstream_descriptions.sql b/integration_tests/tests/test_generate_model_yaml_upstream_descriptions.sql index 3bc9804..71ecd89 100644 --- a/integration_tests/tests/test_generate_model_yaml_upstream_descriptions.sql +++ b/integration_tests/tests/test_generate_model_yaml_upstream_descriptions.sql @@ -1,5 +1,5 @@ {% set actual_model_yaml = codegen.generate_model_yaml( - model_name='child_model', + model_names=['child_model'], upstream_descriptions=True ) %} diff --git a/macros/generate_model_yaml.sql b/macros/generate_model_yaml.sql index f00f910..c36b01d 100644 --- a/macros/generate_model_yaml.sql +++ b/macros/generate_model_yaml.sql @@ -17,24 +17,31 @@ {% do return(model_yaml) %} {% endmacro %} -{% macro generate_model_yaml(model_name, upstream_descriptions=False) %} +{% macro generate_model_yaml(model_names=[], upstream_descriptions=False) %} -{% set model_yaml=[] %} -{% set column_desc_dict = codegen.build_dict_column_descriptions(model_name) if upstream_descriptions else {} %} + {% set model_yaml=[] %} -{% do model_yaml.append('version: 2') %} -{% do model_yaml.append('') %} -{% do model_yaml.append('models:') %} -{% do model_yaml.append(' - name: ' ~ model_name | lower) %} -{% do model_yaml.append(' description: ""') %} -{% do model_yaml.append(' columns:') %} - -{% set relation=ref(model_name) %} -{%- set columns = adapter.get_columns_in_relation(relation) -%} + {% do model_yaml.append('version: 2') %} + {% do model_yaml.append('') %} + {% do model_yaml.append('models:') %} -{% for column in columns %} - {% set model_yaml = codegen.generate_column_yaml(column, model_yaml, column_desc_dict) %} -{% endfor %} + {% if model_names is string %} + {{ exceptions.raise_compiler_error("The `model_names` argument must always be a list, even if there is only one model.") }} + {% else %} + {% for model in model_names %} + {% do model_yaml.append(' - name: ' ~ model | lower) %} + {% do model_yaml.append(' description: ""') %} + {% do model_yaml.append(' columns:') %} + + {% set relation=ref(model) %} + {%- set columns = adapter.get_columns_in_relation(relation) -%} + {% set column_desc_dict = codegen.build_dict_column_descriptions(model) if upstream_descriptions else {} %} + + {% for column in columns %} + {% set model_yaml = codegen.generate_column_yaml(column, model_yaml, column_desc_dict) %} + {% endfor %} + {% endfor %} + {% endif %} {% if execute %}