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

Add --target and --profile to global config #9081

Merged
merged 15 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231115-092005.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Global config for --target and --profile CLI flags and DBT_TARGET and DBT_PROFILE environment variables.
time: 2023-11-15T09:20:05.12461Z
custom:
Author: barton996
Issue: "7798"
40 changes: 2 additions & 38 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ def global_flags(func):
@p.populate_cache
@p.print
@p.printer_width
@p.profile
@p.quiet
@p.record_timing_info
@p.send_anonymous_usage_stats
@p.single_threaded
@p.state
@p.static_parser
@p.target
barton996 marked this conversation as resolved.
Show resolved Hide resolved
@p.use_colors
@p.use_colors_file
@p.use_experimental_parser
Expand Down Expand Up @@ -172,7 +174,6 @@ def cli(ctx, **kwargs):
@p.export_saved_queries
@p.full_refresh
@p.deprecated_include_saved_query
@p.profile
@p.profiles_dir
@p.project_dir
@p.resource_type
Expand All @@ -181,7 +182,6 @@ def cli(ctx, **kwargs):
@p.selector
@p.show
@p.store_failures
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -211,10 +211,8 @@ def build(ctx, **kwargs):
@click.pass_context
@global_flags
@p.clean_project_files_only
@p.profile
@p.profiles_dir
@p.project_dir
@p.target
@p.target_path
@p.vars
@requires.postflight
Expand Down Expand Up @@ -246,14 +244,12 @@ def docs(ctx, **kwargs):
@global_flags
@p.compile_docs
@p.exclude
@p.profile
@p.profiles_dir
@p.project_dir
@p.select
@p.selector
@p.empty_catalog
@p.static
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -284,10 +280,8 @@ def docs_generate(ctx, **kwargs):
@global_flags
@p.browser
@p.port
@p.profile
@p.profiles_dir
@p.project_dir
@p.target
@p.target_path
@p.vars
@requires.postflight
Expand Down Expand Up @@ -317,15 +311,13 @@ def docs_serve(ctx, **kwargs):
@p.full_refresh
@p.show_output_format
@p.introspect
@p.profile
@p.profiles_dir
@p.project_dir
@p.empty
@p.select
@p.selector
@p.inline
@p.compile_inject_ephemeral_ctes
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -360,13 +352,11 @@ def compile(ctx, **kwargs):
@p.show_output_format
@p.show_limit
@p.introspect
@p.profile
@p.profiles_dir
@p.project_dir
@p.select
@p.selector
@p.inline
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -398,10 +388,8 @@ def show(ctx, **kwargs):
@global_flags
@p.debug_connection
@p.config_dir
@p.profile
@p.profiles_dir_exists_false
@p.project_dir
@p.target
@p.vars
@requires.postflight
@requires.preflight
Expand All @@ -423,10 +411,8 @@ def debug(ctx, **kwargs):
@cli.command("deps")
@click.pass_context
@global_flags
@p.profile
@p.profiles_dir_exists_false
@p.project_dir
@p.target
@p.vars
@p.source
@p.lock
Expand Down Expand Up @@ -468,11 +454,9 @@ def deps(ctx, **kwargs):
@global_flags
# for backwards compatibility, accept 'project_name' as an optional positional argument
@click.argument("project_name", required=False)
@p.profile
@p.profiles_dir_exists_false
@p.project_dir
@p.skip_profile_setup
@p.target
@p.vars
@requires.postflight
@requires.preflight
Expand All @@ -495,14 +479,12 @@ def init(ctx, **kwargs):
@p.models
@p.output
@p.output_keys
@p.profile
@p.profiles_dir
@p.project_dir
@p.resource_type
@p.exclude_resource_type
@p.raw_select
@p.selector
@p.target
@p.target_path
@p.vars
@requires.postflight
Expand Down Expand Up @@ -536,10 +518,8 @@ def list(ctx, **kwargs):
@cli.command("parse")
@click.pass_context
@global_flags
@p.profile
@p.profiles_dir
@p.project_dir
@p.target
@p.target_path
@p.threads
@p.vars
Expand All @@ -561,13 +541,11 @@ def parse(ctx, **kwargs):
@global_flags
@p.exclude
@p.full_refresh
@p.profile
@p.profiles_dir
@p.project_dir
@p.empty
@p.select
@p.selector
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -599,8 +577,6 @@ def run(ctx, **kwargs):
@p.project_dir
@p.profiles_dir
@p.vars
@p.profile
@p.target
@p.target_path
@p.threads
@p.full_refresh
Expand Down Expand Up @@ -630,14 +606,12 @@ def retry(ctx, **kwargs):
@global_flags
@p.exclude
@p.full_refresh
@p.profile
@p.profiles_dir
@p.project_dir
@p.resource_type
@p.exclude_resource_type
@p.select
@p.selector
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -668,10 +642,8 @@ def clone(ctx, **kwargs):
@global_flags
@click.argument("macro")
@p.args
@p.profile
@p.profiles_dir
@p.project_dir
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -702,13 +674,11 @@ def run_operation(ctx, **kwargs):
@global_flags
@p.exclude
@p.full_refresh
@p.profile
@p.profiles_dir
@p.project_dir
@p.select
@p.selector
@p.show
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -737,12 +707,10 @@ def seed(ctx, **kwargs):
@click.pass_context
@global_flags
@p.exclude
@p.profile
@p.profiles_dir
@p.project_dir
@p.select
@p.selector
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -781,12 +749,10 @@ def source(ctx, **kwargs):
@global_flags
@p.exclude
@p.output_path # TODO: Is this ok to re-use? We have three different output params, how much can we consolidate?
@p.profile
@p.profiles_dir
@p.project_dir
@p.select
@p.selector
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down Expand Up @@ -822,13 +788,11 @@ def freshness(ctx, **kwargs):
@click.pass_context
@global_flags
@p.exclude
@p.profile
@p.profiles_dir
@p.project_dir
@p.select
@p.selector
@p.store_failures
@p.target
@p.target_path
@p.threads
@p.vars
Expand Down
4 changes: 2 additions & 2 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@

