From df11fae49347e522f43bf3147a62b5d15204d31a Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 29 Jan 2024 11:21:30 -0800 Subject: [PATCH] Fix bug with schema not correcctly shown, closes #49 --- datasette_edit_schema/__init__.py | 5 +++-- tests/test_edit_schema.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/datasette_edit_schema/__init__.py b/datasette_edit_schema/__init__.py index 4d35416..b42ea96 100644 --- a/datasette_edit_schema/__init__.py +++ b/datasette_edit_schema/__init__.py @@ -392,10 +392,11 @@ def get_columns_and_schema_and_fks_and_pks_and_indexes(conn): textwrap.dedent( """ select group_concat(sql, '; - ') from sqlite_master where tbl_name = 'Orders' + ') from sqlite_master where tbl_name = ? order by type desc """ - ) + ), + [table], ).fetchone()[0] return columns, schema, t.foreign_keys, t.pks, t.indexes diff --git a/tests/test_edit_schema.py b/tests/test_edit_schema.py index 81565ac..d02dee9 100644 --- a/tests/test_edit_schema.py +++ b/tests/test_edit_schema.py @@ -571,6 +571,28 @@ async def test_permission_alter_table(permission_plugin, ds, rules_allow, should assert response.status_code == 302 +@pytest.mark.asyncio +async def test_table_form_contains_schema(permission_plugin, ds): + ds._rules_allow = [ + Rule( + actor_id="user", + action="edit-schema", + database="data", + resource=None, + ), + ] + response = await ds.client.get( + "/-/edit-schema/data/creatures", + cookies={"ds_actor": ds.sign({"a": {"id": "user"}}, "actor")}, + ) + assert response.status_code == 200 + assert ( + "CREATE TABLE [creatures]" in response.text + # In case we remove '[' in the future: + or "CREATE TABLE creatures" in response.text + ) + + @pytest.mark.asyncio @pytest.mark.parametrize( "new_name,should_work,expected_message",