diff --git a/datasette_edit_schema/__init__.py b/datasette_edit_schema/__init__.py index bf204aa..58846ab 100644 --- a/datasette_edit_schema/__init__.py +++ b/datasette_edit_schema/__init__.py @@ -312,6 +312,29 @@ async def edit_schema_table(request, datasette): raise NotFound("Table not found") if request.method == "POST": + + def get_schema(conn): + table_obj = sqlite_utils.Database(conn)[table] + if not table_obj.exists(): + return None + return table_obj.schema + + before_schema = await database.execute_fn(get_schema) + + async def track_analytics(): + after_schema = await database.execute_fn(get_schema) + # Don't track drop tables, which happen when after_schema is None + if after_schema is not None and after_schema != before_schema: + await datasette.track_event( + AlterTableEvent( + actor=request.actor, + database=database_name, + table=table, + before_schema=before_schema, + after_schema=after_schema, + ) + ) + formdata = await request.post_vars() if formdata.get("action") == "update_columns": types = {} @@ -367,31 +390,35 @@ def transform_the_table(conn): await database.execute_write_fn(transform_the_table, block=True) datasette.add_message(request, "Changes to table have been saved") - + await track_analytics() return Response.redirect(request.path) if formdata.get("action") == "update_foreign_keys": - return await update_foreign_keys( + response = await update_foreign_keys( request, datasette, database, table, formdata ) elif formdata.get("action") == "update_primary_key": - return await update_primary_key( + response = await update_primary_key( request, datasette, database, table, formdata ) elif "drop_table" in formdata: - return await drop_table(request, datasette, database, table) + response = await drop_table(request, datasette, database, table) elif "add_column" in formdata: - return await add_column(request, datasette, database, table, formdata) + response = await add_column(request, datasette, database, table, formdata) elif "rename_table" in formdata: - return await rename_table(request, datasette, database, table, formdata) + response = await rename_table(request, datasette, database, table, formdata) elif "add_index" in formdata: column = formdata.get("add_index_column") or "" unique = formdata.get("add_index_unique") - return await add_index(request, datasette, database, table, column, unique) + response = await add_index( + request, datasette, database, table, column, unique + ) elif any(key.startswith("drop_index_") for key in formdata.keys()): - return await drop_index(request, datasette, database, table, formdata) + response = await drop_index(request, datasette, database, table, formdata) else: - return Response.html("Unknown operation", status=400) + response = Response.html("Unknown operation", status=400) + await track_analytics() + return response def get_columns_and_schema_and_fks_and_pks_and_indexes(conn): db = sqlite_utils.Database(conn) @@ -450,13 +477,15 @@ def get_columns_and_schema_and_fks_and_pks_and_indexes(conn): all_columns_to_manage_foreign_keys = [ { "name": column["name"], - "foreign_key": foreign_keys_by_column.get(column["name"])[0] - if foreign_keys_by_column.get(column["name"]) - else None, + "foreign_key": ( + foreign_keys_by_column.get(column["name"])[0] + if foreign_keys_by_column.get(column["name"]) + else None + ), "suggestions": [], - "options": integer_primary_keys - if column["type"] is int - else string_primary_keys, + "options": ( + integer_primary_keys if column["type"] is int else string_primary_keys + ), } for column in columns ] diff --git a/tests/test_edit_schema.py b/tests/test_edit_schema.py index db4e6ea..5a7b44a 100644 --- a/tests/test_edit_schema.py +++ b/tests/test_edit_schema.py @@ -343,6 +343,7 @@ async def test_transform_table( cookies = {"ds_actor": ds.sign({"a": {"id": "root"}}, "actor")} db = sqlite_utils.Database(db_path) table = db["creatures"] + before_schema = table.schema assert table.columns_dict == {"name": str, "description": str} csrftoken = ( await ds.client.get("/-/edit-schema/data/creatures", cookies=cookies) @@ -361,6 +362,11 @@ async def test_transform_table( assert [c.name for c in table.columns] == expected_order assert len(messages) == 1 assert messages[0][0] == expected_message + # Should have tracked an event + event = get_last_event(ds) + assert event.name == "alter-table" + assert event.before_schema == before_schema + assert event.after_schema == table.schema @pytest.mark.asyncio