Skip to content
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

[#4253] Support partial parsing of env_vars in metrics definitions #4322

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## dbt-core 1.0.0 (Release TBD)

### Under the hood
### Fixes
- Support partial parsing of env_vars in metrics ([#4253](https://github.com/dbt-labs/dbt-core/issues/4293), [#4322](https://github.com/dbt-labs/dbt-core/pull/4322))

### Under the hood
- Change some CompilationExceptions to ParsingExceptions ([#4254](http://github.com/dbt-labs/dbt-core/issues/4254), [#4328](https://github.com/dbt-core/pull/4328))


## dbt-core 1.0.0rc2 (November 22, 2021)

### Breaking changes
Expand All @@ -21,6 +24,11 @@
- Fix serialization error with missing quotes in metrics model ref ([#4252](https://github.com/dbt-labs/dbt-core/issues/4252), [#4287](https://github.com/dbt-labs/dbt-core/pull/4289))
- Correct definition of 'created_at' in ParsedMetric nodes ([#4298](http://github.com/dbt-labs/dbt-core/issues/4298), [#4299](https://github.com/dbt-labs/dbt-core/pull/4299))

### Fixes
- Allow specifying default in Jinja config.get with default keyword ([#4273](https://github.com/dbt-labs/dbt-core/issues/4273), [#4297](https://github.com/dbt-labs/dbt-core/pull/4297))
- Fix serialization error with missing quotes in metrics model ref ([#4252](https://github.com/dbt-labs/dbt-core/issues/4252), [#4287](https://github.com/dbt-labs/dbt-core/pull/4289))
- Correct definition of 'created_at' in ParsedMetric nodes ([#4298](https://github.com/dbt-labs/dbt-core/issues/4298), [#4299](https://github.com/dbt-labs/dbt-core/pull/4299))

### Under the hood
- Add --indirect-selection parameter to profiles.yml and builtin DBT_ env vars; stringified parameter to enable multi-modal use ([#3997](https://github.com/dbt-labs/dbt-core/issues/3997), [#4270](https://github.com/dbt-labs/dbt-core/pull/4270))
- Fix filesystem searcher test failure on Python 3.9 ([#3689](https://github.com/dbt-labs/dbt-core/issues/3689), [#4271](https://github.com/dbt-labs/dbt-core/pull/4271))
Expand Down
18 changes: 14 additions & 4 deletions core/dbt/parser/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,21 +693,31 @@ def handle_schema_file_changes(self, schema_file, saved_yaml_dict, new_yaml_dict
continue
elem = self.get_schema_element(new_yaml_dict[dict_key], name)
if elem:
self.delete_schema_exposure(schema_file, exposure)
self.merge_patch(schema_file, dict_key, exposure)
self.delete_schema_exposure(schema_file, elem)
self.merge_patch(schema_file, dict_key, elem)

# metrics
dict_key = 'metrics'
metric_diff = self.get_diff_for('metrics', saved_yaml_dict, new_yaml_dict)
if metric_diff['changed']:
for metric in metric_diff['changed']:
self.delete_schema_metric(schema_file, metric)
self.merge_patch(schema_file, 'metrics', metric)
self.merge_patch(schema_file, dict_key, metric)
if metric_diff['deleted']:
for metric in metric_diff['deleted']:
self.delete_schema_metric(schema_file, metric)
if metric_diff['added']:
for metric in metric_diff['added']:
self.merge_patch(schema_file, 'metrics', metric)
self.merge_patch(schema_file, dict_key, metric)
# Handle schema file updates due to env_var changes
if dict_key in env_var_changes and dict_key in new_yaml_dict:
for name in env_var_changes[dict_key]:
if name in metric_diff['changed_or_deleted_names']:
continue
elem = self.get_schema_element(new_yaml_dict[dict_key], name)
if elem:
self.delete_schema_metric(schema_file, elem)
self.merge_patch(schema_file, dict_key, elem)

# Take a "section" of the schema file yaml dictionary from saved and new schema files
# and determine which parts have changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: 2

metrics:

- model: "ref('people')"
name: number_of_people
description: Total count of people
label: "Number of people"
type: count
sql: "*"
gshank marked this conversation as resolved.
Show resolved Hide resolved
timestamp: created_at
time_grains: [day, week, month]
dimensions:
- favorite_color
- loves_dbt
meta:
my_meta: '{{ env_var("ENV_VAR_METRICS") }}'

- model: "ref('people')"
name: collective_tenure
description: Total number of years of team experience
label: "Collective tenure"
type: sum
sql: tenure
timestamp: created_at
time_grains: [day]
filters:
- field: loves_dbt
operator: is
value: 'true'
18 changes: 18 additions & 0 deletions test/integration/068_partial_parsing_tests/test_pp_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ def test_postgres_env_vars_models(self):
expected_env_vars = {'models': {'model_one': ['TEST_SCHEMA_VAR', 'ENV_VAR_COLOR'], 'model_color': ['ENV_VAR_ENABLED']}, 'exposures': {'proxy_for_dashboard': ['ENV_VAR_OWNER']}}
self.assertEqual(expected_env_vars, schema_file.env_vars)

# Add a metrics file with env_vars
os.environ['ENV_VAR_METRICS'] = 'TeStInG'
self.copy_file('test-files/people.sql', 'models/people.sql')
self.copy_file('test-files/env_var_metrics.yml', 'models/metrics.yml')
results = self.run_dbt(["run"])
manifest = get_manifest()
self.assertIn('ENV_VAR_METRICS', manifest.env_vars)
self.assertEqual(manifest.env_vars['ENV_VAR_METRICS'], 'TeStInG')
metric_node = manifest.metrics['metric.test.number_of_people']
self.assertEqual(metric_node.meta, {'my_meta': 'TeStInG'})

# Change metrics env var
os.environ['ENV_VAR_METRICS'] = 'Changed!'
results = self.run_dbt(["run"])
manifest = get_manifest()
metric_node = manifest.metrics['metric.test.number_of_people']
self.assertEqual(metric_node.meta, {'my_meta': 'Changed!'})

# delete the env vars to cleanup
del os.environ['ENV_VAR_TEST']
Expand All @@ -230,6 +247,7 @@ def test_postgres_env_vars_models(self):
del os.environ['ENV_VAR_COLOR']
del os.environ['ENV_VAR_SOME_KEY']
del os.environ['ENV_VAR_OWNER']
del os.environ['ENV_VAR_METRICS']


class ProjectEnvVarTest(BasePPTest):
Expand Down