-
Notifications
You must be signed in to change notification settings - Fork 359
Feat(dbt_cli): Add support for '--profile' and '--target' #5174
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
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,8 +101,10 @@ def _read_profile( | |
| target_name = context.render(project_data.get("target")) | ||
|
|
||
| if target_name not in outputs: | ||
| target_names = "\n".join(f"- {name}" for name in outputs) | ||
| raise ConfigError( | ||
| f"Target '{target_name}' not specified in profiles for '{context.profile_name}'." | ||
| f"Target '{target_name}' not specified in profiles for '{context.profile_name}'. " | ||
| f"The valid target names for this profile are:\n{target_names}" | ||
|
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. I noticed that the dbt cli lists valid targets if you get the profile correct but the target wrong, so I updated this to do the same |
||
| ) | ||
|
|
||
| target_fields = load_yaml(context.render(yaml.dump(outputs[target_name]))) | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import typing as t | ||
| import logging | ||
| from functools import wraps | ||
| import click | ||
| import sys | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def cli_global_error_handler( | ||
| func: t.Callable[..., t.Any], | ||
| ) -> t.Callable[..., t.Any]: | ||
| @wraps(func) | ||
| def wrapper(*args: t.List[t.Any], **kwargs: t.Any) -> t.Any: | ||
| try: | ||
| return func(*args, **kwargs) | ||
| except Exception as ex: | ||
| # these imports are deliberately deferred to avoid the penalty of importing the `sqlmesh` | ||
| # package up front for every CLI command | ||
| from sqlmesh.utils.errors import SQLMeshError | ||
| from sqlglot.errors import SqlglotError | ||
|
|
||
| if isinstance(ex, (SQLMeshError, SqlglotError, ValueError)): | ||
| click.echo(click.style("Error: " + str(ex), fg="red")) | ||
| sys.exit(1) | ||
| else: | ||
| raise | ||
| finally: | ||
| context_or_obj = args[0] | ||
| sqlmesh_context = ( | ||
| context_or_obj.obj if isinstance(context_or_obj, click.Context) else context_or_obj | ||
| ) | ||
| if sqlmesh_context is not None: | ||
| # important to import this only if a context was created | ||
| # otherwise something like `sqlmesh_dbt run --help` will trigger this import because it's in the finally: block | ||
| from sqlmesh import Context | ||
|
|
||
| if isinstance(sqlmesh_context, Context): | ||
| sqlmesh_context.close() | ||
|
|
||
| return wrapper |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import typing as t | ||
| from pathlib import Path | ||
| import pytest | ||
| from click.testing import Result | ||
|
|
||
| pytestmark = pytest.mark.slow | ||
|
|
||
|
|
||
| def test_profile_and_target(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[..., Result]): | ||
| # profile doesnt exist - error | ||
| result = invoke_cli(["--profile", "nonexist"]) | ||
| assert result.exit_code == 1 | ||
| assert "Profile 'nonexist' not found in profiles" in result.output | ||
|
|
||
| # profile exists - successful load with default target | ||
| result = invoke_cli(["--profile", "jaffle_shop"]) | ||
| assert result.exit_code == 0 | ||
| assert "No command specified" in result.output | ||
|
|
||
| # profile exists but target doesnt - error | ||
| result = invoke_cli(["--profile", "jaffle_shop", "--target", "nonexist"]) | ||
| assert result.exit_code == 1 | ||
| assert "Target 'nonexist' not specified in profiles" in result.output | ||
| assert "valid target names for this profile are" in result.output | ||
| assert "- dev" in result.output | ||
|
|
||
| # profile exists and so does target - successful load with specified target | ||
| result = invoke_cli(["--profile", "jaffle_shop", "--target", "dev"]) | ||
| assert result.exit_code == 0 | ||
| assert "No command specified" in result.output |
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
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.
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.
The
config_loader_kwargsparameter was added toContextso it could flow through to here.I first attempted to make these arguments to the
DbtLoader, but theyre needed to work out theConnectionConfigand by the timeDbtLoaderis invoked it's too late