@@ -239,11 +239,11 @@ def _load_requirements(self) -> t.Tuple[t.Dict[str, str], t.Set[str]]:
239239 def _load_environment_statements (self , macros : MacroRegistry ) -> t .List [EnvironmentStatements ]:
240240 """Loads dbt's on_run_start, on_run_end hooks into sqlmesh's before_all, after_all statements respectively."""
241241
242- environment_statements : t .List [EnvironmentStatements ] = []
242+ hooks_by_package_name : t .Dict [str , EnvironmentStatements ] = {}
243+ first_project_name : t .Optional [str ] = None
243244 dialect = self .config .dialect
244245 for project in self ._load_projects ():
245246 context = project .context
246- hooks_by_package_name : t .Dict [str , EnvironmentStatements ] = {}
247247 for package_name , package in project .packages .items ():
248248 context .set_and_render_variables (package .variables , package_name )
249249 on_run_start : t .List [str ] = [
@@ -278,15 +278,17 @@ def _load_environment_statements(self, macros: MacroRegistry) -> t.List[Environm
278278 python_env = {},
279279 jinja_macros = jinja_registry ,
280280 )
281- # Project hooks should be executed first and then rest of the packages
282- environment_statements = [
283- statements
284- for _ , statements in sorted (
285- hooks_by_package_name .items (),
286- key = lambda item : 0 if item [0 ] == context .project_name else 1 ,
287- )
288- ]
289- return environment_statements
281+
282+ if not first_project_name :
283+ first_project_name = package_name
284+
285+ return [
286+ statements
287+ for _ , statements in sorted (
288+ hooks_by_package_name .items (),
289+ key = lambda item : 0 if item [0 ] == first_project_name else 1 ,
290+ )
291+ ]
290292
291293 def _compute_yaml_max_mtime_per_subfolder (self , root : Path ) -> t .Dict [Path , float ]:
292294 if not root .is_dir ():
0 commit comments