@@ -840,7 +840,50 @@ def run_janitor(self, ignore_ttl: bool) -> bool:
840840 def destroy (self ) -> bool :
841841 success = False
842842
843- if self .console .start_destroy ():
843+ # Collect resources to be deleted
844+ environments = self .state_reader .get_environments ()
845+ schemas_to_delete = set ()
846+ tables_to_delete = set ()
847+ views_to_delete = set ()
848+ all_snapshot_infos = set ()
849+
850+ # For each environment find schemas and tables
851+ for environment in environments :
852+ all_snapshot_infos .update (environment .snapshots )
853+ snapshots = self .state_reader .get_snapshots (environment .snapshots ).values ()
854+ for snapshot in snapshots :
855+ if snapshot .is_model and not snapshot .is_symbolic :
856+ # Get the appropriate adapter
857+ if environment .gateway_managed and snapshot .model_gateway :
858+ adapter = self .engine_adapters .get (
859+ snapshot .model_gateway , self .engine_adapter
860+ )
861+ else :
862+ adapter = self .engine_adapter
863+
864+ if environment .suffix_target .is_schema or environment .suffix_target .is_catalog :
865+ schema = snapshot .qualified_view_name .schema_for_environment (
866+ environment .naming_info , dialect = adapter .dialect
867+ )
868+ catalog = snapshot .qualified_view_name .catalog_for_environment (
869+ environment .naming_info , dialect = adapter .dialect
870+ )
871+ if catalog :
872+ schemas_to_delete .add (f"{ catalog } .{ schema } " )
873+ else :
874+ schemas_to_delete .add (schema )
875+
876+ if environment .suffix_target .is_table :
877+ view_name = snapshot .qualified_view_name .for_environment (
878+ environment .naming_info , dialect = adapter .dialect
879+ )
880+ views_to_delete .add (view_name )
881+
882+ # Add snapshot tables
883+ table_name = snapshot .table_name ()
884+ tables_to_delete .add (table_name )
885+
886+ if self .console .start_destroy (schemas_to_delete , views_to_delete , tables_to_delete ):
844887 try :
845888 success = self ._destroy ()
846889 finally :
@@ -2705,80 +2748,6 @@ def _context_diff(
27052748 )
27062749
27072750 def _destroy (self ) -> bool :
2708- environments = self .state_reader .get_environments ()
2709-
2710- schemas_to_delete = set ()
2711- tables_to_delete = set ()
2712- views_to_delete = set ()
2713- all_snapshot_infos = set ()
2714-
2715- # For each environment find schemas and tables
2716- for environment in environments :
2717- all_snapshot_infos .update (environment .snapshots )
2718- snapshots = self .state_reader .get_snapshots (environment .snapshots ).values ()
2719- for snapshot in snapshots :
2720- if snapshot .is_model and not snapshot .is_symbolic :
2721- # Get the appropriate adapter
2722- if environment .gateway_managed and snapshot .model_gateway :
2723- adapter = self .engine_adapters .get (
2724- snapshot .model_gateway , self .engine_adapter
2725- )
2726- else :
2727- adapter = self .engine_adapter
2728-
2729- if environment .suffix_target .is_schema or environment .suffix_target .is_catalog :
2730- schema = snapshot .qualified_view_name .schema_for_environment (
2731- environment .naming_info , dialect = adapter .dialect
2732- )
2733- catalog = snapshot .qualified_view_name .catalog_for_environment (
2734- environment .naming_info , dialect = adapter .dialect
2735- )
2736- if catalog :
2737- schemas_to_delete .add (f"{ catalog } .{ schema } " )
2738- else :
2739- schemas_to_delete .add (schema )
2740-
2741- if environment .suffix_target .is_table :
2742- view_name = snapshot .qualified_view_name .for_environment (
2743- environment .naming_info , dialect = adapter .dialect
2744- )
2745- views_to_delete .add (view_name )
2746-
2747- # Add snapshot tables
2748- table_name = snapshot .table_name ()
2749- tables_to_delete .add (table_name )
2750-
2751- # Display what will be deleted
2752- self .console .log_error ("\n " + "=" * 50 + "\n " )
2753- if schemas_to_delete :
2754- self .console .log_error ("Schemas to be deleted:" )
2755- for schema in sorted (schemas_to_delete ):
2756- self .console .log_error (f" • { schema } " )
2757-
2758- if views_to_delete :
2759- self .console .log_error ("\n Environment views to be deleted:" )
2760- for view in sorted (views_to_delete ):
2761- self .console .log_error (f" • { view } " )
2762-
2763- if tables_to_delete :
2764- self .console .log_error ("\n Snapshot tables to be deleted:" )
2765- for table in sorted (tables_to_delete ):
2766- self .console .log_error (f" • { table } " )
2767-
2768- self .console .log_error ("\n All SQLMesh state tables will be deleted" )
2769- self .console .log_error ("\n " + "=" * 50 + "\n " )
2770-
2771- # Final confirmation with stronger warning
2772- self .console .log_error (
2773- "!!! CRITICAL WARNING: This action will PERMANENTLY DELETE ALL the above resources!\n "
2774- "This includes ALL tables, views and schemas managed by SQLMesh AND potentially\n "
2775- "external resources created by other tools in these schemas. This action is IRREVERSIBLE!\n "
2776- )
2777-
2778- if not self .console ._confirm ("Are you ABSOLUTELY SURE you want to proceed with deletion?" ): # type: ignore
2779- self .console .log_error ("Destroy operation cancelled." )
2780- return False
2781-
27822751 # Invalidate all environments, including prod
27832752 for environment in self .state_reader .get_environments ():
27842753 self .state_sync .invalidate_environment (name = environment .name , protect_prod = False )
0 commit comments