Skip to content

Commit

Permalink
Track alter-event on rename, requires datasette>1.0a8
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 16, 2024
1 parent 3d67551 commit 4bc989b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions datasette_edit_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ async def rename_table(request, datasette, database, table, formdata):
return redirect

try:
before_schema = await database.execute_fn(
lambda conn: sqlite_utils.Database(conn)[table].schema
)
await database.execute_write(
"""
ALTER TABLE [{}] RENAME TO [{}];
Expand All @@ -685,9 +688,22 @@ async def rename_table(request, datasette, database, table, formdata):
),
block=True,
)
after_schema = await database.execute_fn(
lambda conn: sqlite_utils.Database(conn)[new_name].schema
)
datasette.add_message(
request, "Table renamed to '{}'".format(new_name), datasette.INFO
)
await track_event(
datasette,
"AlterTableEvent",
actor=request.actor,
database=database.name,
table=new_name,
before_schema=before_schema,
after_schema=after_schema,
)

except Exception as error:
datasette.add_message(
request, "Error renaming table: {}".format(str(error)), datasette.ERROR
Expand Down
8 changes: 8 additions & 0 deletions tests/test_edit_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,14 @@ async def test_rename_table(db_path, new_name, should_work, expected_message):
assert response.status_code == 302
if should_work:
expected_path = "/-/edit-schema/data/{}".format(new_name)
if expected_message != "Table name was the same":
event = get_last_event(ds)
if event:
assert event.name == "alter-table"
assert event.table == new_name
assert new_name in event.properties()["after_schema"]
assert "creatures" in event.properties()["before_schema"]

else:
expected_path = "/-/edit-schema/data/creatures"
assert response.headers["location"] == expected_path
Expand Down

0 comments on commit 4bc989b

Please sign in to comment.