@@ -858,7 +858,50 @@ def run_janitor(self, ignore_ttl: bool) -> bool:
858858 def destroy (self ) -> bool :
859859 success = False
860860
861- if self .console .start_destroy ():
861+ # Collect resources to be deleted
862+ environments = self .state_reader .get_environments ()
863+ schemas_to_delete = set ()
864+ tables_to_delete = set ()
865+ views_to_delete = set ()
866+ all_snapshot_infos = set ()
867+
868+ # For each environment find schemas and tables
869+ for environment in environments :
870+ all_snapshot_infos .update (environment .snapshots )
871+ snapshots = self .state_reader .get_snapshots (environment .snapshots ).values ()
872+ for snapshot in snapshots :
873+ if snapshot .is_model and not snapshot .is_symbolic :
874+ # Get the appropriate adapter
875+ if environment .gateway_managed and snapshot .model_gateway :
876+ adapter = self .engine_adapters .get (
877+ snapshot .model_gateway , self .engine_adapter
878+ )
879+ else :
880+ adapter = self .engine_adapter
881+
882+ if environment .suffix_target .is_schema or environment .suffix_target .is_catalog :
883+ schema = snapshot .qualified_view_name .schema_for_environment (
884+ environment .naming_info , dialect = adapter .dialect
885+ )
886+ catalog = snapshot .qualified_view_name .catalog_for_environment (
887+ environment .naming_info , dialect = adapter .dialect
888+ )
889+ if catalog :
890+ schemas_to_delete .add (f"{ catalog } .{ schema } " )
891+ else :
892+ schemas_to_delete .add (schema )
893+
894+ if environment .suffix_target .is_table :
895+ view_name = snapshot .qualified_view_name .for_environment (
896+ environment .naming_info , dialect = adapter .dialect
897+ )
898+ views_to_delete .add (view_name )
899+
900+ # Add snapshot tables
901+ table_name = snapshot .table_name ()
902+ tables_to_delete .add (table_name )
903+
904+ if self .console .start_destroy (schemas_to_delete , views_to_delete , tables_to_delete ):
862905 try :
863906 success = self ._destroy ()
864907 finally :
@@ -2723,80 +2766,6 @@ def _context_diff(
27232766 )
27242767
27252768 def _destroy (self ) -> bool :
2726- environments = self .state_reader .get_environments ()
2727-
2728- schemas_to_delete = set ()
2729- tables_to_delete = set ()
2730- views_to_delete = set ()
2731- all_snapshot_infos = set ()
2732-
2733- # For each environment find schemas and tables
2734- for environment in environments :
2735- all_snapshot_infos .update (environment .snapshots )
2736- snapshots = self .state_reader .get_snapshots (environment .snapshots ).values ()
2737- for snapshot in snapshots :
2738- if snapshot .is_model and not snapshot .is_symbolic :
2739- # Get the appropriate adapter
2740- if environment .gateway_managed and snapshot .model_gateway :
2741- adapter = self .engine_adapters .get (
2742- snapshot .model_gateway , self .engine_adapter
2743- )
2744- else :
2745- adapter = self .engine_adapter
2746-
2747- if environment .suffix_target .is_schema or environment .suffix_target .is_catalog :
2748- schema = snapshot .qualified_view_name .schema_for_environment (
2749- environment .naming_info , dialect = adapter .dialect
2750- )
2751- catalog = snapshot .qualified_view_name .catalog_for_environment (
2752- environment .naming_info , dialect = adapter .dialect
2753- )
2754- if catalog :
2755- schemas_to_delete .add (f"{ catalog } .{ schema } " )
2756- else :
2757- schemas_to_delete .add (schema )
2758-
2759- if environment .suffix_target .is_table :
2760- view_name = snapshot .qualified_view_name .for_environment (
2761- environment .naming_info , dialect = adapter .dialect
2762- )
2763- views_to_delete .add (view_name )
2764-
2765- # Add snapshot tables
2766- table_name = snapshot .table_name ()
2767- tables_to_delete .add (table_name )
2768-
2769- # Display what will be deleted
2770- self .console .log_error ("\n " + "=" * 50 + "\n " )
2771- if schemas_to_delete :
2772- self .console .log_error ("Schemas to be deleted:" )
2773- for schema in sorted (schemas_to_delete ):
2774- self .console .log_error (f" • { schema } " )
2775-
2776- if views_to_delete :
2777- self .console .log_error ("\n Environment views to be deleted:" )
2778- for view in sorted (views_to_delete ):
2779- self .console .log_error (f" • { view } " )
2780-
2781- if tables_to_delete :
2782- self .console .log_error ("\n Snapshot tables to be deleted:" )
2783- for table in sorted (tables_to_delete ):
2784- self .console .log_error (f" • { table } " )
2785-
2786- self .console .log_error ("\n All SQLMesh state tables will be deleted" )
2787- self .console .log_error ("\n " + "=" * 50 + "\n " )
2788-
2789- # Final confirmation with stronger warning
2790- self .console .log_error (
2791- "!!! CRITICAL WARNING: This action will PERMANENTLY DELETE ALL the above resources!\n "
2792- "This includes ALL tables, views and schemas managed by SQLMesh AND potentially\n "
2793- "external resources created by other tools in these schemas. This action is IRREVERSIBLE!\n "
2794- )
2795-
2796- if not self .console ._confirm ("Are you ABSOLUTELY SURE you want to proceed with deletion?" ): # type: ignore
2797- self .console .log_error ("Destroy operation cancelled." )
2798- return False
2799-
28002769 # Invalidate all environments, including prod
28012770 for environment in self .state_reader .get_environments ():
28022771 self .state_sync .invalidate_environment (name = environment .name , protect_prod = False )
0 commit comments