77import abc
88
99from dataclasses import dataclass
10+ from sqlglot import exp
1011
1112from sqlmesh .core .console import Console
1213from sqlmesh .core .dialect import schema_
@@ -29,7 +30,7 @@ def cleanup_expired_views(
2930 warn_on_delete_failure : bool = False ,
3031 console : t .Optional [Console ] = None ,
3132) -> None :
32- expired_schema_environments = [
33+ expired_schema_or_catalog_environments = [
3334 environment
3435 for environment in environments
3536 if environment .suffix_target .is_schema or environment .suffix_target .is_catalog
@@ -45,8 +46,9 @@ def get_adapter(gateway_managed: bool, gateway: t.Optional[str] = None) -> Engin
4546 return default_adapter
4647
4748 catalogs_to_drop : t .Set [t .Tuple [EngineAdapter , str ]] = set ()
49+ schemas_to_drop : t .Set [t .Tuple [EngineAdapter , exp .Table ]] = set ()
4850
49- # Drop the schemas for the expired environments
51+ # Collect schemas and catalogs to drop
5052 for engine_adapter , expired_catalog , expired_schema , suffix_target in {
5153 (
5254 (engine_adapter := get_adapter (environment .gateway_managed , snapshot .model_gateway )),
@@ -58,29 +60,16 @@ def get_adapter(gateway_managed: bool, gateway: t.Optional[str] = None) -> Engin
5860 ),
5961 environment .suffix_target ,
6062 )
61- for environment in expired_schema_environments
63+ for environment in expired_schema_or_catalog_environments
6264 for snapshot in environment .snapshots
6365 if snapshot .is_model and not snapshot .is_symbolic
6466 }:
65- schema = schema_ (expired_schema , expired_catalog )
66- try :
67- engine_adapter .drop_schema (
68- schema ,
69- ignore_if_not_exists = True ,
70- cascade = True ,
71- )
72-
73- if suffix_target .is_catalog and expired_catalog :
67+ if suffix_target .is_catalog :
68+ if expired_catalog :
7469 catalogs_to_drop .add ((engine_adapter , expired_catalog ))
75-
76- if console :
77- console .update_cleanup_progress (schema .sql (dialect = engine_adapter .dialect ))
78- except Exception as e :
79- message = f"Failed to drop the expired environment schema '{ schema } ': { e } "
80- if warn_on_delete_failure :
81- logger .warning (message )
82- else :
83- raise SQLMeshError (message ) from e
70+ else :
71+ schema = schema_ (expired_schema , expired_catalog )
72+ schemas_to_drop .add ((engine_adapter , schema ))
8473
8574 # Drop the views for the expired environments
8675 for engine_adapter , expired_view in {
@@ -105,6 +94,23 @@ def get_adapter(gateway_managed: bool, gateway: t.Optional[str] = None) -> Engin
10594 else :
10695 raise SQLMeshError (message ) from e
10796
97+ # Drop the schemas for the expired environments
98+ for engine_adapter , schema in schemas_to_drop :
99+ try :
100+ engine_adapter .drop_schema (
101+ schema ,
102+ ignore_if_not_exists = True ,
103+ cascade = True ,
104+ )
105+ if console :
106+ console .update_cleanup_progress (schema .sql (dialect = engine_adapter .dialect ))
107+ except Exception as e :
108+ message = f"Failed to drop the expired environment schema '{ schema } ': { e } "
109+ if warn_on_delete_failure :
110+ logger .warning (message )
111+ else :
112+ raise SQLMeshError (message ) from e
113+
108114 # Drop any catalogs that were associated with a snapshot where the engine adapter supports dropping catalogs
109115 # catalogs_to_drop is only populated when environment_suffix_target is set to 'catalog'
110116 for engine_adapter , catalog in catalogs_to_drop :
0 commit comments