-
Notifications
You must be signed in to change notification settings - Fork 122
feat: opt-in config to filter artifacts to current project only #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd90eea
fix: filter dbt artifacts to upload only current project
cedric-orange 5061952
feat: make project-scoped artifact filtering opt-in via config var
haritamar 9d0419e
test: add integration tests for upload_only_current_project_artifacts
haritamar 42102a9
refactor: rename filter_to_current_project to filter_to_current_proje…
haritamar 5609d64
fix: use post_hook full-replace in project filtering tests
haritamar 88e2916
fix: use DELETE + on-run-end diff for project filtering tests
haritamar 4e7fbb4
fix: remove unused tmp_path parameter from test
haritamar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
integration_tests/tests/test_dbt_artifacts/test_project_filtering.py
This file contains hidden or 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,107 @@ | ||
| """ | ||
| Integration tests for the upload_only_current_project_artifacts config var. | ||
|
|
||
| When enabled, artifact uploads should only include resources from the current | ||
| project (by package_name), excluding artifacts from dependency packages. | ||
| When disabled (default), all artifacts including dependencies should be uploaded. | ||
| """ | ||
|
|
||
| import uuid | ||
|
|
||
| from dbt_project import DbtProject | ||
|
|
||
| TEST_MODEL = "one" | ||
|
|
||
|
|
||
| def test_default_includes_dependency_artifacts(dbt_project: DbtProject): | ||
| """ | ||
| By default (upload_only_current_project_artifacts=false), artifacts from | ||
| dependency packages (like 'elementary') should be present in dbt_models. | ||
| """ | ||
| dbt_project.dbt_runner.vars["disable_dbt_artifacts_autoupload"] = False | ||
| dbt_project.dbt_runner.vars["cache_artifacts"] = False | ||
|
|
||
| dbt_project.dbt_runner.run(select=TEST_MODEL) | ||
|
|
||
| all_models = dbt_project.read_table("dbt_models", raise_if_empty=True) | ||
| package_names = {row["package_name"] for row in all_models} | ||
|
|
||
| assert "elementary" in package_names, ( | ||
| "Expected 'elementary' package artifacts to be present by default, " | ||
| f"but only found packages: {package_names}" | ||
| ) | ||
| assert "elementary_tests" in package_names, ( | ||
| "Expected 'elementary_tests' package artifacts to be present, " | ||
| f"but only found packages: {package_names}" | ||
| ) | ||
|
|
||
|
|
||
| def test_filtering_excludes_dependency_artifacts(dbt_project: DbtProject): | ||
| """ | ||
| When upload_only_current_project_artifacts=true, only artifacts from the | ||
| current project should be uploaded — dependency packages like 'elementary' | ||
| should be excluded. | ||
|
|
||
| We first clear the dbt_models table, then run with filtering enabled via | ||
| the on-run-end hook. The diff upload sees an empty table and inserts all | ||
| (filtered) artifacts, so only current-project rows end up in the table. | ||
| """ | ||
| # Clear existing rows so the diff upload will insert fresh filtered data. | ||
| dbt_project.run_query("DELETE FROM {{ ref('dbt_models') }} WHERE 1=1") | ||
|
|
||
| dbt_project.dbt_runner.vars["disable_dbt_artifacts_autoupload"] = False | ||
| dbt_project.dbt_runner.vars["upload_only_current_project_artifacts"] = True | ||
|
|
||
| dbt_project.dbt_runner.run(select=TEST_MODEL) | ||
|
|
||
| all_models = dbt_project.read_table("dbt_models", raise_if_empty=True) | ||
| package_names = {row["package_name"] for row in all_models} | ||
|
|
||
| assert package_names == {"elementary_tests"}, ( | ||
| "Expected only 'elementary_tests' artifacts when filtering is enabled, " | ||
| f"but found packages: {package_names}" | ||
| ) | ||
|
|
||
|
|
||
| def test_filtering_applies_to_tests(dbt_project: DbtProject): | ||
| """ | ||
| When upload_only_current_project_artifacts=true, tests from the current | ||
| project should still be uploaded. | ||
| """ | ||
| unique_id = str(uuid.uuid4()).replace("-", "_") | ||
| model_name = f"filter_test_model_{unique_id}" | ||
| model_sql = "select 1 as col" | ||
| schema_yaml = { | ||
| "version": 2, | ||
| "models": [ | ||
| { | ||
| "name": model_name, | ||
| "columns": [{"name": "col", "tests": ["unique"]}], | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| with dbt_project.write_yaml( | ||
| schema_yaml, name=f"schema_filter_test_{unique_id}.yml" | ||
| ): | ||
| dbt_model_path = dbt_project.models_dir_path / "tmp" / f"{model_name}.sql" | ||
| dbt_model_path.parent.mkdir(parents=True, exist_ok=True) | ||
| dbt_model_path.write_text(model_sql) | ||
| try: | ||
| # Clear existing rows so the diff upload inserts fresh filtered data. | ||
| dbt_project.run_query("DELETE FROM {{ ref('dbt_tests') }} WHERE 1=1") | ||
|
|
||
| dbt_project.dbt_runner.vars["disable_dbt_artifacts_autoupload"] = False | ||
| dbt_project.dbt_runner.vars["upload_only_current_project_artifacts"] = True | ||
|
|
||
| dbt_project.dbt_runner.run(select=model_name) | ||
|
|
||
| tests = dbt_project.read_table("dbt_tests", raise_if_empty=True) | ||
| test_packages = {row["package_name"] for row in tests} | ||
| assert test_packages == {"elementary_tests"}, ( | ||
| "Expected only 'elementary_tests' tests when filtering is enabled, " | ||
| f"but found packages: {test_packages}" | ||
| ) | ||
| finally: | ||
| if dbt_model_path.exists(): | ||
| dbt_model_path.unlink() | ||
This file contains hidden or 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,9 @@ | ||
| {%- macro filter_to_current_project_if_needed(entities) -%} | ||
| {%- if elementary.get_config_var("upload_only_current_project_artifacts") -%} | ||
| {% set project_name = elementary.get_project_name() %} | ||
| {% do return( | ||
| entities | selectattr("package_name", "==", project_name) | list | ||
| ) %} | ||
| {%- else -%} {% do return(entities | list) %} | ||
| {%- endif -%} | ||
| {%- endmacro -%} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.