Skip to content

Commit

Permalink
fix(dbt): Avoid raising an error when syncing specific models (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitor-Avila committed May 1, 2024
1 parent b87dd7c commit e1ed229
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/preset_cli/api/clients/dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ def get_models(self, job_id: int) -> List[ModelSchema]:
name
description
type
meta
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/preset_cli/cli/superset/sync/dbt/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def get_sl_metric(
sql = output[start:]

models = get_models_from_sql(sql, dialect, model_map)
if len(models) > 1:
if not models or len(models) > 1:
return None
model = models[0]

Expand Down Expand Up @@ -427,7 +427,7 @@ def fetch_sl_metrics(
continue

models = get_models_from_sql(sql, dialect, model_map)
if len(models) > 1:
if not models or len(models) > 1:
continue
model = models[0]

Expand Down
4 changes: 2 additions & 2 deletions src/preset_cli/cli/superset/sync/dbt/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def get_models_from_sql(
sql: str,
dialect: MFSQLEngine,
model_map: Dict[ModelKey, ModelSchema],
) -> List[ModelSchema]:
) -> Optional[List[ModelSchema]]:
"""
Return the model associated with a SQL query.
"""
Expand All @@ -390,7 +390,7 @@ def get_models_from_sql(

for table in sources:
if ModelKey(table.db, table.name) not in model_map:
raise ValueError(f"Unable to find model for SQL source {table}")
return None

return [model_map[ModelKey(table.db, table.name)] for table in sources]

Expand Down
6 changes: 3 additions & 3 deletions tests/cli/superset/sync/dbt/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,9 @@ def test_get_models_from_sql() -> None:
model_map, # type: ignore
) == [{"name": "a"}, {"name": "b"}]

with pytest.raises(ValueError) as excinfo:
get_models_from_sql("SELECT 1 FROM schema.c", MFSQLEngine.BIGQUERY, {})
assert str(excinfo.value) == "Unable to find model for SQL source schema.c"
assert (
get_models_from_sql("SELECT 1 FROM schema.c", MFSQLEngine.BIGQUERY, {}) is None
)


def test_get_superset_metrics_per_model() -> None:
Expand Down

0 comments on commit e1ed229

Please sign in to comment.