Skip to content

Commit

Permalink
Fix microbatch dbt list --output JSON
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
internetcoffeephone committed Jan 9, 2025
1 parent 57e279c commit f8b0760
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20250109-123309.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix microbatch dbt list --output json
time: 2025-01-09T12:33:09.958795+01:00
custom:
Author: internetcoffeephone
Issue: 10556 11098
4 changes: 3 additions & 1 deletion core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/microbatch/test_microbatch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from unittest import mock

import pytest
Expand All @@ -15,6 +16,7 @@
MicrobatchMacroOutsideOfBatchesDeprecation,
MicrobatchModelNoEventTimeInputs,
)
from dbt.tests.fixtures.project import TestProjInfo
from dbt.tests.util import (
get_artifact,
patch_microbatch_end_time,
Expand Down Expand Up @@ -333,6 +335,19 @@ class TestMicrobatchCLIBuild(TestMicrobatchCLI):
CLI_COMMAND_NAME = "build"


class TestMicrobatchCLIRunOutputJSON(BaseMicrobatchTest):
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 TestMicroBatchBoundsDefault(BaseMicrobatchTest):
def test_run_with_event_time(self, project):
# initial run -- backfills all data
Expand Down

0 comments on commit f8b0760

Please sign in to comment.