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

Default to current working directory for profiles.yml and fall back to ~/.dbt #5717

Merged
merged 20 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
38f9781
Method for capturing standard out during testing (rather than logs)
dbeatty10 Aug 25, 2022
177160d
Allow dbt exit code assertion to be optional
dbeatty10 Aug 25, 2022
5b683dd
Verify priority order to search for profiles.yml configuration
dbeatty10 Aug 25, 2022
1934113
Updates after pre-commit checks
dbeatty10 Aug 25, 2022
d4c4772
Test searching for profiles.yml within the dbt project directory befo…
dbeatty10 Aug 25, 2022
cda7b74
Refactor `dbt debug` to move to the project directory prior to lookin…
dbeatty10 Aug 25, 2022
3e40f11
Search the current working directory for profiles.yml
dbeatty10 Aug 26, 2022
44ac886
Changelog
dbeatty10 Aug 26, 2022
e2cd1c9
Formatting with Black
dbeatty10 Aug 26, 2022
60d7d2b
Move `run_dbt_and_capture_stdout` into the test case
dbeatty10 Aug 30, 2022
2d1c7ce
Merge branch 'main' into dbeatty/project-profiles
dbeatty10 Sep 16, 2022
90764b0
Update CLI help text
dbeatty10 Sep 16, 2022
af562b3
Unify separate DEFAULT_PROFILES_DIR definitions
dbeatty10 Sep 16, 2022
27fe03e
Remove unused PROFILE_DIR_MESSAGE
dbeatty10 Sep 16, 2022
9c72b0a
Remove unused DEFAULT_PROFILES_DIR
dbeatty10 Sep 16, 2022
bb875a1
Use shared definition of DEFAULT_PROFILES_DIR
dbeatty10 Sep 16, 2022
f3ca376
Define global vs. local profiles location and dynamically determine t…
dbeatty10 Sep 16, 2022
5c7fc7e
Restore original
dbeatty10 Sep 16, 2022
7f8136c
Remove function for determining the default profiles directory
dbeatty10 Sep 16, 2022
f560e17
Merge branch 'main' into dbeatty/project-profiles
dbeatty10 Sep 20, 2022
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
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,
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
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