Skip to content

Commit

Permalink
dbt init Interactive profile creation (#3625)
Browse files Browse the repository at this point in the history
* Initial

* Further dev

* Make mypy happy

* Further dev

* Existing tests passing

* Functioning integration test

* Passing integration test

* Integration tests

* Add changelog entry

* Add integration test for init outside of project

* Fall back to target_options.yml when invalid profile_template.yml is provided

* Use built-in yaml with exception of in init

* Remove oyaml and fix tests

* Update dbt_project.yml in test comparison

* Create the profiles directory if it doesn't exist

* Use safe_load

* Update integration test

Co-authored-by: Jeremy Cohen <jeremy@dbtlabs.com>

automatic commit by git-black, original commits:
  11436fe
  • Loading branch information
NiallRees authored and iknox-fa committed Feb 8, 2022
1 parent 0faea1b commit abfc3ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
36 changes: 13 additions & 23 deletions core/dbt/task/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"int": click.INT,
"float": click.FLOAT,
"bool": click.BOOL,
None: None
None: None,
}


Expand Down Expand Up @@ -100,11 +100,7 @@ def create_profile_from_sample(self, adapter: str, profile_name: str):
sample_profile_name = list(yaml.safe_load(sample_profile).keys())[0]
# Use a regex to replace the name of the sample_profile with
# that of the project without losing any comments from the sample
sample_profile = re.sub(
f"^{sample_profile_name}:",
f"{profile_name}:",
sample_profile
)
sample_profile = re.sub(f"^{sample_profile_name}:", f"{profile_name}:", sample_profile)
profiles_filepath = Path(flags.PROFILES_DIR) / Path("profiles.yml")
if profiles_filepath.exists():
with open(profiles_filepath, "a") as f:
Expand All @@ -127,7 +123,7 @@ def get_addendum(self, project_name: str, profiles_path: str) -> str:
slack_url=SLACK_URL,
)

def generate_target_from_input(
def generate_target_from_input(self, profile_template: dict, target: dict = {}) -> dict:
self,
profile_template: dict,
target: dict = {}
Expand All @@ -139,9 +135,10 @@ def generate_target_from_input(
if key.startswith("_choose"):
choice_type = key[8:].replace("_", " ")
option_list = list(value.keys())
prompt_msg = "\n".join([
f"[{n+1}] {v}" for n, v in enumerate(option_list)
]) + f"\nDesired {choice_type} option (enter a number)"
prompt_msg = (
"\n".join([f"[{n+1}] {v}" for n, v in enumerate(option_list)])
+ f"\nDesired {choice_type} option (enter a number)"
)
numeric_choice = click.prompt(prompt_msg, type=click.INT)
choice = option_list[numeric_choice - 1]
# Complete the chosen option's values in a recursive call
Expand Down Expand Up @@ -198,12 +195,7 @@ def create_profile_from_profile_template(self, profile_template: dict, profile_n
initial_target = profile_template.get('fixed', {})
prompts = profile_template.get('prompts', {})
target = self.generate_target_from_input(prompts, initial_target)
profile = {
"outputs": {
"dev": target
},
"target": "dev"
}
profile = {"outputs": {"dev": target}, "target": "dev"}
self.write_profile(profile, profile_name)

def create_profile_from_target(self, adapter: str, profile_name: str):
Expand Down Expand Up @@ -236,9 +228,7 @@ def check_if_can_write_profile(self, profile_name: Optional[str] = None) -> bool
profiles_file = Path(flags.PROFILES_DIR) / Path("profiles.yml")
if not profiles_file.exists():
return True
profile_name = (
profile_name or self.get_profile_name_from_current_project()
)
profile_name = profile_name or self.get_profile_name_from_current_project()
with open(profiles_file, "r") as f:
profiles = yaml.safe_load(f) or {}
if profile_name in profiles.keys():
Expand All @@ -264,10 +254,10 @@ def ask_for_adapter_choice(self) -> str:
"""Ask the user which adapter (database) they'd like to use."""
available_adapters = list(_get_adapter_plugin_names())
prompt_msg = (
"Which database would you like to use?\n" +
"\n".join([f"[{n+1}] {v}" for n, v in enumerate(available_adapters)]) +
"\n\n(Don't see the one you want? https://docs.getdbt.com/docs/available-adapters)" +
"\n\nEnter a number"
"Which database would you like to use?\n"
+ "\n".join([f"[{n+1}] {v}" for n, v in enumerate(available_adapters)])
+ "\n\n(Don't see the one you want? https://docs.getdbt.com/docs/available-adapters)"
+ "\n\nEnter a number"
)
numeric_choice = click.prompt(prompt_msg, type=click.INT)
return available_adapters[numeric_choice - 1]
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[mypy]
mypy_path = ./third-party-stubs
namespace_packages = True
namespace_packages = True

0 comments on commit abfc3ec

Please sign in to comment.