Skip to content

Commit

Permalink
Default to current working directory for profiles.yml and fall back…
Browse files Browse the repository at this point in the history
… to `~/.dbt` (#5717)

* Method for capturing standard out during testing (rather than logs)

* Allow dbt exit code assertion to be optional

* Verify priority order to search for profiles.yml configuration

* Updates after pre-commit checks

* Test searching for profiles.yml within the dbt project directory before `~/.dbt/`

* Refactor `dbt debug` to move to the project directory prior to looking up profiles directory

* Search the current working directory for profiles.yml

* Changelog

* Formatting with Black

* Move `run_dbt_and_capture_stdout` into the test case

* Update CLI help text

* Unify separate DEFAULT_PROFILES_DIR definitions

* Remove unused PROFILE_DIR_MESSAGE

* Remove unused DEFAULT_PROFILES_DIR

* Use shared definition of DEFAULT_PROFILES_DIR

* Define global vs. local profiles location and dynamically determine the default

* Restore original

* Remove function for determining the default profiles directory
  • Loading branch information
dbeatty10 authored Sep 20, 2022
1 parent 6baaa2b commit bbf4fc3
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 27 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20220825-195023.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Search current working directory for `profiles.yml`
time: 2022-08-25T19:50:23.940417-06:00
custom:
Author: dbeatty10
Issue: "5411"
PR: "5717"
12 changes: 5 additions & 7 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,17 @@
profiles_dir = click.option(
"--profiles-dir",
envvar="DBT_PROFILES_DIR",
help=f"Which directory to look in for the profiles.yml file. Default = {PurePath.joinpath(Path.home(), '.dbt')}",
default=PurePath.joinpath(Path.home(), ".dbt"),
type=click.Path(
exists=True,
),
help="Which directory to look in for the profiles.yml file. If not set, dbt will look in the current working directory first, then HOME/.dbt/",
default=None,
type=click.Path(),
)

project_dir = click.option(
"--project-dir",
envvar=None,
help="Which directory to look in for the dbt_project.yml file. Default is the current working directory and its parents.",
default=Path.cwd(),
type=click.Path(exists=True),
default=None,
type=click.Path(),
)

quiet = click.option(
Expand Down
4 changes: 1 addition & 3 deletions core/dbt/config/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

DEFAULT_THREADS = 1

DEFAULT_PROFILES_DIR = os.path.join(os.path.expanduser("~"), ".dbt")

INVALID_PROFILE_MESSAGE = """
dbt encountered an error while trying to read your profiles.yml file.
Expand All @@ -44,7 +42,7 @@
{profiles_file}/profiles.yml
""".format(
profiles_file=DEFAULT_PROFILES_DIR
profiles_file=flags.DEFAULT_PROFILES_DIR
)


Expand Down
9 changes: 8 additions & 1 deletion core/dbt/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
# PROFILES_DIR must be set before the other flags
# It also gets set in main.py and in set_from_args because the rpc server
# doesn't go through exactly the same main arg processing.
DEFAULT_PROFILES_DIR = os.path.join(os.path.expanduser("~"), ".dbt")
GLOBAL_PROFILES_DIR = os.path.join(os.path.expanduser("~"), ".dbt")
LOCAL_PROFILES_DIR = os.getcwd()
# Use the current working directory if there is a profiles.yml file present there
if os.path.exists(Path(LOCAL_PROFILES_DIR) / Path("profiles.yml")):
DEFAULT_PROFILES_DIR = LOCAL_PROFILES_DIR
else:
DEFAULT_PROFILES_DIR = GLOBAL_PROFILES_DIR
PROFILES_DIR = os.path.expanduser(os.getenv("DBT_PROFILES_DIR", DEFAULT_PROFILES_DIR))

STRICT_MODE = False # Only here for backwards compatibility
Expand Down Expand Up @@ -52,6 +58,7 @@

_NON_DBT_ENV_FLAGS = ["DO_NOT_TRACK"]


# Global CLI defaults. These flags are set from three places:
# CLI args, environment variables, and user_config (profiles.yml).
# Environment variables use the pattern 'DBT_{flag name}', like DBT_PROFILES_DIR
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_dbt_config(project_dir, args=None, single_threaded=False):
if os.getenv("DBT_PROFILES_DIR"):
profiles_dir = os.getenv("DBT_PROFILES_DIR")
else:
profiles_dir = os.path.expanduser("~/.dbt")
profiles_dir = flags.DEFAULT_PROFILES_DIR

profile = args.profile if hasattr(args, "profile") else None
target = args.target if hasattr(args, "target") else None
Expand Down
14 changes: 5 additions & 9 deletions core/dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import dbt.tracking

from dbt.utils import ExitCodes, args_to_dict
from dbt.config.profile import DEFAULT_PROFILES_DIR, read_user_config
from dbt.config.profile import read_user_config
from dbt.exceptions import InternalException, NotImplementedException, FailedToConnectException


Expand Down Expand Up @@ -258,10 +258,8 @@ def _build_base_subparser():
dest="sub_profiles_dir", # Main cli arg precedes subcommand
type=str,
help="""
Which directory to look in for the profiles.yml file. Default = {}
""".format(
DEFAULT_PROFILES_DIR
),
Which directory to look in for the profiles.yml file. If not set, dbt will look in the current working directory first, then HOME/.dbt/
""",
)

base_subparser.add_argument(
Expand Down Expand Up @@ -1059,10 +1057,8 @@ def parse_args(args, cls=DBTArgumentParser):
dest="profiles_dir",
type=str,
help="""
Which directory to look in for the profiles.yml file. Default = {}
""".format(
DEFAULT_PROFILES_DIR
),
Which directory to look in for the profiles.yml file. If not set, dbt will look in the current working directory first, then HOME/.dbt/
""",
)

p.add_argument(
Expand Down
1 change: 1 addition & 0 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def get_nearest_project_dir(args):
def move_to_nearest_project_dir(args):
nearest_project_dir = get_nearest_project_dir(args)
os.chdir(nearest_project_dir)
return nearest_project_dir


class ConfiguredTask(BaseTask):
Expand Down
4 changes: 0 additions & 4 deletions core/dbt/task/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

from dbt.task.base import BaseTask, get_nearest_project_dir

PROFILE_DIR_MESSAGE = """To view your profiles.yml file, run:
{open_cmd} {profiles_dir}"""

ONLY_PROFILE_MESSAGE = """
A `dbt_project.yml` file was not found in this directory.
Using the only profile `{}`.
Expand Down
2 changes: 0 additions & 2 deletions test/unit/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from .utils import normalize
from dbt import flags
from dbt.contracts.project import UserConfig
from dbt.config.profile import DEFAULT_PROFILES_DIR

from dbt.graph.selector_spec import IndirectSelection

class TestFlags(TestCase):
Expand Down

0 comments on commit bbf4fc3

Please sign in to comment.