Skip to content

Commit

Permalink
Move logic to validate event_time cli flags to flags.py
Browse files Browse the repository at this point in the history
  • Loading branch information
QMalcolm committed Oct 7, 2024
1 parent ca9b381 commit 7b995c5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
22 changes: 22 additions & 0 deletions core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
from dataclasses import dataclass
from datetime import datetime
from importlib import import_module
from pathlib import Path
from pprint import pformat as pf
Expand Down Expand Up @@ -313,6 +314,9 @@ def _assign_params(
self._assert_mutually_exclusive(params_assigned_from_default, ["SELECT", "INLINE"])
self._assert_mutually_exclusive(params_assigned_from_default, ["SELECTOR", "INLINE"])

# Check event_time configs for validity
self._validate_event_time_configs()

# Support lower cased access for legacy code.
params = set(
x for x in dir(self) if not callable(getattr(self, x)) and not x.startswith("__")
Expand Down Expand Up @@ -349,6 +353,24 @@ def _assert_mutually_exclusive(
elif flag_set_by_user:
set_flag = flag

def _validate_event_time_configs(self) -> None:
event_time_start = (
getattr(self, "EVENT_TIME_START") if hasattr(self, "EVENT_TIME_START") else None
)
if event_time_start is not None:
event_time_end = (

Check warning on line 361 in core/dbt/cli/flags.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/flags.py#L361

Added line #L361 was not covered by tests
getattr(self, "EVENT_TIME_END") if hasattr(self, "EVENT_TIME_END") else None
)

if event_time_end is not None and event_time_start >= event_time_end:
raise DbtUsageException(

Check warning on line 366 in core/dbt/cli/flags.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/flags.py#L365-L366

Added lines #L365 - L366 were not covered by tests
"Value for `--event-time-start` must be less than `--event-time-end`"
)
elif event_time_start >= datetime.now():
raise DbtUsageException(

Check warning on line 370 in core/dbt/cli/flags.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/flags.py#L369-L370

Added lines #L369 - L370 were not covered by tests
"Value for `--event-time-start` must be less than the current time if `--event-time-end` is not specififed"
)

def fire_deprecations(self):
"""Fires events for deprecated env_var usage."""
[dep_fn() for dep_fn in self.deprecated_env_var_warnings]
Expand Down
2 changes: 0 additions & 2 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ def cli(ctx, **kwargs):
@p.vars
@requires.postflight
@requires.preflight
@p.validate_option_interactions
@requires.profile
@requires.project
@requires.runtime_config
Expand Down Expand Up @@ -562,7 +561,6 @@ def parse(ctx, **kwargs):
@p.vars
@requires.postflight
@requires.preflight
@p.validate_option_interactions
@requires.profile
@requires.project
@requires.runtime_config
Expand Down
26 changes: 0 additions & 26 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from datetime import datetime
from functools import update_wrapper
from pathlib import Path

import click

from dbt.cli.option_types import YAML, ChoiceTuple, Package, WarnErrorOptionsType
from dbt.cli.options import MultiOption
from dbt.cli.resolvers import default_profiles_dir, default_project_dir
from dbt.exceptions import DbtRuntimeError
from dbt.version import get_version_information

add_package = click.option(
Expand Down Expand Up @@ -738,26 +735,3 @@ def _version_callback(ctx, _param, value):
envvar="DBT_SHOW_RESOURCE_REPORT",
hidden=True,
)


def validate_option_interactions(func):
def wrapper(*args, **kwargs):
ctx = args[0]
assert isinstance(ctx, click.Context)

event_time_start = ctx.params.get("event_time_start")
if event_time_start is not None:
event_time_end = ctx.params.get("event_time_end")

if event_time_end is not None and event_time_start >= event_time_end:
raise DbtRuntimeError(
"Value for `--event-time-start` must be less than `--event-time-end`"
)
elif event_time_start >= datetime.now():
raise DbtRuntimeError(
"Value for `--event-time-start` must be less than the current time if `--event-time-end` is not specififed"
)

return func(*args, **kwargs)

return update_wrapper(wrapper, func)

0 comments on commit 7b995c5

Please sign in to comment.