Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent orphaning nodes by deleting a non-empty container #503

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tiled/_tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,31 @@ async def test_delete(tree):
metadata={"date": datetime.now(), "array": numpy.array([1, 2, 3])},
key="x",
)


@pytest.mark.asyncio
async def test_delete_non_empty_node(tree):
with Context.from_app(build_app(tree)) as context:
client = from_context(context)
a = client.create_container("a")
b = a.create_container("b")
c = b.create_container("c")
d = c.create_container("d")

# Cannot delete non-empty nodes
assert "a" in client
with fail_with_status_code(409):
client.delete("a")
assert "b" in a
with fail_with_status_code(409):
a.delete("b")
assert "c" in b
with fail_with_status_code(409):
b.delete("c")
assert "d" in c
assert not list(d) # leaf is empty
# Delete from the bottom up.
c.delete("d")
b.delete("c")
a.delete("b")
client.delete("a")
9 changes: 9 additions & 0 deletions tiled/catalog/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Revision,
SortingItem,
)
from ..utils import Conflicts
from . import orm
from .utils import safe_path

Expand Down Expand Up @@ -91,6 +92,14 @@ async def revisions(self, offset, limit):

async def delete(self):
async with self._context.session() as db:
is_child = orm.Node.ancestors == self.ancestors + [self.key]
num_children = (
await db.execute(select(func.count(orm.Node.key)).where(is_child))
).scalar()
if num_children:
raise Conflicts(
"Cannot delete container that is not empty. Delete contents first."
)
for data_source in self.data_sources:
if data_source.management != Management.external:
# TODO Handle case where the same Asset is associated
Expand Down
1 change: 1 addition & 0 deletions tiled/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ..utils import tree # noqa: F401
from .constructors import from_context, from_profile, from_uri # noqa: F401
from .container import ASCENDING, DESCENDING # noqa: F401
from .context import Context # noqa: F401
Expand Down
11 changes: 9 additions & 2 deletions tiled/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ..media_type_registration import (
compression_registry as default_compression_registry,
)
from ..utils import SHARE_TILED_PATH, UnsupportedQueryType
from ..utils import SHARE_TILED_PATH, Conflicts, UnsupportedQueryType
from ..validation_registration import validation_registry as default_validation_registry
from . import schemas
from .authentication import get_current_principal
Expand Down Expand Up @@ -271,8 +271,15 @@ async def index(
async def tiled_ui_settings():
return ui_settings

@app.exception_handler(Conflicts)
async def conflicts_exception_handler(request: Request, exc: Conflicts):
message = exc.args[0]
return JSONResponse(status_code=409, content={"detail": message})

@app.exception_handler(UnsupportedQueryType)
async def unicorn_exception_handler(request: Request, exc: UnsupportedQueryType):
async def unsupported_query_type_exception_handler(
request: Request, exc: UnsupportedQueryType
):
query_type = exc.args[0]
return JSONResponse(
status_code=400,
Expand Down
5 changes: 5 additions & 0 deletions tiled/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ class UnsupportedQueryType(TypeError):
pass


class Conflicts(Exception):
"Prompts the server to send 409 Conflicts with message"
pass


# Arrow obtained an official MIME type 2021-06-23.
# https://www.iana.org/assignments/media-types/application/vnd.apache.arrow.file
APACHE_ARROW_FILE_MIME_TYPE = "application/vnd.apache.arrow.file"
Expand Down