33import typing as t
44from pathlib import Path
55
6- from sqlmesh .core .config .connection import ConnectionConfig
76from sqlmesh .dbt .common import PROJECT_FILENAME , DbtContext , load_yaml
87from sqlmesh .dbt .target import TargetConfig
98from sqlmesh .utils .errors import ConfigError
9+ from sqlmesh .utils .yaml import dumps as dump_yaml
1010
1111
1212class Profile :
@@ -19,21 +19,21 @@ class Profile:
1919 def __init__ (
2020 self ,
2121 path : Path ,
22- targets : t . Dict [ str , TargetConfig ] ,
23- default_target : str ,
22+ target_name : str ,
23+ target : TargetConfig ,
2424 ):
2525 """
2626 Args:
2727 path: Path to the profile file
28- targets: Dict of targets defined for the project
29- default_target: Name of the default target for the proejct
28+ target_name: Name of the target loaded
29+ target: TargetConfig for target_name
3030 """
3131 self .path = path
32- self .targets = targets
33- self .default_target = default_target
32+ self .target_name = target_name
33+ self .target = target
3434
3535 @classmethod
36- def load (cls , context : DbtContext ) -> Profile :
36+ def load (cls , context : DbtContext , target_name : t . Optional [ str ] = None ) -> Profile :
3737 """
3838 Loads the profile for the specified project
3939
@@ -59,8 +59,8 @@ def load(cls, context: DbtContext) -> Profile:
5959 if not profile_filepath :
6060 raise ConfigError (f"{ cls .PROFILE_FILE } not found." )
6161
62- targets , default_target = cls ._read_profile (profile_filepath , context )
63- return Profile (profile_filepath , targets , default_target )
62+ target_name , target = cls ._read_profile (profile_filepath , context , target_name )
63+ return Profile (profile_filepath , target_name , target )
6464
6565 @classmethod
6666 def _find_profile (cls , project_root : Path ) -> t .Optional [Path ]:
@@ -77,28 +77,27 @@ def _find_profile(cls, project_root: Path) -> t.Optional[Path]:
7777
7878 @classmethod
7979 def _read_profile (
80- cls , path : Path , context : DbtContext
81- ) -> t .Tuple [t .Dict [str , TargetConfig ], str ]:
82- with open (path , "r" , encoding = "utf-8" ) as file :
83- source = file .read ()
84- contents = load_yaml (context .render (source ))
85-
86- project_data = contents .get (context .profile_name )
80+ cls , path : Path , context : DbtContext , target_name : t .Optional [str ] = None
81+ ) -> t .Tuple [str , TargetConfig ]:
82+ project_data = load_yaml (path ).get (context .profile_name )
8783 if not project_data :
8884 raise ConfigError (f"Profile '{ context .profile_name } ' not found in profiles." )
8985
9086 outputs = project_data .get ("outputs" )
9187 if not outputs :
9288 raise ConfigError (f"No outputs exist in profiles for '{ context .profile_name } '." )
9389
94- targets = {name : TargetConfig .load (name , output ) for name , output in outputs .items ()}
95- default_target = context .render (project_data .get ("target" ))
96- if default_target not in targets :
90+ if not target_name :
91+ if "target" not in project_data :
92+ raise ConfigError (f"No target specified for '{ context .profile_name } '." )
93+ target_name = context .render (project_data .get ("target" ))
94+
95+ if target_name not in outputs :
9796 raise ConfigError (
98- f"Default target ' { default_target } ' not specified in profiles for '{ context .profile_name } '."
97+ f"Target ' { target_name } ' not specified in profiles for '{ context .profile_name } '."
9998 )
10099
101- return (targets , default_target )
100+ target_fields = load_yaml (context .render (dump_yaml (outputs [target_name ])))
101+ target = TargetConfig .load (target_name , target_fields )
102102
103- def to_sqlmesh (self ) -> t .Dict [str , ConnectionConfig ]:
104- return {self .default_target : self .targets [self .default_target ].to_sqlmesh ()}
103+ return (target_name , target )
0 commit comments