Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 40e62dc

Browse files
authored
Merge pull request #682 from datafold/LAB-135
partial --select support for dbt < 1.5
2 parents 2327d44 + 3b6a1c4 commit 40e62dc

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

data_diff/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ def write_usage(self, prog: str, args: str = "", prefix: Optional[str] = None) -
253253
"--select",
254254
"-s",
255255
default=None,
256-
metavar="PATH",
257-
help="select dbt resources to compare using dbt selection syntax.",
256+
metavar="SELECTION or MODEL_NAME",
257+
help="--select dbt resources to compare using dbt selection syntax in dbt versions >= 1.5.\nIn versions < 1.5, it will naively search for a model with MODEL_NAME as the name.",
258258
)
259259
@click.option(
260260
"--state",

data_diff/dbt_parser.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
DataDiffDbtRunResultsVersionError,
2121
DataDiffDbtSelectNoMatchingModelsError,
2222
DataDiffDbtSelectUnexpectedError,
23-
DataDiffDbtSelectVersionTooLowError,
2423
DataDiffDbtSnowflakeSetConnectionError,
24+
DataDiffSimpleSelectNotFound,
2525
)
2626

2727
from .utils import getLogger, get_from_dict_with_raise
@@ -167,9 +167,11 @@ def get_models(self, dbt_selection: Optional[str] = None):
167167
"data-diff is using a dbt-core version < 1.5, update the environment's dbt-core version via pip install 'dbt-core>=1.5' in order to use `--select`"
168168
)
169169
else:
170-
raise DataDiffDbtSelectVersionTooLowError(
171-
f"The `--select` feature requires dbt >= 1.5, but your project's manifest.json is from dbt v{dbt_version}. Please follow these steps to use the `--select` feature: \n 1. Update your dbt-core version via pip install 'dbt-core>=1.5'. Details: https://docs.getdbt.com/docs/core/pip-install#change-dbt-core-versions \n 2. Execute any `dbt` command (`run`, `compile`, `build`) to create a new manifest.json."
170+
# Naively get node named <dbt_selection>
171+
logger.warning(
172+
f"Full `--select` support requires dbt >= 1.5. Naively searching for a single model with name: '{dbt_selection}'."
172173
)
174+
return self.get_simple_model_selection(dbt_selection)
173175
else:
174176
return self.get_run_results_models()
175177

@@ -209,6 +211,25 @@ def get_dbt_selection_models(self, dbt_selection: str) -> List[str]:
209211
logger.debug(str(results))
210212
raise DataDiffDbtSelectUnexpectedError("Encountered an unexpected error while finding `--select` models")
211213

214+
def get_simple_model_selection(self, dbt_selection: str):
215+
model_nodes = dict(filter(lambda item: item[0].startswith("model."), self.dev_manifest_obj.nodes.items()))
216+
217+
model_unique_key_list = [k for k, v in model_nodes.items() if v.name == dbt_selection]
218+
219+
# name *should* always be unique, but just in case:
220+
if len(model_unique_key_list) > 1:
221+
logger.warning(
222+
f"Found more than one model with name '{dbt_selection}' {model_unique_key_list}, using the first one."
223+
)
224+
elif len(model_unique_key_list) < 1:
225+
raise DataDiffSimpleSelectNotFound(
226+
f"Did not find a model node with name '{dbt_selection}' in the manifest."
227+
)
228+
229+
model = model_nodes.get(model_unique_key_list[0])
230+
231+
return [model]
232+
212233
def get_run_results_models(self):
213234
with open(self.project_dir / RUN_RESULTS_PATH) as run_results:
214235
logger.info(f"Parsing file {RUN_RESULTS_PATH}")

data_diff/errors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ class DataDiffDbtCoreNoRunnerError(Exception):
4242
"Raised when the manifest version >= 1.5, but the dbt-core package is < 1.5. This is an edge case most likely to occur in development."
4343

4444

45-
class DataDiffDbtSelectVersionTooLowError(Exception):
46-
"Raised when attempting to use `--select` with a dbt-core version < 1.5."
47-
48-
4945
class DataDiffCustomSchemaNoConfigError(Exception):
5046
"Raised when a model has a custom schema, but there is no prod_custom_schema config. (And not using --state)."
5147

@@ -68,3 +64,7 @@ class DataDiffCloudDiffFailed(Exception):
6864

6965
class DataDiffCloudDiffTimedOut(Exception):
7066
"Raised when using --cloud and the diff did not return finish before the timeout value."
67+
68+
69+
class DataDiffSimpleSelectNotFound(Exception):
70+
"Raised when using --select on dbt < 1.5 and a model node is not found in the manifest."

tests/test_dbt_parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
DataDiffDbtProfileNotFoundError,
1111
DataDiffDbtRedshiftPasswordOnlyError,
1212
DataDiffDbtRunResultsVersionError,
13-
DataDiffDbtSelectVersionTooLowError,
1413
DataDiffDbtSnowflakeSetConnectionError,
1514
)
1615

@@ -56,17 +55,18 @@ def test_get_models(self):
5655
mock_self.get_dbt_selection_models.assert_called_once_with(selection)
5756
self.assertEqual(models, mock_return_value)
5857

59-
def test_get_models_unsupported_manifest_version(self):
58+
def test_get_models_simple_select(self):
6059
mock_self = Mock()
6160
mock_self.project_dir = Path()
6261
mock_self.dbt_version = "1.4.0"
6362
selection = "model+"
6463
mock_return_value = Mock()
65-
mock_self.get_dbt_selection_models.return_value = mock_return_value
64+
mock_self.get_simple_model_selection.return_value = mock_return_value
6665

67-
with self.assertRaises(DataDiffDbtSelectVersionTooLowError):
68-
_ = DbtParser.get_models(mock_self, selection)
66+
models = DbtParser.get_models(mock_self, selection)
6967
mock_self.get_dbt_selection_models.assert_not_called()
68+
mock_self.get_simple_model_selection.assert_called_with(selection)
69+
self.assertEqual(models, mock_return_value)
7070

7171
def test_get_models_no_runner(self):
7272
mock_self = Mock()

0 commit comments

Comments
 (0)