@@ -896,21 +896,19 @@ async def delete_tree(self, external_only=True):
896
896
Any DataSources belonging to those Nodes and any Assets associated (only) with
897
897
those DataSources will also be deleted.
898
898
"""
899
- conditions = []
900
899
segments = self .ancestors + [self .key ]
901
- for generation in range (len (segments )):
902
- conditions .append (orm .Node .ancestors [generation ] == segments [0 ])
900
+ condition = orm .Node .ancestors == segments
903
901
async with self .context .session () as db :
904
902
if external_only :
905
- count_int_asset_statement = select (
906
- func .count (orm .Asset .data_uri )
907
- ).filter (
908
- orm .Asset .data_sources .any (
909
- orm .DataSource .management != Management .external
903
+ count_int_asset_statement = (
904
+ select (func .count (orm .Asset .data_uri ))
905
+ .filter (
906
+ orm .Asset .data_sources .any (
907
+ orm .DataSource .management != Management .external
908
+ )
910
909
)
910
+ .filter (condition )
911
911
)
912
- for condition in conditions :
913
- count_int_asset_statement .filter (condition )
914
912
count_int_assets = (
915
913
await db .execute (count_int_asset_statement )
916
914
).scalar ()
@@ -921,26 +919,26 @@ async def delete_tree(self, external_only=True):
921
919
"If you want to delete them, pass external_only=False."
922
920
)
923
921
else :
924
- sel_int_asset_statement = select (
925
- orm .Asset .data_uri , orm .Asset .is_directory
926
- ).filter (
927
- orm .Asset .data_sources .any (
928
- orm .DataSource .management != Management .external
929
- )
930
- )
931
- for condition in conditions :
932
- sel_int_asset_statement .filter (condition )
933
- int_assets = (await db .execute (sel_int_asset_statement )).all ()
934
- for data_uri , is_directory in int_assets :
935
- delete_asset (data_uri , is_directory )
922
+ raise NotImplementedError
936
923
# TODO Deal with Assets belonging to multiple DataSources.
937
- del_asset_statement = delete (orm .Asset )
938
- for condition in conditions :
939
- del_asset_statement .filter (condition )
924
+ asset_ids_to_delete = (
925
+ select (orm .Asset .id )
926
+ .join (
927
+ orm .DataSourceAssetAssociation ,
928
+ orm .DataSourceAssetAssociation .asset_id == orm .Asset .id ,
929
+ )
930
+ .join (
931
+ orm .DataSource ,
932
+ orm .DataSource .id == orm .DataSourceAssetAssociation .data_source_id ,
933
+ )
934
+ .join (orm .Node , orm .Node .id == orm .DataSource .node_id )
935
+ .filter (condition )
936
+ )
937
+ del_asset_statement = delete (orm .Asset ).where (
938
+ orm .Asset .id .in_ (asset_ids_to_delete )
939
+ )
940
940
await db .execute (del_asset_statement )
941
- del_node_statement = delete (orm .Node )
942
- for condition in conditions :
943
- del_node_statement .filter (condition )
941
+ del_node_statement = delete (orm .Node ).filter (condition )
944
942
result = await db .execute (del_node_statement )
945
943
await db .commit ()
946
944
return result .rowcount
0 commit comments