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