Skip to content

Commit

Permalink
Do dbt parse when given skip datasource option
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Chun, Chang <[email protected]>
  • Loading branch information
wcchang1115 committed Oct 12, 2023
1 parent 6e36aa6 commit f13cc17
Showing 1 changed file with 59 additions and 9 deletions.
68 changes: 59 additions & 9 deletions piperider_cli/recipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import piperider_cli.dbtutil as dbtutil
from piperider_cli import get_run_json_path, load_jinja_template, load_json
from piperider_cli.configuration import Configuration, FileSystem
from piperider_cli.dbt import dbt_version
from piperider_cli.error import RecipeConfigException
from piperider_cli.event import CompareEventPayload
from piperider_cli.recipes.utils import InteractiveStopException
Expand Down Expand Up @@ -274,6 +275,13 @@ def replace_commands_dbt_state_path(commands: List[str], dbt_state_path: str):
return [command.replace('<DBT_STATE_PATH>', dbt_state_path) for command in commands]


def prepare_dbt_state(cfg: RecipeConfiguration, options: Dict, recipe_type: str = 'base'):
if options.get('skip_datasource_connection') and dbt_version >= '1.5':
execute_dbt_parse_archive(cfg, recipe_type=recipe_type)

Check warning on line 280 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L279-L280

Added lines #L279 - L280 were not covered by tests
else:
execute_dbt_compile_archive(cfg, recipe_type=recipe_type)

Check warning on line 282 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L282

Added line #L282 was not covered by tests


def prepare_dbt_resources_candidate(cfg: RecipeConfiguration, options: Dict):
config = Configuration.instance()
state = None
Expand All @@ -282,15 +290,18 @@ def prepare_dbt_resources_candidate(cfg: RecipeConfiguration, options: Dict):
select = update_select_with_cli_option(options)

if require_base_state(cfg):
execute_dbt_compile_archive(cfg, recipe_type='base')
prepare_dbt_state(cfg, options, recipe_type='base')

Check warning on line 293 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L293

Added line #L293 was not covered by tests
state = cfg.base.state_path

if cfg.target.ref is not None:
execute_dbt_compile_archive(cfg, recipe_type='target')
prepare_dbt_state(cfg, options, recipe_type='target')

Check warning on line 297 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L297

Added line #L297 was not covered by tests
target_path = 'state'
else:
execute_dbt_deps(cfg.target)
execute_dbt_compile(cfg.target)
if options.get('skip_datasource_connection') and dbt_version >= '1.5':
execute_dbt_parse(cfg.target)

Check warning on line 302 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L301-L302

Added lines #L301 - L302 were not covered by tests
else:
execute_dbt_compile(cfg.target)

Check warning on line 304 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L304

Added line #L304 was not covered by tests
dbt_project = dbtutil.load_dbt_project(config.dbt.get('projectDir'))
target_path = dbt_project.get('target-path') if dbt_project.get('target-path') else 'target'

Expand Down Expand Up @@ -351,12 +362,7 @@ def execute_recipe(cfg: RecipeConfiguration, recipe_type='base', debug=False, ev
console.print()


def execute_dbt_compile_archive(cfg: RecipeConfiguration, recipe_type='base'):
if recipe_type == 'target':
model = cfg.target
else:
model = cfg.base

def execute_dbt_archive(cfg: RecipeConfiguration, recipe_type='base'):
if recipe_type == 'target':
branch_or_commit = tool().git_rev_parse(cfg.target.ref)
else:
Expand All @@ -366,6 +372,33 @@ def execute_dbt_compile_archive(cfg: RecipeConfiguration, recipe_type='base'):
if not branch_or_commit:
raise RecipeConfigException("Branch is not specified")

return branch_or_commit

Check warning on line 375 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L375

Added line #L375 was not covered by tests


def execute_dbt_parse_archive(cfg: RecipeConfiguration, recipe_type='base'):
branch_or_commit = execute_dbt_archive(cfg, recipe_type)

Check warning on line 379 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L379

Added line #L379 was not covered by tests

if recipe_type == 'target':
model = cfg.target

Check warning on line 382 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L381-L382

Added lines #L381 - L382 were not covered by tests
else:
model = cfg.base

Check warning on line 384 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L384

Added line #L384 was not covered by tests

if model.tmp_dir_path is None:
model.tmp_dir_path = tool().git_archive(branch_or_commit)
model.state_path = Path(os.path.join(model.tmp_dir_path, 'state')).as_posix()

Check warning on line 388 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L386-L388

Added lines #L386 - L388 were not covered by tests

execute_dbt_deps(model, model.tmp_dir_path, Path(FileSystem.DBT_PROFILES_DIR).as_posix())
execute_dbt_parse(model, model.tmp_dir_path, Path(FileSystem.DBT_PROFILES_DIR).as_posix(), model.state_path)

Check warning on line 391 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L390-L391

Added lines #L390 - L391 were not covered by tests


def execute_dbt_compile_archive(cfg: RecipeConfiguration, recipe_type='base'):
branch_or_commit = execute_dbt_archive(cfg, recipe_type)

Check warning on line 395 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L395

Added line #L395 was not covered by tests

if recipe_type == 'target':
model = cfg.target

Check warning on line 398 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L397-L398

Added lines #L397 - L398 were not covered by tests
else:
model = cfg.base

Check warning on line 400 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L400

Added line #L400 was not covered by tests

if model.tmp_dir_path is None:
model.tmp_dir_path = tool().git_archive(branch_or_commit)
model.state_path = Path(os.path.join(model.tmp_dir_path, 'state')).as_posix()
Expand Down Expand Up @@ -407,6 +440,23 @@ def execute_dbt_compile(model: RecipeModel, project_dir: str = None, profiles_di
console.print()


def execute_dbt_parse(model: RecipeModel, project_dir: str = None, profiles_dir: str = None, target_path: str = None):
console.print("Run: \[dbt parse]")
cmd = 'dbt parse'
if project_dir:
cmd += f' --project-dir {project_dir}'
if target_path:
cmd += f' --target-path {target_path}'
if profiles_dir:
cmd += f' --profiles-dir {profiles_dir}'
exit_code = tool().execute_command_with_showing_output(cmd.strip(), model.dbt.envs())
if exit_code != 0:
console.print(

Check warning on line 454 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L444-L454

Added lines #L444 - L454 were not covered by tests
f"[bold yellow]Warning: [/bold yellow] Dbt command failed: '{cmd}' with exit code: {exit_code}")
sys.exit(exit_code)
console.print()

Check warning on line 457 in piperider_cli/recipes/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/recipes/__init__.py#L456-L457

Added lines #L456 - L457 were not covered by tests


def execute_recipe_archive(cfg: RecipeConfiguration, recipe_type='base', debug=False, event_payload=CompareEventPayload()):
"""
We execute a recipe in the following steps:
Expand Down

0 comments on commit f13cc17

Please sign in to comment.