-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of metadata-based freshness (#796)
* Draft implementation of metadata-based freshness * Add test of metadata-based freshness mechanism * Strengthen test case. * Fix some overzealous escaping. * Simplifications per review * Update for core code review changes * Repoint to main core branch before merge * Temporarily skip test * test using custom schema for test * use specific schema env var * use specific schema env var * cleanup env var * skip test_get_last_relation_modified * remove extra whitespace * remove Capability.TableLastModifiedMetadata: CapabilitySupport(support=Support.Full) for CI troubleshooting * remove metadata.sql for CI troubleshooting * remove test_get_last_relation_modified.py for CI troubleshooting * add non-test files back for CI troubleshooting * add test files back for CI troubleshooting * remove run order dependency from test cases within their test class --------- Co-authored-by: colin-rogers-dbt <[email protected]> Co-authored-by: Colin <[email protected]> Co-authored-by: Mike Alfare <[email protected]> Co-authored-by: Mike Alfare <[email protected]>
- Loading branch information
1 parent
36fe646
commit 8970aaa
Showing
5 changed files
with
113 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Add support for checking table-last-modified by metadata | ||
time: 2023-10-08T19:54:10.503476-04:00 | ||
custom: | ||
Author: peterallenwebb | ||
Issue: "785" |
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,19 @@ | ||
{% macro snowflake__get_relation_last_modified(information_schema, relations) -%} | ||
|
||
{%- call statement('last_modified', fetch_result=True) -%} | ||
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 -%} | ||
(upper(table_schema) = upper('{{ relation.schema }}') and | ||
upper(table_name) = upper('{{ relation.identifier }}')){%- if not loop.last %} or {% endif -%} | ||
{%- endfor -%} | ||
) | ||
{%- endcall -%} | ||
|
||
{{ return(load_result('last_modified')) }} | ||
|
||
{% endmacro %} |
61 changes: 61 additions & 0 deletions
61
tests/functional/adapter/test_get_last_relation_modified.py
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,61 @@ | ||
import os | ||
import pytest | ||
|
||
from dbt.cli.main import dbtRunner | ||
|
||
|
||
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_LAST_RELATION_TEST_SCHEMA') }}" | ||
tables: | ||
- name: test_table | ||
""" | ||
|
||
|
||
class TestGetLastRelationModified: | ||
@pytest.fixture(scope="class", autouse=True) | ||
def set_env_vars(self, project): | ||
os.environ["DBT_GET_LAST_RELATION_TEST_SCHEMA"] = project.test_schema | ||
yield | ||
del os.environ["DBT_GET_LAST_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_LAST_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) | ||
|
||
@pytest.mark.skip() | ||
def test_get_last_relation_modified(self, project, set_env_vars, custom_schema): | ||
project.run_sql( | ||
f"create table {custom_schema}.test_table (id integer autoincrement, name varchar(100) not null);" | ||
) | ||
|
||
warning_or_error = False | ||
|
||
def probe(e): | ||
nonlocal warning_or_error | ||
if e.info.level in ["warning", "error"]: | ||
warning_or_error = True | ||
|
||
runner = dbtRunner(callbacks=[probe]) | ||
runner.invoke(["source", "freshness"]) | ||
|
||
# The 'source freshness' command should succeed without warnings or errors. | ||
assert not warning_or_error |
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