Skip to content

Commit

Permalink
fix schema from baseline enforce types (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
IDoneShaveIt authored Jan 5, 2023
1 parent fa14ae7 commit 213cba6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
21 changes: 16 additions & 5 deletions integration_tests/macros/e2e_tests/tests_validation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,27 @@
} %}
{%- set max_bucket_end = elementary.quote(elementary.get_run_started_at().strftime("%Y-%m-%d 00:00:00")) %}
{% set alerts_relation = ref('alerts_schema_changes') %}
{% set schema_changes_alerts %}
{% set failed_schema_changes_alerts %}
select test_short_name, column_name, sub_type
from {{ alerts_relation }}
where detected_at >= {{ max_bucket_end }} and column_name is not NULL
where detected_at >= {{ max_bucket_end }} and status = 'fail'
group by 1,2,3
{% endset %}
{% set alert_rows = run_query(schema_changes_alerts) %}
{% set error_schema_changes_alerts %}
select test_short_name, column_name, sub_type
from {{ alerts_relation }}
where detected_at >= {{ max_bucket_end }} and status = 'error'
group by 1,2,3
{% endset %}
{% set error_alert_rows = run_query(error_schema_changes_alerts) %}
{# We should have one error test from schema_changes_from_baseline with enforce_types true #}
{% if error_alert_rows | length != 1 %}
{% do elementary.edr_log("FAILED: for schema_changes_from_baseline with enforce_types true - no error eccured") %}
{{ return(1) }}
{% endif %}
{% set failure_alert_rows = run_query(failed_schema_changes_alerts) %}
{% set found_schema_changes = {} %}
{% for row in alert_rows %}
{% for row in failure_alert_rows %}
{% set test_short_name = row[0] | lower %}
{% set column_name = row[1] | lower %}
{% set alert = row[2] | lower %}
Expand All @@ -299,7 +311,6 @@
{{ return(1) }}
{% endif %}
{% do found_schema_changes.update({(test_short_name, column_name): alert}) %}

{% endfor %}
{% if found_schema_changes %}
{%- set missing_changes = [] %}
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ models:
- elementary.schema_changes_from_baseline:
fail_on_added: true
tags: [ "schema_changes_from_baseline" ]
- elementary.schema_changes_from_baseline:
tags: [ "schema_changes_from_baseline" ]
enforce_types: true

- name: stats_players
columns:
Expand All @@ -245,6 +248,9 @@ models:
tags: [ "schema_changes" ]
- elementary.schema_changes_from_baseline:
tags: [ "schema_changes_from_baseline" ]
- elementary.schema_changes_from_baseline:
tags: [ "schema_changes_from_baseline" ]
enforce_types: true

- name: stats_team
tests:
Expand Down
12 changes: 7 additions & 5 deletions macros/edr/tests/test_utils/get_model_baseline_columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
{% endif %}
{% do baseline.append(column_info) %}
{% endfor %}

{% if enforce_types %}
{% do exceptions.raise_compiler_error("Data type not defined for columns `{}`".format(columns_without_types)) %}
{% else %}
{% do elementary.edr_log("Warning - missing data types for columns: " ~ columns_without_types) %}

{% if columns_without_types %}
{% if enforce_types %}
{% do exceptions.raise_compiler_error("Data type not defined for columns `{}` on model `{}` for schema change from baseline test".format(columns_without_types, model)) %}
{% else %}
{% do elementary.edr_log("Warning - missing data types for columns: " ~ columns_without_types) %}
{% endif %}
{% endif %}

{% do return(baseline) %}
Expand Down

0 comments on commit 213cba6

Please sign in to comment.