-
Notifications
You must be signed in to change notification settings - Fork 358
Feat(dbt_cli): Add support for --vars
#5205
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import typing as t | ||
| import click | ||
| from click.core import Context, Parameter | ||
|
|
||
|
|
||
| class YamlParamType(click.ParamType): | ||
| name = "yaml" | ||
|
|
||
| def convert( | ||
| self, value: t.Any, param: t.Optional[Parameter], ctx: t.Optional[Context] | ||
| ) -> t.Any: | ||
| if not isinstance(value, str): | ||
| self.fail(f"Input value '{value}' should be a string", param, ctx) | ||
|
|
||
| from sqlmesh.utils import yaml | ||
|
|
||
| try: | ||
| parsed = yaml.load(source=value, render_jinja=False) | ||
| except: | ||
| self.fail(f"String '{value}' is not valid YAML", param, ctx) | ||
|
|
||
| if not isinstance(parsed, dict): | ||
| self.fail(f"String '{value}' did not evaluate to a dict, got: {parsed}", param, ctx) | ||
|
|
||
| return parsed |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,3 +46,17 @@ def test_list_select_exclude(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[.. | |
| assert "main.orders" not in result.output | ||
| assert "main.stg_payments" not in result.output | ||
| assert "main.raw_orders" not in result.output | ||
|
|
||
|
|
||
| def test_list_with_vars(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[..., Result]): | ||
| (jaffle_shop_duckdb / "models" / "aliased_model.sql").write_text(""" | ||
| {{ config(alias='model_' + var('foo')) }} | ||
| select 1 | ||
| """) | ||
|
|
||
| result = invoke_cli(["list", "--vars", "foo: bar"]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to test more variances like: or are we confident that the YAML parser takes care of it?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The YAML parser does indeed take care of it, but I added a test |
||
|
|
||
| assert result.exit_code == 0 | ||
| assert not result.exception | ||
|
|
||
| assert "model_bar" in result.output | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import typing as t | ||
| import pytest | ||
| from sqlmesh_dbt.options import YamlParamType | ||
| from click.exceptions import BadParameter | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input,expected", | ||
| [ | ||
| (1, BadParameter("Input value '1' should be a string")), | ||
| ("", BadParameter("String '' is not valid YAML")), | ||
| ("['a', 'b']", BadParameter("String.*did not evaluate to a dict, got.*")), | ||
| ("foo: bar", {"foo": "bar"}), | ||
| ('{"key": "value", "date": 20180101}', {"key": "value", "date": 20180101}), | ||
| ("{key: value, date: 20180101}", {"key": "value", "date": 20180101}), | ||
| ], | ||
| ) | ||
| def test_yaml_param_type(input: str, expected: t.Union[BadParameter, t.Dict[str, t.Any]]): | ||
| if isinstance(expected, BadParameter): | ||
| with pytest.raises(BadParameter, match=expected.message): | ||
| YamlParamType().convert(input, None, None) | ||
| else: | ||
| assert YamlParamType().convert(input, None, None) == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we handle this differently from
profile/ target`?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--profile/--targetare top level dbt options - they show indbt --helpand apply to every subcommand.--varsonly applies to certain subcommands. eg it shows indbt list --helpanddbt run --helpbut notdbt source --help