@@ -854,21 +854,19 @@ async def delete_tree(self, external_only=True):
854
854
Any DataSources belonging to those Nodes and any Assets associated (only) with
855
855
those DataSources will also be deleted.
856
856
"""
857
- conditions = []
858
857
segments = self .ancestors + [self .key ]
859
- for generation in range (len (segments )):
860
- conditions .append (orm .Node .ancestors [generation ] == segments [0 ])
858
+ condition = orm .Node .ancestors == segments
861
859
async with self .context .session () as db :
862
860
if external_only :
863
- count_int_asset_statement = select (
864
- func .count (orm .Asset .data_uri )
865
- ).filter (
866
- orm .Asset .data_sources .any (
867
- orm .DataSource .management != Management .external
861
+ count_int_asset_statement = (
862
+ select (func .count (orm .Asset .data_uri ))
863
+ .filter (
864
+ orm .Asset .data_sources .any (
865
+ orm .DataSource .management != Management .external
866
+ )
868
867
)
868
+ .filter (condition )
869
869
)
870
- for condition in conditions :
871
- count_int_asset_statement .filter (condition )
872
870
count_int_assets = (
873
871
await db .execute (count_int_asset_statement )
874
872
).scalar ()
@@ -879,26 +877,26 @@ async def delete_tree(self, external_only=True):
879
877
"If you want to delete them, pass external_only=False."
880
878
)
881
879
else :
882
- sel_int_asset_statement = select (
883
- orm .Asset .data_uri , orm .Asset .is_directory
884
- ).filter (
885
- orm .Asset .data_sources .any (
886
- orm .DataSource .management != Management .external
887
- )
888
- )
889
- for condition in conditions :
890
- sel_int_asset_statement .filter (condition )
891
- int_assets = (await db .execute (sel_int_asset_statement )).all ()
892
- for data_uri , is_directory in int_assets :
893
- delete_asset (data_uri , is_directory )
880
+ raise NotImplementedError
894
881
# TODO Deal with Assets belonging to multiple DataSources.
895
- del_asset_statement = delete (orm .Asset )
896
- for condition in conditions :
897
- del_asset_statement .filter (condition )
882
+ asset_ids_to_delete = (
883
+ select (orm .Asset .id )
884
+ .join (
885
+ orm .DataSourceAssetAssociation ,
886
+ orm .DataSourceAssetAssociation .asset_id == orm .Asset .id ,
887
+ )
888
+ .join (
889
+ orm .DataSource ,
890
+ orm .DataSource .id == orm .DataSourceAssetAssociation .data_source_id ,
891
+ )
892
+ .join (orm .Node , orm .Node .id == orm .DataSource .node_id )
893
+ .filter (condition )
894
+ )
895
+ del_asset_statement = delete (orm .Asset ).where (
896
+ orm .Asset .id .in_ (asset_ids_to_delete )
897
+ )
898
898
await db .execute (del_asset_statement )
899
- del_node_statement = delete (orm .Node )
900
- for condition in conditions :
901
- del_node_statement .filter (condition )
899
+ del_node_statement = delete (orm .Node ).filter (condition )
902
900
result = await db .execute (del_node_statement )
903
901
await db .commit ()
904
902
return result .rowcount
0 commit comments