-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing metadata freshness checks (#481)
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{% macro databricks__get_relation_last_modified(information_schema, relations) -%} | ||
|
||
{%- call statement('last_modified', fetch_result=True) -%} | ||
{% if information_schema.is_hive_metastore %} | ||
{%- for relation in relations -%} | ||
select '{{ relation.schema }}' as schema, | ||
'{{ relation.identifier }}' as identifier, | ||
max(timestamp) as last_modified, | ||
{{ current_timestamp() }} as snapshotted_at | ||
from (describe history {{ relation.schema }}.{{ relation.identifier }}) | ||
{% if not loop.last %} | ||
union all | ||
{% endif %} | ||
{%- endfor -%} | ||
{% else %} | ||
select table_schema as schema, | ||
table_name as identifier, | ||
last_altered as last_modified, | ||
{{ current_timestamp() }} as snapshotted_at | ||
from {{ information_schema }}.tables | ||
where ( | ||
{%- for relation in relations -%} | ||
(table_schema = '{{ relation.schema }}' and | ||
table_name = '{{ relation.identifier }}'){%- if not loop.last %} or {% endif -%} | ||
{%- endfor -%} | ||
) | ||
{% endif %} | ||
{%- endcall -%} | ||
|
||
{{ return(load_result('last_modified')) }} | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
import pytest | ||
|
||
from dbt.tests.util import get_artifact, run_dbt | ||
|
||
freshness_via_metadata_schema_yml = """ | ||
version: 2 | ||
sources: | ||
- name: test_source | ||
freshness: | ||
warn_after: {count: 10, period: hour} | ||
error_after: {count: 1, period: day} | ||
schema: "{{ env_var('DBT_GET_RELATION_TEST_SCHEMA') }}" | ||
tables: | ||
- name: test_table | ||
""" | ||
|
||
|
||
class TestGetRelationLastModified: | ||
@pytest.fixture(scope="class", autouse=True) | ||
def set_env_vars(self, project): | ||
os.environ["DBT_GET_RELATION_TEST_SCHEMA"] = project.test_schema | ||
yield | ||
del os.environ["DBT_GET_RELATION_TEST_SCHEMA"] | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"schema.yml": freshness_via_metadata_schema_yml} | ||
|
||
@pytest.fixture(scope="class") | ||
def custom_schema(self, project, set_env_vars): | ||
with project.adapter.connection_named("__test"): | ||
relation = project.adapter.Relation.create( | ||
database=project.database, schema=os.environ["DBT_GET_RELATION_TEST_SCHEMA"] | ||
) | ||
project.adapter.drop_schema(relation) | ||
project.adapter.create_schema(relation) | ||
|
||
yield relation.schema | ||
|
||
with project.adapter.connection_named("__test"): | ||
project.adapter.drop_schema(relation) | ||
|
||
def test_get_relation_last_modified(self, project, custom_schema): | ||
project.run_sql( | ||
f"create table {custom_schema}.test_table (id integer, name varchar(100) not null);" | ||
) | ||
|
||
run_dbt(["source", "freshness"]) | ||
|
||
sources = get_artifact("target/sources.json") | ||
|
||
assert sources["results"][0]["status"] == "pass" |