Skip to content

Commit f65a4ae

Browse files
committed
Fix critical bug in delete_tree
1 parent e19d276 commit f65a4ae

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
@@ -854,21 +854,19 @@ async def delete_tree(self, external_only=True):
854854
Any DataSources belonging to those Nodes and any Assets associated (only) with
855855
those DataSources will also be deleted.
856856
"""
857-
conditions = []
858857
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
861859
async with self.context.session() as db:
862860
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+
)
868867
)
868+
.filter(condition)
869869
)
870-
for condition in conditions:
871-
count_int_asset_statement.filter(condition)
872870
count_int_assets = (
873871
await db.execute(count_int_asset_statement)
874872
).scalar()
@@ -879,26 +877,26 @@ async def delete_tree(self, external_only=True):
879877
"If you want to delete them, pass external_only=False."
880878
)
881879
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
894881
# 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+
)
898898
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)
902900
result = await db.execute(del_node_statement)
903901
await db.commit()
904902
return result.rowcount

0 commit comments

Comments
 (0)