profile = click.option(
"--profile",
envvar=None,
envvar="DBT_PROFILE",
help="Which existing profile to load. Overrides setting in dbt_project.yml.",
)

Expand Down Expand Up @@ -603,7 +603,7 @@
target = click.option(
"--target",
"-t",
envvar=None,
envvar="DBT_TARGET",
help="Which target to load for the given profile",
)

Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ def test_commands_in_enum_and_dict(self):
continue
cmd = Command.from_str(command.name)
command_args(cmd)

def test_global_flags_not_on_subcommands(self):
def run_test(command):
# For each subcommand
for command_name, command in command.commands.items():
# Get string representation of all parameter options for this subcommand e.g. "['-t', '--target']"
parameter_options = [str(sorted(param.opts)) for param in command.params]
# Check there are no parameter options that have been applied twice
assert sorted(list(set(parameter_options))) == sorted(parameter_options)

run_test(cli)
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions tests/unit/test_cli_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,38 @@ def test_log_file_settings_from_config(self):
assert flags.USE_COLORS is True
assert flags.USE_COLORS_FILE is False

@pytest.mark.parametrize(
"cli_profile, flag_profile",
[
(None, None),
("profile", "profile"),
],
)
@pytest.mark.parametrize(
"cli_target, flag_target",
[
(None, None),
("target", "target"),
],
)
def test_profile_settings_interaction(
self, cli_profile, cli_target, flag_profile, flag_target
):
cli_params = ["run"]

if cli_profile:
cli_params = ["--profile", cli_profile] + cli_params

if cli_target:
cli_params = ["--target", cli_target] + cli_params

context = self.make_dbt_context("run", cli_params)
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved

flags = Flags(context, None)

assert flags.PROFILE == flag_profile
assert flags.TARGET == flag_target

def test_duplicate_flags_raises_error(self):
parent_context = self.make_dbt_context("parent", ["--version-check"])
context = self.make_dbt_context("child", ["--version-check"], parent_context)
Expand Down
Loading