Skip to content

Commit

Permalink
Validate project names in interactive dbt init (#4536) (#4675)
Browse files Browse the repository at this point in the history
* Validate project names in interactive dbt init

- workflow: ask the user to provide a valid project name until they do.
- new integration tests
- supported scenarios:
  - dbt init
  - dbt init -s
  - dbt init [name]
  - dbt init [name] -s

* Update Changelog.md

* Add full URLs to CHANGELOG.md

Co-authored-by: Chenyu Li <[email protected]>

Co-authored-by: Chenyu Li <[email protected]>

Co-authored-by: Amir Kadivar <[email protected]>
  • Loading branch information
ChenyuLInx and amirkdv authored Feb 3, 2022
1 parent 3be057b commit bec6bec
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
### Features
- New Dockerfile to support specific db adapters and platforms. See docker/README.md for details ([#4495](https:/dbt-labs/dbt-core/issues/4495), [#4487](https:/dbt-labs/dbt-core/pull/4487))

### Fixes

* Add project name validation to `dbt init` ([#4490](https:/dbt-labs/dbt-core/issues/4490),[#4536](https:/dbt-labs/dbt-core/pull/4536))

### Under the hood
- Testing cleanup ([#4496](https:/dbt-labs/dbt-core/pull/4496), [#4509](https:/dbt-labs/dbt-core/pull/4509))
- Clean up test deprecation warnings ([#3988](https:/dbt-labs/dbt-core/issue/3988), [#4556](https:/dbt-labs/dbt-core/pull/4556))
Expand All @@ -23,6 +27,10 @@ Contributors:

## dbt-core 1.0.1 (January 03, 2022)

Contributors:

* [@amirkdv](https:/amirkdv) ([#4536](https:/dbt-labs/dbt-core/pull/4536))

## dbt-core 1.0.1rc1 (December 20, 2021)

### Fixes
Expand Down
12 changes: 12 additions & 0 deletions core/dbt/contracts/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
class Name(ValidatedStringMixin):
ValidationRegex = r'^[^\d\W]\w*$'

@classmethod
def is_valid(cls, value: Any) -> bool:
if not isinstance(value, str):
return False

try:
cls.validate(value)
except ValidationError:
return False

return True


register_pattern(Name, r'^[^\d\W]\w*$')

Expand Down
18 changes: 13 additions & 5 deletions core/dbt/task/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from dbt.version import _get_adapter_plugin_names
from dbt.adapters.factory import load_plugin, get_include_paths

from dbt.contracts.project import Name as ProjectName

from dbt.events.functions import fire_event
from dbt.events.types import (
StarterProjectPath, ConfigFolderDirectory, NoSampleProfileFound, ProfileWrittenWithSample,
Expand Down Expand Up @@ -269,6 +271,16 @@ def ask_for_adapter_choice(self) -> str:
numeric_choice = click.prompt(prompt_msg, type=click.INT)
return available_adapters[numeric_choice - 1]

def get_valid_project_name(self) -> str:
"""Returns a valid project name, either from CLI arg or user prompt."""
name = self.args.project_name
while not ProjectName.is_valid(name):
if name:
click.echo(name + " is not a valid project name.")
name = click.prompt("Enter a name for your project (letters, digits, underscore)")

return name

def run(self):
"""Entry point for the init task."""
profiles_dir = flags.PROFILES_DIR
Expand Down Expand Up @@ -306,11 +318,7 @@ def run(self):

# When dbt init is run outside of an existing project,
# create a new project and set up the user's profile.
project_name = self.args.project_name
if project_name is None:
# If project name is not provided,
# ask the user which project name they'd like to use.
project_name = click.prompt("What is the desired project name?")
project_name = self.get_valid_project_name()
project_path = Path(project_name)
if project_path.exists():
fire_event(ProjectNameAlreadyExists(name=project_name))
Expand Down
54 changes: 44 additions & 10 deletions test/integration/040_init_test/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def test_postgres_init_task_outside_of_project(self, mock_prompt, mock_confirm):
]
self.run_dbt(['init'])
manager.assert_has_calls([
call.prompt('What is the desired project name?'),
call.prompt("Enter a name for your project (letters, digits, underscore)"),
call.prompt("Which database would you like to use?\n[1] postgres\n\n(Don't see the one you want? https://docs.getdbt.com/docs/available-adapters)\n\nEnter a number", type=click.INT),
call.prompt('host (hostname for the instance)', default=None, hide_input=False, type=None),
call.prompt('port', default=5432, hide_input=False, type=click.INT),
Expand Down Expand Up @@ -532,6 +532,48 @@ def test_postgres_init_with_provided_project_name(self, mock_prompt, mock_confir
+materialized: view
"""

@use_profile('postgres')
@mock.patch('click.confirm')
@mock.patch('click.prompt')
def test_postgres_init_invalid_project_name_cli(self, mock_prompt, mock_confirm):
manager = Mock()
manager.attach_mock(mock_prompt, 'prompt')
manager.attach_mock(mock_confirm, 'confirm')

os.remove('dbt_project.yml')
invalid_name = 'name-with-hyphen'
valid_name = self.get_project_name()
manager.prompt.side_effect = [
valid_name
]

self.run_dbt(['init', invalid_name, '-s'])
manager.assert_has_calls([
call.prompt("Enter a name for your project (letters, digits, underscore)"),
])

@use_profile('postgres')
@mock.patch('click.confirm')
@mock.patch('click.prompt')
def test_postgres_init_invalid_project_name_prompt(self, mock_prompt, mock_confirm):
manager = Mock()
manager.attach_mock(mock_prompt, 'prompt')
manager.attach_mock(mock_confirm, 'confirm')

os.remove('dbt_project.yml')

invalid_name = 'name-with-hyphen'
valid_name = self.get_project_name()
manager.prompt.side_effect = [
invalid_name, valid_name
]

self.run_dbt(['init', '-s'])
manager.assert_has_calls([
call.prompt("Enter a name for your project (letters, digits, underscore)"),
call.prompt("Enter a name for your project (letters, digits, underscore)"),
])

@use_profile('postgres')
@mock.patch('click.confirm')
@mock.patch('click.prompt')
Expand All @@ -546,20 +588,12 @@ def test_postgres_init_skip_profile_setup(self, mock_prompt, mock_confirm):
project_name = self.get_project_name()
manager.prompt.side_effect = [
project_name,
1,
'localhost',
5432,
'test_username',
'test_password',
'test_db',
'test_schema',
4,
]

# provide project name through the ini command
self.run_dbt(['init', '-s'])
manager.assert_has_calls([
call.prompt('What is the desired project name?')
call.prompt("Enter a name for your project (letters, digits, underscore)")
])

with open(os.path.join(self.test_root_dir, project_name, 'dbt_project.yml'), 'r') as f:
Expand Down

0 comments on commit bec6bec

Please sign in to comment.