Skip to content

Commit

Permalink
DropTableEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2024
1 parent 1afcab8 commit 068d294
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
20 changes: 19 additions & 1 deletion datasette/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ class CreateTableEvent(Event):
schema: str


@dataclass
class DropTableEvent(Event):
"""
Event name: ``drop-table``
A table has been dropped from the database.
:ivar database: The name of the database where the table was dropped.
:type database: str
:ivar table: The name of the table that was dropped
:type table: str
"""

name = "drop-table"
database: str
table: str


@hookimpl
def register_events():
return [LoginEvent, LogoutEvent, CreateTableEvent, CreateTokenEvent]
return [LoginEvent, LogoutEvent, CreateTableEvent, CreateTokenEvent, DropTableEvent]
6 changes: 6 additions & 0 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from datasette.plugins import pm
from datasette.database import QueryInterrupted
from datasette.events import DropTableEvent
from datasette import tracer
from datasette.utils import (
add_cors_headers,
Expand Down Expand Up @@ -587,6 +588,11 @@ def drop_table(conn):
sqlite_utils.Database(conn)[table_name].drop()

await db.execute_write_fn(drop_table)
await self.ds.track_event(
DropTableEvent(
actor=request.actor, database=database_name, table=table_name
)
)
return Response.json({"ok": True}, status=200)


Expand Down
7 changes: 7 additions & 0 deletions tests/test_api_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,13 @@ async def test_drop_table(ds_write, scenario):
headers=_headers(token),
)
assert response2.json() == {"ok": True}
# Check event
event = last_event(ds_write)
assert event.name == "drop-table"
assert event.actor == {"id": "root", "token": "dstok"}
assert event.table == "docs"
assert event.database == "data"
# Table should 404
assert (await ds_write.client.get("/data/docs")).status_code == 404


Expand Down

0 comments on commit 068d294

Please sign in to comment.