Skip to content

Commit

Permalink
fix(dbt Core): Support syncing metrics without a MetricFlow dialect (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitor-Avila authored Jun 21, 2024
1 parent 7ed1b97 commit 7e3caac
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/preset_cli/cli/superset/sync/dbt/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
)
@raise_cli_errors
@click.pass_context
def dbt_core( # pylint: disable=too-many-arguments, too-many-branches, too-many-locals ,too-many-statements
def dbt_core( # pylint: disable=too-many-arguments, too-many-branches, too-many-locals ,too-many-statements # noqa: C901
ctx: click.core.Context,
file: str,
project: Optional[str],
Expand Down Expand Up @@ -186,7 +186,10 @@ def dbt_core( # pylint: disable=too-many-arguments, too-many-branches, too-many

config = load_profiles(Path(profiles), project, profile, target)
dialect = config[project]["outputs"][target]["type"]
mf_dialect = MFSQLEngine(dialect.upper())
try:
mf_dialect = MFSQLEngine(dialect.upper())
except ValueError:
mf_dialect = None

model_schema = ModelSchema()
models = []
Expand Down Expand Up @@ -219,7 +222,10 @@ def dbt_core( # pylint: disable=too-many-arguments, too-many-branches, too-many
config["uniqueId"] = config.pop("unique_id")
config["dialect"] = dialect
og_metrics.append(metric_schema.load(config))
elif sl_metric := get_sl_metric(config, model_map, mf_dialect):
# Only validate semantic layer metrics if MF dialect is specified
elif mf_dialect is not None and (
sl_metric := get_sl_metric(config, model_map, mf_dialect)
):
sl_metrics.append(sl_metric)

superset_metrics = get_superset_metrics_per_model(og_metrics, sl_metrics)
Expand Down
54 changes: 54 additions & 0 deletions tests/cli/superset/sync/dbt/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
},
},
},
"athena_project": {
"outputs": {
"dev": {
"type": "athena",
},
},
},
},
)

Expand Down Expand Up @@ -1671,6 +1678,53 @@ def test_dbt_core_metricflow_not_found(
)


def test_dbt_core_metricflow_dialect_not_found(
mocker: MockerFixture,
fs: FakeFilesystem,
) -> None:
"""
Test the ``dbt-core`` command when the project dialect is not
compatible with MetricFlow.
"""
root = Path("/path/to/root")
fs.create_dir(root)
manifest = root / "default/target/manifest.json"
fs.create_file(manifest, contents=manifest_metricflow_contents)
profiles = root / ".dbt/profiles.yml"
fs.create_file(profiles, contents=profiles_contents)

mocker.patch(
"preset_cli.cli.superset.sync.dbt.command.SupersetClient",
)
mocker.patch("preset_cli.cli.superset.main.UsernamePasswordAuth")
mocker.patch(
"preset_cli.cli.superset.sync.dbt.command.sync_database",
)
get_sl_metric_mock = mocker.patch(
"preset_cli.cli.superset.sync.dbt.command.get_sl_metric",
)

runner = CliRunner()
result = runner.invoke(
superset_cli,
[
"https://superset.example.org/",
"sync",
"dbt-core",
str(manifest),
"--profiles",
str(profiles),
"--project",
"athena_project",
"--target",
"dev",
],
catch_exceptions=False,
)
assert result.exit_code == 0
get_sl_metric_mock.assert_not_called()


def test_dbt_core_preserve_metadata(
mocker: MockerFixture,
fs: FakeFilesystem,
Expand Down

0 comments on commit 7e3caac

Please sign in to comment.