Skip to content

Commit

Permalink
add --project-dir flag to allow specifying project directory
Browse files Browse the repository at this point in the history
In particular, this is the directory that contains the dbt_project.yml
file and all the related project files and directories.

Without this flag, it is necessary to cd to the project directory before
running dbt.

See #1544
  • Loading branch information
Matt Ball committed Jun 17, 2019
1 parent 85164b6 commit 2e7c1fd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
10 changes: 10 additions & 0 deletions core/dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ def run_from_args(parsed):
def _build_base_subparser():
base_subparser = argparse.ArgumentParser(add_help=False)

base_subparser.add_argument(
'--project-dir',
default=None,
type=str,
help="""
Which directory to look in for the dbt_project.yml file.
Default is the current working directory and its parents.
"""
)

base_subparser.add_argument(
'--profiles-dir',
default=PROFILES_DIR,
Expand Down
31 changes: 20 additions & 11 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,19 @@ def interpret_results(self, results):
return True


def get_nearest_project_dir():
def get_nearest_project_dir(args):
# If the user provides an explicit project directory, use that
# but don't look at parent directories.
if args.project_dir:
project_file = os.path.join(args.project_dir, "dbt_project.yml")
if os.path.exists(project_file):
return args.project_dir
else:
raise dbt.exceptions.RuntimeException(
"fatal: Invalid --project-dir flag. Not a dbt project. "
"Missing dbt_project.yml file"
)

root_path = os.path.abspath(os.sep)
cwd = os.getcwd()

Expand All @@ -102,24 +114,21 @@ def get_nearest_project_dir():
return cwd
cwd = os.path.dirname(cwd)

return None

raise dbt.exceptions.RuntimeException(
"fatal: Not a dbt project (or any of the parent directories). "
"Missing dbt_project.yml file"
)

def move_to_nearest_project_dir():
nearest_project_dir = get_nearest_project_dir()
if nearest_project_dir is None:
raise dbt.exceptions.RuntimeException(
"fatal: Not a dbt project (or any of the parent directories). "
"Missing dbt_project.yml file"
)

def move_to_nearest_project_dir(args):
nearest_project_dir = get_nearest_project_dir(args)
os.chdir(nearest_project_dir)


class RequiresProjectTask(BaseTask):
@classmethod
def from_args(cls, args):
move_to_nearest_project_dir()
move_to_nearest_project_dir(args)
return super(RequiresProjectTask, cls).from_args(args)


Expand Down

0 comments on commit 2e7c1fd

Please sign in to comment.