From 704cecae5ee41143d1eb250ed3da4f311b934b44 Mon Sep 17 00:00:00 2001 From: internetcoffeephone Date: Mon, 6 Jan 2025 18:31:18 +0100 Subject: [PATCH] Fix microbatch dbt list --output JSON Currently, running this command on a project containing a microbatch model results in an error, as microbatch models require a datetime value in their config which cannot be serialized by the default JSON serializer. There already exists a custom JSON serializer within the dbt-core project that converts datetime to ISO string format. This change uses the above serializer to resolve the error. --- .changes/unreleased/Fixes-20250107-105738.yaml | 6 ++++++ core/dbt/task/list.py | 4 +++- tests/functional/microbatch/test_microbatch.py | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Fixes-20250107-105738.yaml diff --git a/.changes/unreleased/Fixes-20250107-105738.yaml b/.changes/unreleased/Fixes-20250107-105738.yaml new file mode 100644 index 00000000000..990ed864fbe --- /dev/null +++ b/.changes/unreleased/Fixes-20250107-105738.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix microbatch dbt list --output json +time: 2025-01-07T10:57:38.985761+01:00 +custom: + Author: internetcoffeephone + Issue: "11098" diff --git a/core/dbt/task/list.py b/core/dbt/task/list.py index 2638920976a..13b7388d1a1 100644 --- a/core/dbt/task/list.py +++ b/core/dbt/task/list.py @@ -17,6 +17,7 @@ from dbt.node_types import NodeType from dbt.task.base import resource_types_from_args from dbt.task.runnable import GraphRunnableTask +from dbt.utils import JSONEncoder from dbt_common.events.contextvars import task_contextvars from dbt_common.events.functions import fire_event, warn_or_error from dbt_common.events.types import PrintEvent @@ -142,7 +143,8 @@ def generate_json(self): if self.args.output_keys else k in self.ALLOWED_KEYS ) - } + }, + cls=JSONEncoder, ) def generate_paths(self) -> Iterator[str]: diff --git a/tests/functional/microbatch/test_microbatch.py b/tests/functional/microbatch/test_microbatch.py index 953b372b226..98c714869d9 100644 --- a/tests/functional/microbatch/test_microbatch.py +++ b/tests/functional/microbatch/test_microbatch.py @@ -1,3 +1,4 @@ +import json from unittest import mock import pytest @@ -15,6 +16,7 @@ MicrobatchMacroOutsideOfBatchesDeprecation, MicrobatchModelNoEventTimeInputs, ) +from dbt.tests.fixtures.project import TestProjInfo from dbt.tests.util import ( get_artifact, patch_microbatch_end_time, @@ -328,6 +330,17 @@ def test_run_with_event_time(self, project): ) self.assert_row_count(project, "microbatch_model", 1) + def test_list_output_json(self, project: TestProjInfo): + """Test whether the command `dbt list --output json` works""" + model_catcher = EventCatcher(event_to_catch=LogModelResult) + batch_catcher = EventCatcher(event_to_catch=LogBatchResult) + + _, microbatch_json = run_dbt( + ["list", "--output", "json"], callbacks=[model_catcher.catch, batch_catcher.catch] + ) + microbatch_dict = json.loads(microbatch_json) + assert microbatch_dict["config"]["begin"] == "2020-01-01T00:00:00" + class TestMicrobatchCLIBuild(TestMicrobatchCLI): CLI_COMMAND_NAME = "build"