Skip to content

Commit 9e95836

Browse files
committed
Fix critical bug in delete_tree
1 parent 9b1e18a commit 9e95836

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

tiled/catalog/adapter.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -896,21 +896,19 @@ async def delete_tree(self, external_only=True):
896896
Any DataSources belonging to those Nodes and any Assets associated (only) with
897897
those DataSources will also be deleted.
898898
"""
899-
conditions = []
900899
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
903901
async with self.context.session() as db:
904902
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+
)
910909
)
910+
.filter(condition)
911911
)
912-
for condition in conditions:
913-
count_int_asset_statement.filter(condition)
914912
count_int_assets = (
915913
await db.execute(count_int_asset_statement)
916914
).scalar()
@@ -921,26 +919,26 @@ async def delete_tree(self, external_only=True):
921919
"If you want to delete them, pass external_only=False."
922920
)
923921
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
936923
# 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+
)
940940
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)
944942
result = await db.execute(del_node_statement)
945943
await db.commit()
946944
return result.rowcount

0 commit comments

Comments
 (0)