11import os
2+ from enum import Enum
23from typing import Any , Dict , Optional , Type
34
45from dbt .version import __version__ as dbt_version_string
56from packaging import version
67
78from elementary .clients .dbt .command_line_dbt_runner import CommandLineDbtRunner
9+ from elementary .clients .dbt .dbt_fusion_runner import DbtFusionRunner
10+ from elementary .clients .dbt .subprocess_dbt_runner import SubprocessDbtRunner
811
912DBT_VERSION = version .Version (dbt_version_string )
1013
11- RUNNER_CLASS : Type [CommandLineDbtRunner ]
12- if (
13- DBT_VERSION >= version .Version ("1.5.0" )
14- and os .getenv ("DBT_RUNNER_METHOD" ) != "subprocess"
15- ):
16- from elementary .clients .dbt .api_dbt_runner import APIDbtRunner
1714
18- RUNNER_CLASS = APIDbtRunner
19- else :
20- from elementary .clients .dbt .subprocess_dbt_runner import SubprocessDbtRunner
21-
22- RUNNER_CLASS = SubprocessDbtRunner
15+ class RunnerMethod (Enum ):
16+ SUBPROCESS = "subprocess"
17+ API = "api"
18+ FUSION = "fusion"
2319
2420
2521def create_dbt_runner (
@@ -33,8 +29,11 @@ def create_dbt_runner(
3329 allow_macros_without_package_prefix : bool = False ,
3430 run_deps_if_needed : bool = True ,
3531 force_dbt_deps : bool = False ,
32+ runner_method : Optional [RunnerMethod ] = None ,
3633) -> CommandLineDbtRunner :
37- return RUNNER_CLASS (
34+ runner_method = runner_method or get_dbt_runner_method ()
35+ runner_class = get_dbt_runner_class (runner_method )
36+ return runner_class (
3837 project_dir = project_dir ,
3938 profiles_dir = profiles_dir ,
4039 target = target ,
@@ -46,3 +45,27 @@ def create_dbt_runner(
4645 run_deps_if_needed = run_deps_if_needed ,
4746 force_dbt_deps = force_dbt_deps ,
4847 )
48+
49+
50+ def get_dbt_runner_method () -> RunnerMethod :
51+ runner_method = os .getenv ("DBT_RUNNER_METHOD" )
52+ if runner_method :
53+ return RunnerMethod (runner_method )
54+
55+ if DBT_VERSION >= version .Version ("1.5.0" ):
56+ return RunnerMethod .API
57+ return RunnerMethod .SUBPROCESS
58+
59+
60+ def get_dbt_runner_class (runner_method : RunnerMethod ) -> Type [CommandLineDbtRunner ]:
61+ if runner_method == RunnerMethod .API :
62+ # Import it internally since it will fail if the dbt version is below 1.5.0
63+ from elementary .clients .dbt .api_dbt_runner import APIDbtRunner
64+
65+ return APIDbtRunner
66+ elif runner_method == RunnerMethod .SUBPROCESS :
67+ return SubprocessDbtRunner
68+ elif runner_method == RunnerMethod .FUSION :
69+ return DbtFusionRunner
70+ else :
71+ raise ValueError (f"Invalid runner method: { runner_method } " )
0 commit comments