4
4
from sqlmesh_dbt .operations import DbtOperations , create
5
5
from sqlmesh_dbt .error import cli_global_error_handler
6
6
from pathlib import Path
7
+ from sqlmesh_dbt .options import YamlParamType
8
+ import functools
7
9
8
10
9
- def _get_dbt_operations (ctx : click .Context ) -> DbtOperations :
10
- if not isinstance (ctx .obj , DbtOperations ):
11
+ def _get_dbt_operations (ctx : click .Context , vars : t . Optional [ t . Dict [ str , t . Any ]] ) -> DbtOperations :
12
+ if not isinstance (ctx .obj , functools . partial ):
11
13
raise ValueError (f"Unexpected click context object: { type (ctx .obj )} " )
12
- return ctx .obj
14
+
15
+ dbt_operations = ctx .obj (vars = vars )
16
+
17
+ if not isinstance (dbt_operations , DbtOperations ):
18
+ raise ValueError (f"Unexpected dbt operations type: { type (dbt_operations )} " )
19
+
20
+ @ctx .call_on_close
21
+ def _cleanup () -> None :
22
+ dbt_operations .close ()
23
+
24
+ return dbt_operations
25
+
26
+
27
+ vars_option = click .option (
28
+ "--vars" ,
29
+ type = YamlParamType (),
30
+ help = "Supply variables to the project. This argument overrides variables defined in your dbt_project.yml file. This argument should be a YAML string, eg. '{my_variable: my_value}'" ,
31
+ )
13
32
14
33
15
34
select_option = click .option (
@@ -40,10 +59,15 @@ def dbt(
40
59
# we dont need to import sqlmesh/load the project for CLI help
41
60
return
42
61
43
- # TODO: conditionally call create() if there are times we dont want/need to import sqlmesh and load a project
44
- ctx .obj = create (project_dir = Path .cwd (), profile = profile , target = target )
62
+ # we have a partially applied function here because subcommands might set extra options like --vars
63
+ # that need to be known before we attempt to load the project
64
+ ctx .obj = functools .partial (create , project_dir = Path .cwd (), profile = profile , target = target )
45
65
46
66
if not ctx .invoked_subcommand :
67
+ if profile or target :
68
+ # trigger a project load to validate the specified profile / target
69
+ ctx .obj ()
70
+
47
71
click .echo (
48
72
f"No command specified. Run `{ ctx .info_name } --help` to see the available commands."
49
73
)
@@ -57,19 +81,21 @@ def dbt(
57
81
"--full-refresh" ,
58
82
help = "If specified, dbt will drop incremental models and fully-recalculate the incremental table from the model definition." ,
59
83
)
84
+ @vars_option
60
85
@click .pass_context
61
- def run (ctx : click .Context , ** kwargs : t .Any ) -> None :
86
+ def run (ctx : click .Context , vars : t . Optional [ t . Dict [ str , t . Any ]], ** kwargs : t .Any ) -> None :
62
87
"""Compile SQL and execute against the current target database."""
63
- _get_dbt_operations (ctx ).run (** kwargs )
88
+ _get_dbt_operations (ctx , vars ).run (** kwargs )
64
89
65
90
66
91
@dbt .command (name = "list" )
67
92
@select_option
68
93
@exclude_option
94
+ @vars_option
69
95
@click .pass_context
70
- def list_ (ctx : click .Context , ** kwargs : t .Any ) -> None :
96
+ def list_ (ctx : click .Context , vars : t . Optional [ t . Dict [ str , t . Any ]], ** kwargs : t .Any ) -> None :
71
97
"""List the resources in your project"""
72
- _get_dbt_operations (ctx ).list_ (** kwargs )
98
+ _get_dbt_operations (ctx , vars ).list_ (** kwargs )
73
99
74
100
75
101
@dbt .command (name = "ls" , hidden = True ) # hidden alias for list
0 commit comments