44from sqlmesh_dbt .operations import DbtOperations , create
55from sqlmesh_dbt .error import cli_global_error_handler
66from pathlib import Path
7+ from sqlmesh_dbt .options import YamlParamType
8+ import functools
79
810
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 ):
1113 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+ )
1332
1433
1534select_option = click .option (
@@ -40,10 +59,15 @@ def dbt(
4059 # we dont need to import sqlmesh/load the project for CLI help
4160 return
4261
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 )
4565
4666 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+
4771 click .echo (
4872 f"No command specified. Run `{ ctx .info_name } --help` to see the available commands."
4973 )
@@ -57,19 +81,21 @@ def dbt(
5781 "--full-refresh" ,
5882 help = "If specified, dbt will drop incremental models and fully-recalculate the incremental table from the model definition." ,
5983)
84+ @vars_option
6085@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 :
6287 """Compile SQL and execute against the current target database."""
63- _get_dbt_operations (ctx ).run (** kwargs )
88+ _get_dbt_operations (ctx , vars ).run (** kwargs )
6489
6590
6691@dbt .command (name = "list" )
6792@select_option
6893@exclude_option
94+ @vars_option
6995@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 :
7197 """List the resources in your project"""
72- _get_dbt_operations (ctx ).list_ (** kwargs )
98+ _get_dbt_operations (ctx , vars ).list_ (** kwargs )
7399
74100
75101@dbt .command (name = "ls" , hidden = True ) # hidden alias for list
0 commit comments